Perl reference


Release date:2023-10-19 Update date:2023-10-21 Editor:admin View counts:230

Label:

Perl reference

A reference is a pointer. 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.

Create referenc

When defining a variable, add a \ 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

In an array, we can use anonymous array references, using the [] definition:

$aref= [ 1,"foo",undef,13 ];

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.

my $aref = [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9],
]

In the hash we can use anonymous hash references, using the {} definition:

$href= { APR =>4, AUG =>8 };

We can also create an anonymous subroutine reference without a subroutine name:

$coderef = sub { print "Runoob!\n" };

Dereference

Dereference can be cancelled using $, @, 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";

The execution result of the above example is:

10 is: 10
1, 2, and 3 are: 123
\%Var is: key110key220

If you are not sure about the variable type, you can use the 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

Examples are as follows:

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";

The execution result of the above example is:

reference type of r : SCALAR
reference type of r : ARRAY
reference type of r: HASH

Circular reference

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:

Example

#!/usr/bin/perlmy$foo=100;$foo= \\$foo;print"Value of foo is :",
$$foo,"\\n";

The execution result of the above example is:

Value of foo is : REF(0x9aae38)

Reference function

Function reference format: \\&

Call reference function format: & + the reference name created.

Examples are as follows:

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);

The execution result of the above example is:

Element: age
Element: 3
Element: name
Element: runoob

Powered by TorCMS (https://github.com/bukun/TorCMS).