为什么没有调用 `&{ "main::test" }`?
Why `&{ "main::test" }` was not called?
我知道 &
是调用子程序的旧语法:
sub test { die 'xx' };
&{ "main::test" };
OUTPUT:
xx at main.pl line 1.
但是为什么我用defined &{ "main::test" }
没有调用子程序?
sub test { die 'xx' };
print defined &{ "main::test" };
OUTPUT:
1
记录在 perlsub 中:
A subroutine may be called using an explicit &
prefix. The &
is
optional in modern Perl, as are parentheses if the subroutine has been
predeclared. The &
is not optional when just naming the subroutine, such
as when it's used as an argument to defined() or undef(). Nor is it
optional when you want to do an indirect subroutine call with a subroutine
name or reference using the &$subref()
or &{$subref}()
constructs,
although the $subref->()
notation solves that problem. See perlref for
more about all that.
在defined, you can find (and similarly under undef and exists下):
You may also use defined(&func)
to check whether subroutine func has ever been defined.
defined
之后的括号是可选的(类似于预声明的 sub 之后的括号)。有关详细信息,请参阅 perlfunc。
我知道 &
是调用子程序的旧语法:
sub test { die 'xx' };
&{ "main::test" };
OUTPUT:
xx at main.pl line 1.
但是为什么我用defined &{ "main::test" }
没有调用子程序?
sub test { die 'xx' };
print defined &{ "main::test" };
OUTPUT:
1
记录在 perlsub 中:
A subroutine may be called using an explicit
&
prefix. The&
is optional in modern Perl, as are parentheses if the subroutine has been predeclared. The&
is not optional when just naming the subroutine, such as when it's used as an argument to defined() or undef(). Nor is it optional when you want to do an indirect subroutine call with a subroutine name or reference using the&$subref()
or&{$subref}()
constructs, although the$subref->()
notation solves that problem. See perlref for more about all that.
在defined, you can find (and similarly under undef and exists下):
You may also use
defined(&func)
to check whether subroutine func has ever been defined.
defined
之后的括号是可选的(类似于预声明的 sub 之后的括号)。有关详细信息,请参阅 perlfunc。