Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Perl References

Perl References

A reference is a pointer. A Perl reference is a scalar type that can point to variables, arrays, hashes (also called associative arrays), and even subroutines, and can be used anywhere in a program.


Creating References

When defining a variable, adding a \ before the variable name gives you a reference to that variable, for example:



$scalarref = \$foo;     # 标量变量引用

$arrayref  = \@ARGV;    # 列表的引用

$hashref   = \%ENV;     # 哈希的引用

$coderef   = \&handler; # 子过程引用

$globref   = \*foo;     # GLOB句柄引用

For arrays we can use anonymous array references, defined with []:



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

The elements of an anonymous array can themselves be anonymous arrays, so we can use this method to construct arrays of arrays, building arrays of any dimension.



my $aref = [

        [1, 2, 3],

        [4, 5, 6],

        [7, 8, 9],

]

For hashes we can use anonymous hash references, defined with {}:



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

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



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


Dereferencing

Dereferencing can use $, @, or % depending on the type, as shown in the following examples:

Example

#!/usr/bin/perl

$var

=

10

;

$r references the $var scalar

$r

= \

$var

;

Output the value of the variable stored locally in $r

print

"

$var

is :

"

, $

$r

,

"

\n

"

;

@var

=

(

1

,

2

,

3

)

;

$r references the @var array

$r

= \

@var

;

Output the value of the variable stored locally in $r

print

"

@var is:

"

, @

$r

,

"

\n

"

;

%var

=

(

'

key1

'

=>

10

,

'

key2

'

=>

20

)

;

$r references the %var hash

$r

= \

%var

;

Output the value of the variable stored locally in $r

print

"

%var is :

"

, %

$r

,

"

\n

"

;

Running the above examples produces the following results:



10 为 : 10

1 2 3 为: 123

\%var 为 : key110key220

If you cannot determine the variable type, you can use ref to determine it. The list of return values is as follows; if none of the following values apply, it returns false:



SCALAR

ARRAY

HASH

CODE

GLOB

REF

The example is 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

"

;

Running the above examples produces the following results:



r 的引用类型 : SCALAR

r 的引用类型 : ARRAY

r 的引用类型 : HASH


Circular References

Circular references occur when two references contain each other. You need to use them carefully, otherwise they will cause memory leaks, as in the following example:

Example

#!/usr/bin/perl

my

$foo

=

100

;

$foo

= \

$foo

;

print

"

Value of foo is :

"

, $

$foo

,

"

\n

"

;

Running the above examples produces the following results:



Value of foo is : REF(0x9aae38)


Reference Functions

Function reference format: &

Calling a referenced function format: & + the name of the created reference.

The example is as follows:

Example

#!/usr/bin/perl

Function definition

sub

PrintHash

{

my

(

%hash

)

=

@_

;

foreach

$item

(

%hash

)

{

print

"

element:

$item

\n

"

;

}

}

%hash

=

(

'

name

'

=>

'

runoob

'

,

'

age

'

=>

3

)

;

Create a function reference

$cref

= &

PrintHash

;

Call a function using a reference

&

$cref

(

%hash

)

;

Running the above examples produces the following results:



元素 : age

元素 : 3

元素 : name

元素 : runoob