pas*_*636 8 syntax perl invocation subroutine
对于Perl子例程,如果传递0参数,我可以使用4个表单来调用它.但是如果传递一个或多个参数,有一个我不能使用的表单,请看下面:
sub name
{
print "hello\n";
}
# 4 forms to call
name;
&name;
name();
&name();
sub aname
{
print "@_\n";
}
aname "arg1", "arg2";
#&aname "arg1", "arg2"; # syntax error
aname("arg1", "arg2");
&aname("arg1", "arg2");
Run Code Online (Sandbox Code Playgroud)
错误输出是
String found where operator expected at tmp1.pl line 16, near "&aname "arg1""
(Missing operator before "arg1"?)
syntax error at tmp1.pl line 16, near "&aname "arg1""
Execution of tmp1.pl aborted due to compilation errors.
Run Code Online (Sandbox Code Playgroud)
有人可以从编译器的角度解释错误输出吗?我不明白为什么抱怨缺少操作员.
谢谢
cho*_*oba 13
它在perlsub中有记录:
要调用子例程:
Run Code Online (Sandbox Code Playgroud)NAME(LIST); # & is optional with parentheses. NAME LIST; # Parentheses optional if predeclared/imported. &NAME(LIST); # Circumvent prototypes. &NAME; # Makes current @_ visible to called subroutine.
有了&NAME "arg"
,perl看到了&NAME() "ARG"
,所以它认为在子呼叫和"ARG"之间缺少一个运营商.
在Perl 5中,&
大多数情况下不需要.