in reply to Perl disproves commutativity of Addition/Multiplication

But that is clearly not so - *including* the parentheses in the above script alters the output (and restores commutativity):

Of course it alters the output. You are allowed to use predeclared subroutines without parenthesis, but the docs don't say that you always get the same result as with parens -- in the case without parens, you need to be aware of the precedence of your expressions.

In particular non_commutative + 10 parses as non_commutative( +10 ). You meant non_commutative() + 10, but perl can't know that; you'd have to write (non_commutative) + 10 to disambiguate.

In case you wonder what the *10 is: as a term that's the glob syntax that you might know from *STDOUT.

The precedence of subroutines without parenthesis is described in perlop as "list operators (rightward)", and looser than + and *.

Another option is to declare the sub with a prototoype:

sub non_commutative() { 17 }

Which is described in perlsub.

Replies are listed 'Best First'.
Re^2: Perl disproves commutativity of Addition/Multiplication
by syphilis (Archbishop) on Jan 23, 2012 at 22:19 UTC
    You are allowed to use predeclared subroutines without parenthesis, but the docs don't say that you always get the same result as with parens

    You're right ... being "allowed" to do something doesn't mean anything more than it "won't produce a fatal error". (Got suckered by that one ;-)

    Not that I'm interested in pursuing the matter, but I think the documentation *could* have taken the extra step of warning that removing the parens could cause unexpected results. I see other documentation that takes that extra step, and I (fallaciously) start to believe that if the docs don't mention any traps then there aren't any.

    Thanks to all respondents.

    Cheers,
    Rob

      Then by all means, submit a patch to p5p that improves the documentation.

      In my experience, the Perl 5 hackers are very open to doc improvements, and haven't rejected any of my doc patches. You really should try it.