请解释为什么这些 Perl 函数的调用方式在函数定义之上决定了它们是否运行。
print "Why does this bare call to foo not run?\n";
foo;
print "When this call to foo() does run?\n";
foo();
print "And this call to &foo also runs?\n";
&foo;
sub foo {
print " print from inside function foo:\n";
}
print "And this bare call to foo below the function definition, does run?\n";
foo;
最佳答案
如果解析器知道所讨论的标识符指的是函数,则只能在函数调用中省略括号。
您的第一个 foo;
不是函数调用,因为解析器还没有看到 sub foo
(并且 foo
不是内置函数).
如果你使用了use strict;使用警告;
在顶部,这将被标记为错误。
关于perl - 为什么可以在sub foo 声明之前调用foo() 和&foo,而不能调用plain foo?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39531045/