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