Definition 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
ChildProgram\ & 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;
Translated from:
Http://www.thegeekstuff.com/2010/06/perl-array-reference-examples/
Http://www.thegeekstuff.com/2010/06/perl-hash-reference/
Reference is the address of another variable. Reference can point to array, hash, or PerlCode. Reference makes Perl code run faster.
One array reference and Dereference
1) array reference
Normally, the elements stored in the list are as follows in the array: @ array = ("one", "two", "three", "four", "five ");
Use \ to assign the array address to the reference variable, as shown below:$ Array_ref =\@ array;
If you print $ array_ref, the following is displayed: array (0x1a2b3c );
The reference of array can be passed to subroutine, as follows:
Sub add_numbers
{
My $ array_ref = shift;
.....
}
@ Numbers = (11,2, 3,45 );
$ Array_ref = add_numbers (\ @ numbers );
In the above Code, we need to perform dereference on the array reference before using the elements in the array.
Below are the advantages of passing an array reference to Subroutine:
* If the array is passed to subroutine, Perl copies the entire array to @ _ again. When the array is large, this will be very inefficient.
* When we need to modify the original array in subroutine, we need to pass the array reference.
* Reference is actually the essence of constructing complex data structures.
You can also assign an anonymous array to reference as follows:$ Array_ref = [11, 2, 3, 45]
2) dereference of the array reference
In subroutine, we can use the following method to reference dereference array: @ {$ array_ref };
Obtain the first element: $ {$ array_ref} [0];
Alternatively, you can use the array reference with special characters of Perl, as shown below:
# Get all the elements of @ numbers array.
@ {$ _ [0]}
# Get A participant element. This gives the first element of the array.
$ {$ _ [0]} [0]
NOTE: If only dereference is a simple Scalar variable, ignore the brackets, as shown below:
@ $ Array_ref # Same as @ {$ array_ref}
$ Array_ref # Same as $ {$ array_ref}
$ _ [0] # Not a simple Scalar variable and this cant be dereferenced,
Binary hash reference and Dereference
The reference and dereference of hash are the same as those of array.
The hash reference is as follows:
% Author = (
'Name' => "Harsha ",
'Design' => "manager"
);
$ Hash_ref =\% author;
The access element after dereference is as follows:
$ Name =$ {$ hash_ref} {name}; equivalent to my $ name = $ hash_ref-> {name };
Access all keys as follows:
My @ keys = keys % {$ hash_ref}; equivalent to my @ keys = keys % author;
For simple Scalar variables, ignore the parentheses, as shown below: My @ keys = keys % $ hash_ref; $ name =$ $ hash_ref {name };
The reference of anonymous hash is as follows:
My $ hash_ref = {
'Name' => "Harsha ",
'Design' => "manager"
};
Use: $ name =$ {$ hash_ref} {name };
Three pairs of reference variables-> to obtain attributes
My $ name = $ hash_ref-> {name };
Complete!