Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Surprised by Perl parse of ternary operator

by moritz (Cardinal)
on Dec 16, 2011 at 12:50 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2024-04-23 12:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found