I was writing an expression to repeat spaces 'N' number of times but to prevent any cases where my indentation had grown "alot", I didn't want to indent any more than half-the screen with... thus, probably a common idea -- modulus the indent amount by the a limit -- I chose half the screen width.

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.

use warnings; use strict; my $col=10; my $maxcol=40; my $str = ' ' x $col%$maxcol;
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?

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

Argument " " isn't numeric in addition (+) at -e line 2.
to be any more helpful than
Argument " " isn't numeric in modulus (%) 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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.