符号引用(typeglob,别名)与全局变量的修改
生活随笔
收集整理的這篇文章主要介紹了
符号引用(typeglob,别名)与全局变量的修改
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
符號(hào)引用(typeglob,別名)與全局變量的修改
不能以為在子函數(shù)里修改了一個(gè)和全局變量同名的變量就會(huì)修改全局變量:#!/usr/bin/perl
$glov = "hello";
change;
print $glov;
sub change {
????????$glov = "world";
}
雖然在子程序 change 里的 $glov 變量和全局變量 $glov 名字相同,但兩個(gè)并不是同一個(gè)變量,子程序里的變量為局部變量。
在子程序里修改全局變量的一種方法是利用“符號(hào)引用”。
符號(hào)引用類似與 Linux 里的軟連接概念,它又稱為別名。創(chuàng)建一個(gè)變量的別名的方法是在實(shí)際變量的前面加上一個(gè) "*" 符號(hào)。星號(hào)("*") 適用于任意類型的變量,包括標(biāo)量,數(shù)組,散列,文件句柄,子函數(shù)等。
別名是符號(hào)表中針對(duì)同名標(biāo)識(shí)符的另一個(gè)名稱,比如 *name 可以表示 $name, @name 以及 %name, &name 等。
當(dāng)通過別名按引用傳遞方式將別名傳遞到函數(shù)中時(shí),需要用 local 函數(shù)聲明一個(gè)私有變量用以接收傳遞進(jìn)來的別名,該私有變量也就是另外一個(gè)別名,然后修改該私有變量也就等效于修改了傳遞進(jìn)來的全局變量。如下程序所示:
#!/usr/bin/perl
$glov = "hello";
&change(*glov);
print $glov, "\n";
sub change {
????????local(*alias) = @_;
????????print "$alias\n";
????????$alias = "world";
}
運(yùn)行輸出:
$ ./changeglobv.pl
hello
world
上面,不能用 my 來聲明這個(gè)私有變量,因?yàn)?my 函數(shù)所創(chuàng)建的變量名稱并不保存在符號(hào)表中,而是位于臨時(shí)緩沖區(qū)中。由于 typeglob 僅能關(guān)聯(lián)到特定的符號(hào)表上,因此 my 不能對(duì)它進(jìn)行私有化,所以要讓 typeglob 本地化就必須使用 local 函數(shù)。
測(cè)試代碼2:
#!/usr/bin/perl
$colors = "rainbow";
@colors = ("red", "green", "yellow");
&printit(*colors);???????? #傳遞進(jìn) colors 數(shù)組的別名
sub printit {
????????local(*whichone) = @_;
????????print *whichone, "\n";
????????$whichone = "hello world";
????????$whichone[0] = "BLUE";?????????? #修改數(shù)組中的元素
}
運(yùn)行輸出:
$ ./alias.pl
*main::colors???????????? #告知 *whichone 是 main 中 colors 的別名
Out of subroutine.
$colors is hello world.
@colors is BLUE green yellow.
測(cè)試代碼3:
#!/usr/bin/perl
@n = split(' ', <STDIN>);
¶ms(*n);
sub params {
????????local (*arr) = @_;
????????print 'The values of the @arr array are ', @arr, "\n";
????????print "The first value is $arr[0]\n";
????????print "the last value is ", pop(@arr), "\n";
????????foreach $value (@arr) {
????????????????$value += 5;
????????????????print "The value is $value.\n";
????????}
}
print "Back in main\n";
print "The new values are @n.\n";
運(yùn)行輸出:
$ ./alias2.pl
1 2 3 4 5?????? #輸入命令行參數(shù)
The values of the @arr array are 12345
The first value is 1
the last value is 5
The value is 6.
The value is 7.
The value is 8.
The value is 9.
Back in main
The new values are 6 7 8 9.
測(cè)試代碼4:
該例子演示通過引用傳遞文件句柄。如果要直接把文件句柄傳遞給子函數(shù),唯一的途徑就是通過引用(注意,還有個(gè)硬引用,這里不涉及)。
#!/usr/bin/perl
open (HD, "<hello.txt") || die "Can not open file: $!";???? #以只讀方式打開文件 hello.txt,并建立相應(yīng)句柄 HD
&readit (*HD);
sub readit {
????????local(*myfile) = @_;?????? #給本地別名 myfile 賦值,即將別名傳遞給子例程
????????while (<myfile>) {?????? #別名是文件句柄 HD 的另一個(gè)名字,while 循環(huán)逐行讀取文件句柄中的各行內(nèi)容
????????????????print;
????????}
}
在上面,別名可以同時(shí)匹配多種類型。如果你只想匹配特定的一種,那么此時(shí)需要用反斜杠運(yùn)算符,Perl 的引用機(jī)制允許對(duì)某類特定變量而不是所有變量類型使用別名,如:
*array = \@array;?? # *array只引用數(shù)組
*scalar = \$scalar;????# *saclar 只引用變量
*hash = \%assoc_array;????# *hash 只引用散列表
*func = \&subroutine;?? # *func 只引用子函數(shù)
測(cè)試代碼5:
#!/usr/bin/perl
@list = (1, 2, 3, 4, 5);
*arr = \@list;??#*arr 此時(shí)只對(duì) @list 數(shù)組引用
print @arr, "\n";
print "$arr\n";???????? #arr 已經(jīng)只能引用數(shù)組而不能引用普通變量,這里內(nèi)容為空
sub alias {???? #修改數(shù)組
????????local (*a) = @_;
????????$a[0] = 7;
????????pop @a;
}
&alias(*arr);
print "@list\n";
$num = 5;
*alnum = \$num; # scalar 只引用變量而不能引用數(shù)組
print "@alnum\n";
運(yùn)行輸出:
$ ./chalias.pl
12345
7 2 3 4
轉(zhuǎn)載于:https://www.cnblogs.com/cosiray/archive/2012/03/21/2409676.html
總結(jié)
以上是生活随笔為你收集整理的符号引用(typeglob,别名)与全局变量的修改的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 清除2960交换机的配置
- 下一篇: PHP实例——获取文件的绝对路径