Perl参数列表中的美元,符号和分号是什么意思? [英] What do dollar, at-sign and semicolon characters in Perl parameter lists mean?
问题描述
在我的工作中,我在代码库中遇到了许多Perl脚本.其中一些包含具有以下语法奇数的子例程:
I have encountered a number of Perl scripts in the codebase at my job. Some of them contain subroutines with the following syntax oddity:
sub sum($$$) {
my($a,$b,$m)=@_;
for my $i (0..$m) {
$$a[$i] += $$b[$i] if $$b[$i] > 0;
}
}
sub gNode($$;$$) {
my($n,$l,$s,$d) = @_;
return (
"Node name='$n' label='$l' descr='$d'" ,
$s ? ("Shape type='$s' /") : (),
'/Node'
);
}
sub gOut($$@) {
my $h = shift;
my $i = shift;
if ($i > 0) {
print $h (('')x$i, map '<'.$_.'>', @_);
} else {
print $h map '<'.$_.'>', @_;
}
}
撇开这些子例程的作用(我不太确定自己...)的问题,参数列表"位置的字符序列是什么意思?就是这些示例中的$$$
,$$;$$
和$$@
序列.
Leaving aside the question of what these subroutines are meant to do (I'm not entirely sure myself...), what do the sequences of characters in the 'parameter list' position mean? Viz. the $$$
, $$;$$
and $$@
sequences in these examples.
我对Perl的了解非常有限,但是我相信第一个示例(sum
)中的my($a,$b,$m)=@_;
行将传递给子例程的参数解压缩为$a
,$b
和$m
局部变量.这表明$$$
表示sum
的Arity和类型签名(在这种情况下,它期望三个标量).这可能暗示gOut
需要两个标量和一个数组.这是正确的解释吗?
I have a very limited understanding of Perl, but I believe that the my($a,$b,$m)=@_;
line in the first example (sum
) unpacks the parameters passed to the subroutine into the $a
, $b
and $m
local variables. This suggests that the $$$
indicates the arity and type signature of sum
(it expects three scalars, in this case). This would potentially suggest that gOut
expects two scalars and an array. Is this the correct interpretation?
即使以上解释是正确的,我也对第二个例程(gNode
)中的分号的含义一无所知.
Even if the above interpretation is correct, I'm lost as to the meaning of the semicolon in the second routine (gNode
).
推荐答案
请参见上的 perldoc perlsub 条目>原型.
See perldoc perlsub entry on Prototypes.
# Declared as Called as
sub mylink ($$) mylink $old, $new
sub myvec ($$$) myvec $var, $offset, 1
sub myindex ($$;$) myindex &getstring, "substr"
sub mysyswrite ($$$;$) mysyswrite $buf, 0, length($buf) - $off, $off
sub myreverse (@) myreverse $a, $b, $c
sub myjoin ($@) myjoin ":", $a, $b, $c
sub mypop (+) mypop @array
sub mysplice (+$$@) mysplice @array, 0, 2, @pushme
sub mykeys (+) mykeys %{$hashref}
sub myopen (*;$) myopen HANDLE, $name
sub mypipe (**) mypipe READHANDLE, WRITEHANDLE
sub mygrep (&@) mygrep { /foo/ } $a, $b, $c
sub myrand (;$) myrand 42
sub mytime () mytime
别忘了: 当然,这一切都非常强大,应该适度使用,以使世界变得更美好.
Don't forget: This is all very powerful, of course, and should be used only in moderation to make the world a better place.
这篇关于Perl参数列表中的美元,符号和分号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!