I know that &
is an old syntax to call subroutines:
sub test { die 'xx' };
&{ "main::test" };
OUTPUT:
xx at main.pl line 1.
But why subroutine is not called when I use defined &{ "main::test" }
?
sub test { die 'xx' };
print defined &{ "main::test" };
OUTPUT:
1
CodePudding user response:
Documented in 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.
Under 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.
The parentheses after defined
are optional (similarly to those after a predeclared sub). See perlfunc for details.