Perl中的引用

前端之家收集整理的这篇文章主要介绍了Perl中的引用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


地址引用

1.  值

$first  = 1;

$second = \$first;     #  表示地址引用

$first = 3;

print $$second;       # 输出值为3,必须使用$$才可以输出,print $second 输出为空

print $first;               


2  数组

@l = (1,2,3);
$rl = \@l;
print   @$rl;    #输出123
print $rl->[2];  #数据第三个元素

print $l[2];       和上面的意思一样

$rl = [1,3];   指向你们的数组


3   Hash

%hash = (1,'fruit','desk');   #创建hash
print $hash{1};                #读取
$rh = \%hash;                #创建引用
print %$rh;                     #  调用,%$   = %hash 
print keys(%$rh);        #同上
print $rh->{1};              #读取key 1 对应的值
print %hash->{1};       #使用hash读取
print $hash->{1};        #错误


4.   获得引用的类型


$ms = [1,3,4];
print "ref type:".ref($ms);
print qq(\n);


返回值为Array.


其中的'.'为连接符,如

print "ref type:".ref($ms);
print qq(\n);


可以写为

print "ref type:".ref($ms)."\n";

猜你在找的Perl相关文章