I. Concept of reference:
Perl provides a data type similar to the pointer function in C/C ++, called reference. This reference is different from the reference concept in C ++, but the effect is the same; references in Perl are pointers used to trace the addresses of variables in the memory. They can trace simple Scalar variables, array variables, hash variables, file handle variables, and even subprograms; the reference stores the address of a variable in the memory, which is useful for tracking large data objects. The reference is also a data type, which is called a reference variable, the referenced variables store the memory address, regardless of the type of the data object to be referenced, regardless of the memory space occupied by the referenced data object, the memory address of the referenced data object is always stored in the referenced variable; the memory address is an unsigned integer, so the referenced variable is always a simple Scalar variable, it is used in the same way as other common scalar variables;
If a reference is used in the program, the perl interpreter no longer calls the value of the variable through the name of the variable, but calls the value of the variable by referencing the memory address in the variable;
In short, the referenced variable can trace the address of the variable in the memory, and it can trace any type of variable. The referenced variable is always a scalar used to store the address of another pair in the memory, the referenced variable occupies an address space in the memory, regardless of the address of a scalar variable or the memory address of other large objects;
Ii. Usage of references:
If the scalar variable $ pointer stores an array address, you must extract the address of the array before accessing the elements of the array; for example, you can access the elements in the array through the form "@ $ Pointer". The meaning of the form "@ $ Pointer" is to retrieve the address stored in $ pointer As an array. Similarly, if $ pointer stores the address of a hash variable, you can access the elements in the hash variable in the form of "% $ Pointer;
Iii. Definitions of common data types:
Data Type Definition example
Scalar variable/$ var $ pointer =/$ VaR
Array/@ array $ pointer =/@ Array
Hash variable/% hash $ pointer =/% hash
File handle/* filehandle $ pointer =/
Constant/constant $ pointer =/3.1415926
Subroutine/& subroutine $ pointer =/& subroutine
Anonymous array [LIST] $ pointer = ["Smith", "Jack", "Jimmy", "zhazha"]
Anonymous hash variable {key => value} $ pointer = {key1 => value1, key2 => value2}
Anonymous subroutine sub {}$ pointer = sub {printf ("Hello, Perl world/N ");}
Object Reference bless $ self;
Iv. Type of referenced variables:
1. Rough points can be divided into hard reference and symbolic reference. symbolic reference contains the name of a variable. symbolic reference is similar to a soft connection with a file name or operating system, hard reference is similar to hard connection in the operating system. When the reference count is 0, Perl Automatically releases the memory occupied by the referenced project; if the referenced Project is a class object, Perl automatically calls the analysis function of this object to release its memory; perl itself is an object-oriented language. Everything in Perl is an object, and packages and modules are objects that are easier to use;
2. If the referenced variables are classified according to the address type stored in the referenced variables, the referenced variables can be divided into six types:
★Direct reference variables: as the name suggests, directly reference variables to save the addresses of other variables, including scalar, array variables, hash variables, file handle variables, and other types of variables; directly referencing a variable can modify the value of the referenced variable, and the address value in the referenced variable can also be modified. directly referencing the variable is defined by the Backslash;
Direct reference to arrays and hash variables can be defined as follows:
$ Lparray =/@ array; # $ lparray stores the first address of the array @ array. $ lparray represents the array name;
In this case, the access method for array elements is: $ lparray-> [$ Index], $ lparray [$ Index], or $ {$ lparray} [$ Index];
$ Lphash =/% hash; # $ lphash stores the first address of the hash variable % hash. $ lphash indicates the name of the hash variable;
In this case, the elements of the hash variable can be accessed in the following ways: $ lphash-> {$ key}, $ lphash {$ key}, or $ {$ lphash} {$ key };
★Constant reference variable: it refers to the direct number instead of the variable. It is also defined by the Backslash "/". The address value saved by the constant reference variable can be modified, however, the referenced constant value cannot be modified because it is referenced as a constant. Its storage method determines that it is stored in another protected memory space;
★Subroutine-referenced variable: it is a mixture of directly referenced variables and constant-referenced variables. The subroutine-referenced variable cannot be used to modify the subroutine pointed to by the subroutine-referenced variable, but the subroutine itself can be modified; when generating a subroutine reference variable, the bitwise AND operator "&" must be used to define the referenced subroutine rather than the direct number;
★Symbol reference variable: A symbol reference variable is a variable that contains the direct number name of another variable. Perl uses the Backslash "/" and variable name to define the reference symbol;
★Anonymous reference variables: anonymous reference variables can generate non-named object references. These objects are also isolated objects. Anonymous references include anonymous arrays, anonymous hash variables, and anonymous subprograms; use the operators "[]", "{}", and "sub {}" respectively {}". The usage of the anonymous reference operator is context-related. A reference is generated only when a scalar variable is assigned a value. The anonymous subroutine operator must end with a semicolon in the subroutine definition; Perl treats the anonymous referenced variable as a statement with a specific address in the memory address;
★Object reference variable: an object reference variable is a typical application of anonymous hash variable reference. Perl uses the bless operator to handle such references, you can generate a connection between the referenced object and the class of the generated object;
V. indirect reference:
The value of the variable to which the reference is directed is called indirect reference. To obtain the final value pointed to by the referenced variable in the memory, you must tell the perl interpreter to find the memory address saved as the reference variable of the numeric container. In reality, every "$" symbol in Perl is a numeric container, the Perl interpreter interprets each "$" symbol from the right to the left, or from the inner to the external order. Every time you encounter the "$" symbol, the object on the right of $ is used as a numerical container. For example: $ pointer, Perl regards pointer As a numerical container, get the address stored in the object pointer from the first $ on the right. When $ on the left is met, $ pointer is treated as a numerical container, and $ pointer is saved as the address, $ on the left represents a value container, so $ {$ pointer} obtains the value stored in the object referenced by the reference variable;
Note: the pointer is the address. You can access the data stored in the address through the pointer. If the pointer points to an invalid memory address, incorrect data is obtained. Generally, perl returns a null value, but it does not depend on this value. It must correctly Initialize all pointers in the program to point them to valid data items;
6. Anonymous array and anonymous hash variable:
Anonymous array: $ lparray = [1, 2, "A", "B", (4 .. 10), [12 .. 20];
Anonymous hash variable: $ lphash = {key1 => value1, key2 => value2, key3 => value3,..., keyn => valuen };