#!/usr/bin/perl
$glov = "hello";
&change(*glov);
print $glov, "\n";
sub change {
local(*alias) = @_;
print "$alias\n";
$alias = "world";
}
$ ./changeglobv.pl
hello
world
#!/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.
#!/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.
*array = \@array; # *array只引用数组
*scalar = \$scalar; # *saclar 只引用变量
*hash = \%assoc_array; # *hash 只引用散列表
*func = \&subroutine; # *func 只引用子函数
$ ./chalias.pl
12345
7 2 3 4
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |