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,2,3];   指向你們的數組


3   Hash

%hash = (1,'fruit',2,'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,2,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";







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章