Note, this question borders on discussing how many perl users can dance inside an array of size undef (and where would they be when doing so? Undef space?)... Seriously, though, back to the Q where we have an indent char/string the repeat operator 'x' and our count that needs to be arrived at via some arithmetic operations.
Wouldn't this tend toward some expression like:
?' ' x $expr;
Under what circumstance would you want to evaluate some left-side of "$expr" and use it as an operand to the 'x' operator, and then multiply the result by the remaining right-side of the expression?
That's the main question. (um...why would someone care?)
It comes down to perl's precedence rules and a change that would allow perl to more often do something useful rather than than throwing an error and discarding what the user more than likely meant. Ex.
The above throws an error that I can see no useful purpose for. The 'x' operator isn't commutative -- you have to have the string on the left, and you have to have the count on the right. So why not interpret the arithmetic portion first, then use that as the count?use warnings; use strict; my $col=10; my $maxcol=40; my $str = ' ' x $col%$maxcol;
Right now perl precedence rules for 'x' and % and the one below it, look like
left * / % x left + - .
Can people see the above precedence being useful? Note, any suggested change wouldn't be retroactively applied to older versions of perl (that almost always goes without saying, but just to make it explicit). So it would only change in P20 or P22 if it were to change, but why the above shouldn't be changed to prioritize arithmetic operators over string operators in an expression. I came up with:
1) left * / % 2) left + - 3) left x 4) left .
Note 2 - alternatively, and 'technically', one might think to keep rule 3 above rule 2, but in the same spirit of the above example, I don't see getting
to be any more helpful thanArgument " " isn't numeric in addition (+) at -e line 2.
To me, it looks like a prime situation for perl's precedence rules to change in a mostly backwards compatible way that would allow for cleaner code in the future.Argument " " isn't numeric in modulus (%) at -e line 2.
I can think of at least 1 incompatibility: anyone who is maintaining a 15-20 y/o piece of code and feels they should be able to put "use <newest.version>;" at the top of their script, but complain that "'3'x 1+2". would no longer evaluate to '5', but instead to '333'. ;-/.
While the above might be useful in an obfuscated code contest, I'm not sure it really benefits the language's clarity or usefulness, whereas changing the precedence would allow for cleaner usage (as well as fewer errors).
By cleaner code, in this case, I'm referring to fewer required parantheses. In order to make the above, failing example 'work', you need parens around the arithmetic operators to have perl use that expression in a manner that would be useful with the string operator on the left. This change would give precedence to arithmetic operators over string operators when used in a mixed context, which might even have a[n almost immeasurable] benefit in execution speed.
Comments? Ideas? Any other similar ops besides "x" and "." that should be considered? Or do people find the current rules more intuitive and less error prone? If so, could you explain how? ;-)
Thanks for your insight!
In reply to Precedence design question...'x' & arith by perl-diddler
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |