Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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.


Comment on Re^2: Surprised by Perl parse of ternary operator
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2014-08-30 13:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls