Please could you explain this *apparently* inconsistent behaviour to me:
use strict;
sub a { 2 + 2 };
print 2 * a(); # this prints: 8
print a() * 2; # this prints: 8
print 2 * a; # this prints: 8
print a * 2; # this prints: 4
Thanks for answers, both very helpful - I learned a lot.
Deparse reveals that you are passing a glob to a in the last one:
$ perl -MO=Deparse,-p
use strict;
sub a { 2 + 2 };
print 2 * a(); # this prints: 8
print a() * 2; # this prints: 8
print 2 * a; # this prints: 8
print a * 2; # this prints: 4
__END__
sub a {
use strict 'refs';
4;
}
use strict 'refs';
print((2 * a()));
print((a() * 2));
print((2 * a()));
print(a(*2));
Using parens on your subroutine calls is a good thing...
In the last example, the expression is parsed as a(*2)
which calls a
with the glob argument *2
which is the short name of the package variable *main::2
If you want a
to be parsed as a function that takes no arguments, you need to declare it like this:
sub a () {2 + 2}
Then perl will parse the statement as you expected. In fact, if you write it like this, perl will detect that it is a constant function, and will inline 4
in every place where a
would have been called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With