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.
In reply to Re^2: Surprised by Perl parse of ternary operator
by moritz
in thread Surprised by Perl parse of ternary operator
by eyepopslikeamosquito
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |