in reply to Perl disproves commutativity of Addition/Multiplication

The subs pragma is a red herring. Remove it, and the problem still holds.

The explanation is in the constant pragma, and some book I cannot remember explains that you should use () for constant-style sub calls because non_commutative * 10 parses as non_commutative(*10).

Replies are listed 'Best First'.
Re^2: Perl disproves commutativity of Addition/Multiplication
by syphilis (Archbishop) on Jan 23, 2012 at 23:41 UTC
    The subs pragma is a red herring. Remove it, and the problem still holds.

    Quite so.
    If I'm not mistaken, moving the definition of the non_commutative sub to the end of the script *does* create a purpose for loading the 'subs' pragma iff we want to avoid the use of parens - and it would have been better if the scripts I had presented did precisely that:
    use warnings; use subs qw(non_commutative); #necessary $x = 10 * non_commutative; $y = non_commutative * 10; print "$x\n$y\n\n"; $x = 10 + non_commutative; $y = non_commutative + 10; print "$x\n$y\n"; sub non_commutative {return 17} __END__ Outputs: 170 17 27 17
    and
    use warnings; use subs qw(non_commutative); #unnecessary $x = 10 * non_commutative(); $y = non_commutative() * 10; print "$x\n$y\n\n"; $x = 10 + non_commutative(); $y = non_commutative() + 10; print "$x\n$y\n"; sub non_commutative {return 17} __END__ Outputs: 170 170 27 27
    Cheers,
    Rob