Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re^2: Surprised by Perl parse of ternary operator

by moritz (Cardinal)
on Dec 16, 2011 at 12:50 UTC ( #943937=note: print w/replies, xml ) Need Help??

in reply to Re: Surprised by Perl parse of ternary operator
in thread Surprised by Perl parse of ternary operator

It is indeed a bit weird since ?: has higher precedence than =

If it were any different, the useful idiom

my $x = $condition ? $a : $b;

would be parsed as

(my $x = $condition) ? $a : $b

which totally wouldn't make sense. It's useful to optimize the operator precedence level for idiomatic use, not for misunderstood corner cases.

That said, it is possible to both allow the common case and detect the error; that's what Perl 6 does:

# program (note that Perl 6 uses ??!! instead of ?:) 1 ?? $_ = 3 !! $_ = 5 # output from the Perl 6 standard grammar: Precedence of = is too loose to use between ?? and !!; please use pare +ns around inner expression at ...

But I guess Perl 5 won't do it soon, because it'll break some obfuscations that successfully "use" that feature somewhere.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://943937]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2016-10-21 01:56 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (284 votes). Check out past polls.