5.33. Perl reference

发布时间 : 2025-10-25 13:33:02 UTC      

Page Views: 9 views

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.

5.33.1. 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" }; 

5.33.2. 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 

5.33.3. 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) 

5.33.4. 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 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。