Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Perl references and arrays

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use Perl quotes and arrays. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

A brief introduction to Perl citation

Perl references are pointers that can point to variables, arrays, hash tables (also known as associative arrays) and even subroutines. Pascal or C programmers should be familiar with the concept of Perl references (that is, pointers). Perl references are the address of a value, and the use of them depends on the programmer and the language. In Perl, Perl references can be called pointers, and the two are generic and indistinguishable. Perl references are useful for creating complex data.

The two Perl reference types in Perl5 are hard references and symbolic references. Symbolic references contain variable names, which are useful for creating variable names and positioning at run time. Basically, symbolic Perl references are like file names or soft links in UNIX systems. Hard Perl references are like hard links in the file system.

Perl4 only allows symbol Perl references, which makes it difficult to use. For example, only the symbolic name hash table (named _ main {}) of the package is allowed to be indexed by name. Perl5 allows hard Perl references to data, which is much more convenient.

Hard Perl references track the count of Perl references, and when the number is 00:00, Perl automatically releases the project referenced by Perl, and if the project is an object, it is destructed into the memory pool. Perl itself is an object-oriented language, because everything in Perl is an object, and packages and modules make objects easier to use.

Hard Perl references to simple variables are simple, and for Perl references to non-simple variables, you must explicitly de-reference the Perl and tell it what to do, as detailed in "object-oriented programming in Chapter Perl".

Second, use Perl references

In this chapter, a simple variable refers to a variable like $pointer, which contains only one data item, which can be a number, a string, or an address.

Any simple variable can hold a hard Perl reference. Because arrays and hash tables contain many simple variables, complex data structures can be established, such as arrays of arrays, arrays of hash tables, hash tables of subprograms, and so on. As long as you understand that you are actually working with simple variables, you should be able to correctly deregulate Perl references in the most complex structures.

First of all, let's look at some basic points.

If the value of $pointer is a pointer to an array, the elements in the array are accessed in the form @ $pointer. The form @ $pointer means "take out the address value in $pointer and use it as an array". Similarly,% $pointer is a Perl reference pointing to * elements in the hash table.

There are several ways to build Perl references, and you can make Perl references to almost any data, such as arrays, simple variables, subroutines, file handles, and-Perl references of interest to C programmers. Perl gives you the ability to write extremely complex code that confuses you. :)

Let's take a look at how to create and use Perl references in Perl.

Third, use the backslash (\) operator

The backslash operator is similar to the operator for passing addresses in the C language. It is common to use\ to create a new Perl reference to the variable. Here is an example of creating a Perl reference to a simple variable:

$variavle=22; $pointer=\ $variable; $ice= "jello"; $iceprt=\ $ice

The Perl reference $pointer points to the location where the $variable value is stored, and the Perl reference $iceptr points to "jello". Even if the original Perl reference $variable is destroyed, the value can still be accessed through $pointer, which is a hard Perl reference, so both $pointer and $variable must be destroyed to free the space into the memory pool.

In the above example, the Perl reference variable $pointer stores the address of $variable, not the value itself. To get the value, it takes the form of two $symbols, as follows:

#! / usr/bin/Perl $value=10; $pointer=\ $value; printf "\ nPointerAddress$pointerof$value\ n"; printf "\ nWhatPointer* ($pointer) pointsto$$pointer\ n"

The output is as follows:

PointerAddressSCALAR (0x806c520) of10

WhatPointer* (SCALAR (0x806c520)) pointsto10

Each time you run, the address in the output changes, but you can see that $pointer gives the address, and $$pointer gives the value of $variable.

Take a look at the display of the address. There is a string of hexadecimals after the SCALAR. SCALAR indicates that the address points to a simple variable (that is, a scalar), and the following number is the address of the actual stored value.

Note: the pointer is the address through which the data stored at that address can be accessed. If the pointer points to an invalid address, you will get incorrect data. Normally, Perl returns a null value, but you shouldn't rely on it. Be sure to initialize all pointers correctly in your program to point to valid data items.

4. Perl references and arrays

Perhaps the most important thing to remember about the Perl language is that arrays and hash tables in Perl are always one-dimensional. Therefore, arrays and hash tables only hold scalar values and do not store arrays or other complex data structures directly. The members of the array are either numbers (or strings) or Perl references.

You can use the backslash operator for arrays and hash tables as you would for simple variables, and the Perl references to arrays are as follows:

1BINGUR 3#UsingArrayreferences BINGUR 3#UsingArrayreferences 5$ pointer=\ @ ARGV; 6printf "\ nPointerAddressofARGV=$pointer\ n"; 7$ i=scalar (@ $pointer); 8printf "\ nNumberofarguments:$i\ n"; 9 $iTun0; 10foreach (@ $pointer) {11printf "$i:$$pointer [$iTunes +];\ n"; 12}

The running results are as follows:

$test1234 PointerAddressofARGV=ARRAY (0x806c378) Numberofarguments:4 0RU 1; 1Rd 2; 2:3

Line 5 points the Perl reference $pointer to the array @ ARGV, and line 6 outputs the address of ARGV. $pointer returns the addresses of the elements of the array, which is similar to the array pointer in the C language. Line 7 calls the function scalar () to get the number of elements of the array, which can also be @ ARGV, but the pointer must specify its type as an array in the form of @ $pointer, $pointer gives the address, and the @ symbol indicates that the address passed is the address of the array's * elements. Line 10 is similar to line 7, and line 11 lists all elements in the form $$pointer [$I].

The method of using the backslash operator for associative arrays is the same-- replace all associative array names with Perl references $poniter. Notice that Perl references to arrays and simple variables (scalars) are displayed with types-- ARRAY and SCALAR, as do hash tables (associative arrays) and functions, which are HASH and CODE, respectively.

Similar to arrays, the element that accesses the hash table through a Perl reference is $$pointer {$index}. Of course, $index is the key value of the hash table, not just a number. There are several forms of access, and in addition, you can use the = > operator to build a hash table for better readability.

V. Multidimensional array

The statement @ array=list; can create Perl references to arrays, and square brackets can create Perl references to anonymous arrays. The following statement is an example of a three-dimensional array for drawing:

$line= ['solid','black', [' 1, 2, 3, 3], ['4, 5, 5, 6]]

This statement creates a three-dimensional array of four elements, and the variable $line points to it. The first two elements are scalars, which store the type and color of the line, and the last two elements are Perl references to the anonymous array, the beginning and end of the store line. The syntax for accessing its elements is as follows:

$arrayReference- > [$index] single-dimensionalarray

$arrayReference- > [$index1] [$index2] two-dimensionalarray

$arrayReference- > [$index1] [$index2] [$index3] three-dimensionalarray

You can create structures that are as complex as your intelligence, design experience, and computer memory allow, but * be friendly to people who may read or manage your code-try to keep it simple. On the other hand, if you want to show off your programming skills to others, Perl gives you plenty of opportunity and ability to write code that you will inevitably be confused about. :)

Suggestion: when you want to use more than three-dimensional arrays, * consider using other data structures to simplify the code.

6. Perl reference of subroutine

The Perl reference of the Perl subprogram is similar to the pointer to the function in C, and is constructed as follows:

$pointer_to_sub=sub {... declarationofsub...}

The method to invoke the subroutine through the constructed Perl reference is:

& $pointer_to_sub (parameters)

This is the end of this article on "how to use Perl references and arrays". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report