in reply to Precedence design question...'x' & arith
G'day perl-diddler,
To be honest, I got the impression that you made a coding error
$ perl -MO=Deparse,-p -e 'my $str = " " x $col%$maxcol;' (my $str = ((' ' x $col) % $maxcol)); -e syntax OK
and instead of simply fixing it with the addition of a pair of parentheses
$ perl -MO=Deparse,-p -e 'my $str = " " x ($col%$maxcol);' (my $str = (' ' x ($col % $maxcol))); -e syntax OK
you decided that Perl should change to accomodate you.
If you simply want a maximum amount of indentation, your logic (regardless of syntax) is wrong. When you see your output starting to zigzag across the screen, will you change your code to
$ perl -MO=Deparse,-p -e 'my $str = " " x $col > $maxcol ? $maxcol : $ +col;' (my $str = (((' ' x $col) > $maxcol) ? $maxcol : $col)); -e syntax OK
instead of
$ perl -MO=Deparse,-p -e 'my $str = " " x ($col > $maxcol ? $maxcol : +$col);' (my $str = (' ' x (($col > $maxcol) ? $maxcol : $col))); -e syntax OK
and then want the precedence of the ternary operator to change as well.
Now, let's say your indentation was based on tabs of four spaces and your check also needed to take a margin into consideration. This code would no longer work as expected under your new precedence rules:
$ perl -MO=Deparse,-p -e 'length " " x $expr + $margin' length(((' ' x $expr) + $margin)); -e syntax OK
"The 'x' operator isn't commutative"
So what? The '/' and '%' operators aren't commutative either: do you want to change their precedence as well based on commutativity?
"So it would only change in P20 or P22 if it were to change, ..."
By that, I assume you mean version 5.20 or 5.22. The 'x' operator has been around, with the same precedence, since at least Perl 4 (it's in my first edition Programming perl) — so, that's at least 22 years (see perlhist) of code that could potentially just break for anyone upgrading perl.
"To me, it looks like a prime situation for perl's precedence rules to change in a mostly backwards compatible way ..."
There is nothing in your post that suggests backwards compatibility. Perhaps you also want:
use feature 'diddler_precedence';
In closing, I see no merit whatsoever in what you're proposing. Just add the parentheses to your broken code and move on with your life.
-- Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Precedence design question...'x' & arith
by perl-diddler (Chaplain) on Jul 05, 2013 at 05:38 UTC |