rsFalse has asked for the wisdom of the Perl Monks concerning the following question:
OUTPUT:perl -le '$_ = "a"; print do { if( length ){ "A" } else{ "B" } };'
There is with "ternary":A
OUTPUT:perl -le '$_ = "a"; print length ? "A" : "B" '
I forgot that 'length' is unary operator with higher precedence, so it interpret following '?' as matching parens. Same if I would write: 'print length // "A" ' - error, when 'print length || "A" ' - is ok.Warning: Use of "length" without parentheses is ambiguous at -e line 1 +. Use of ?PATTERN? without explicit operator is deprecated at -e line 1. Search pattern not terminated or ternary operator parsed as search pat +tern at -e line 1.
But it still outputs:perl -le '$_ = "a"; print length . '' ? "A" : "B" '
Concatenation point has higher precedence than unary 'length' so I think 'length' should give its value which will be concatenated with empty line, and later ternaty op., and later - 'print'.Use of ?PATTERN? without explicit operator is deprecated at -e line 1. Search pattern not terminated or ternary operator parsed as search pat +tern at -e line 1.
perl -le '$_ = "a"; print length + 0 ? "A" : "B" '
Warning: Use of "length" without parentheses is ambiguous at -e line 1 +. A
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: parsing 'length ...'
by Athanasius (Archbishop) on Feb 24, 2016 at 15:38 UTC | |
|
Re: parsing 'length ...'
by Anonymous Monk on Feb 24, 2016 at 16:19 UTC | |
|
Re: parsing 'length ...'
by kcott (Archbishop) on Feb 25, 2016 at 06:17 UTC |