in reply to Re: Difference between leftward and rightward list operators
in thread Difference between leftward and rightward list operators

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.

  • Comment on Re^2: Difference between leftward and rightward list operators