我有时会看到像这样的Perl代码:
my ( $variable ) = blah....
在单个变量周围放置括号的意义是什么?我认为圆括号只用于声明多个变量,如:
my ( $var1,$var2,$var3 ) = blah...
解决方法
有几种情况下有区别:
>当数组在右侧
my @array = ('a','b','c'); my $variable = @array; # 3 size of @array my ($variable) = @array; # 'a' $array[0]
>当列表在右侧
my $variable = qw/ a b c d /; # 'd' last item of the list my ($variable) = qw/ a b c d /; # 'a' first item of the list
>带有变量(数组/标量)返回值的子程序
sub myFunction { ... return (wantarray() ? @array : $scalar); } my $variable = myFunction(...); # $scalar from the subroutine my ($variable) = myFunction(...); # $array[0] from the subroutine