For me, the traditional terms of leftward and rightward precedence always obscured more than they explained. On reflection, it's actually the "leftward" precedence that introduces the confusion.

Operator precedence is just syntax sugar, right? Every expression can be notated without it, as long as every operator gets a pair of parentheses for its argument list. Precedence is just a convention for allowing some of the parentheses to go un-notated.

When we look at an expression like $a + $b ** $c * $d we can imagine those parentheses being emitted from the operator's position and riding outward until some condition requires them to stop. Specifically, each operator's parentheses skate over operators of higher precedence, but stop before operators of lower precedence, or at the edges of the expression.

This is exactly what's going on with the "rightward" precedence of the list operators: The implied right parenthesis continues rightward, scooping things into the operator's argument list, until it is stopped by the right edge of the expression or by one of the low-precedence boolean operators.

But the assertion of their "leftward" precedence is superfluous. It contrives to say that the implied left parenthesis can't pull in any arguments from the left, but we already know that, because the list operators are prefix.

We might just as well assert that the prefix unary operators also have maximal "leftward" precedence -- well of course they do, how could they not? perlop doesn't do this, it just lists them at what we might call their "rightward" precedence.

(Well, ++ and -- appear to have a non-maximal "leftward" precedence, but that's solely because each of them represents a postfix operator as well as a prefix one.)

I owe a bunch of this thinking to Jeffrey Kegler's blog post, "Do list operators have left/right precedence?" I've given the concept of rightward precedence more credit than he did in that post, but only because I had the benefit of reading his post and turning it over in my head for over a decade. Until I started writing this, I'd have affirmed what he wrote, that the concept is just incoherent.

I also haven't made as much hay out of the "grouping operator" description, because the whole point of any operator precedence is to make operators sometimes group their arguments together without having to spell that out in parentheses; but again, that's with benefit of hindsight.


In reply to Re^2: Difference between leftward and rightward list operators by eritain
in thread Difference between leftward and rightward list operators by Firsov

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.