home PYTHONJAVA
 

Perl reference

A

reference is a pointer. A Perl reference is a scalar type that can point to a variable, an array, a hashtable (also called an associative array), or even a subroutine that can be applied anywhere in the program.


Create a reference

When defining a variable, add a \ in front of the variable name to get a reference to the variable, for example:

$scalarref = \$ Foo; # scalar variable reference
$arrayref = \@ARGV; #List of references
$hashref = \%ENV; # hash reference
$coderef = \&handler; # Subprocess reference
$globref = \*foo; # GLOB handle reference

In an array we can use an anonymous array reference, using [] to define:

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

Anonymous array elements can still be anonymous arrays, so we can construct arrays of arrays in this way, and we can construct arrays of arbitrary dimensions.

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

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

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

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

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

Dereference

Dereferences can be canceled using $, @ or % for different types, as follows:

Instance

#!/usr/bin/perl $var = 10 ; # $r Quote $var scalar $r = \$var; # Output local variable $r variable value print "$var is: ", $$r, "\n"; @var = ( 1, 2, 3) ; # $r Reference @var Array $r = \@var; # Output local variable $r variable value print "@var is: ", @$r, "\n"; %var = ( 'key1' => 10, 'key2' => 20); # $r Reference %var Array $r = \%var; # Output local variable $r variable value print "%var is: ", %$r, "\n";

Execute the above example execution results as:

10 for : 10
1 2 3 for: 123
%var   : key110key220

If you're not sure about the variable type, you can use ref to determine that the return value list is as follows, returning false if none of the following values:

SCALAR
ARRAY
HASH
CODE
GLOB
REF

Examples are as follows:

Instance

#!/usr/bin/perl $var = 10 ; $r = \$var; print " reference type for r: ", ref($r), "\n "; @var = ( 1, 2, 3) ; $r = \@var; print " reference type for r: ", ref($r), "\n "; %var = ( 'key1' => 10, 'key2' => 20); $r = \%var; print " reference type for r: ", ref($r), "\n ";

Executing the above example execution results is:

r reference type : SCALAR
r Reference Type : ARRAY
r Reference Type : HASH

Circular reference

Cycle references appear when two references contain each other. You need to use it carefully, otherwise it will lead to memory leaks, as in the following example:

Instance

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

Executing the above example execution results is:

Value of foo is : REF( 0x9aae38)

reference function

Function reference format: \&

Invoke reference function format: & + Created reference name.

Examples are as follows:

Instance

#!/usr/bin/perl #function definition sub PrintHash{ my (%hash) = @_; foreach $item (%hash){ print "Element: $item\n"; } } %hash= ( 'name' => 'welookups', 'age' => 3); #Create a reference to a function $cref = \&PrintHash; # Calling a function with a reference &$cref(%hash);

Executing the above example execution results is:

Elements : age
Elements : 3
Elements : Name
Elements : welookups





welookups is optimized for learning.© Welookups 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.