开发者

apparent strangeness using subroutine with the multiplication operator

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 hel开发者_运维百科pful - 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜