在列表中
结构bareword (LIST1), LIST2 表示“将函数bareword 应用于参数LIST1”,而bareword +(LIST1), LIST2 可以,但不一定意味着“将bareword 应用于组合列表@987654327 的参数@"。这对于分组参数很重要:
my ($a, $b, $c) = (0..2);
print ($a or $b), $c; # print $b
print +($a or $b), $c; # print $b, $c
前缀+也可以用来区分hashrefs和blocks,以及functions和barewords,例如下标哈希时:$hash{shift}返回shift元素,而$hash{+shift}调用函数shift并返回shift值的哈希元素。
间接语法
在面向对象的 Perl 中,您通常使用箭头语法调用对象上的方法:
$object->method(LIST); # call `method` on `$object` with args `LIST`.
但是,不建议使用将动词放在首位的间接表示法:
method $object (LIST); # the same, but stupid.
因为类只是它们自身的实例(在句法意义上),你也可以在它们上调用方法。这就是为什么
new Class (ARGS); # bad style, but pretty
与
相同
Class->new(ARGS); # good style, but ugly
但是,这有时会混淆解析器,因此不建议使用间接样式。
但它确实暗示了 print 的作用:
print $fh ARGS
与
相同
$fh->print(ARGS)
确实,文件句柄$fh 被视为IO::Handle 类的对象。
(虽然这是一个有效的句法解释,但它并不完全正确。IO::Handle 的源代码本身使用print $this @_; 行。打印函数就是这样定义的。)