A reference is a pointer. When defining a variable, add a In an array, we can use anonymous array references, using the The elements of an anonymous array can still be anonymous arrays, so we can use this method to construct an array of arrays, an array of any dimension. In the hash we can use anonymous hash references, using the We can also create an anonymous subroutine reference without a subroutine name: Dereference can be cancelled using The execution result of the above example is: If you are not sure about the variable type, you can use the Examples are as follows: The execution result of the above example is: Circular references occur when two references contain each other. You need to use it carefully, otherwise it will lead to memory leakage, as shown in the following example: The execution result of the above example is: Function reference format: Call reference function format: Examples are as follows: The execution result of the above example is:
Perl
reference is a scalar type that can point to variables, arrays, hash tables (also known as associative arrays) or even subroutines, and can be applied anywhere in the program. 5.33.1. Create referenc #
\
get a reference to this variable, such as:Scalarref= $foo# Scalar variable reference
$arrayref= @ ARGV# Reference to List
$hashref=% ENV# Hashed references
$coderef=&handler# Sub process reference
$globref=* foo# GLOB Handle Reference
[]
definition:$aref= [ 1,"foo",undef,13 ];
my $aref = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]
{}
definition:$href= { APR =>4, AUG =>8 };
$coderef = sub { print "Runoob!\n" };
5.33.2. Dereference #
$
,
@
, or
%
depending on different types. Examples are as follows:Example #
#/ Usr/bin/perl $var=10; #$ R reference $var scalar $r= $var# Output locally stored $r
The variable value print '$var' is: ', $$r,' n '@ Var=(1,2,3); #$ R refers to the @ var array $r=
\\@Var# Output the variable value print "@ var is:" for locally stored $r,
@$r, " n";% Var=('key1 '=>10,'key2'=>20); #$ R reference% var hash $r= % var#
The variable value print " % var of locally stored $r is:",% $r, " n";
10 is: 10
1, 2, and 3 are: 123
\%Var is: key110key220
ref
to judge, the list of return values is as follows, if none of the following values is returned
false
:SCALAR
ARRAY
HASH
CODE
GLOB
REF
Example #
#!/usr/bin/perl$var=10;$r= \\$var;print"reference type of r
:",ref($r),"\\n";@var=(1,2,3);$r= \\@var;print"reference type of r
:",ref($r),"\\n";%var=('key1'=>10,'key2'=>20);$r= \\%var;
print"reference type of r :",ref($r),"\\n";
reference type of r : SCALAR
reference type of r : ARRAY
reference type of r: HASH
5.33.3. Circular reference #
Example #
#!/usr/bin/perlmy$foo=100;$foo= \\$foo;print"Value of foo is :",
$$foo,"\\n";
Value of foo is : REF(0x9aae38)
5.33.4. Reference function #
\\&
&
+
the reference name created.Example #
#!/usr/bin/perl#
Function DefinitionsubPrintHash{my(%hash)=@\_;foreach$item(%hash){print"element
:$item\\n";}}%hash=('name'=>'runoob','age'=>3);# Create a reference to a function$cref=
\\&PrintHash;# Calling functions using references&$cref(%hash);
Element: age
Element: 3
Element: name
Element: runoob