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.
In the last example, the expression is parsed as
a(*2)which callsawith the glob argument*2which is the short name of the package variable*main::2If you want
ato be parsed as a function that takes no arguments, you need to declare it like this: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
4in every place whereawould have been called.