In perl, $x = if (0) {1} else {2}
does not work.
$ perl -E'$x = if (0) {1} else {2}'
syntax error at -e line 1, near "= if"
Execution of -e aborted due to compilation errors.
This makes sense, because if
conditionals are not expressions in Perl. They're flow control.
But then
my $x = do { if (0) {1} else {2} };
Does work! How come a do BLOCK
can accept an if
conditional? But assignment can not? It would seem in the above the flow control must either
- know it's context in a
do BLOCK
- always act as an expression, but have that syntax disallowed by the parser.
Moreover given the simple facts above, what is the right way to describe an if-conditional that behaves like that? Is it an expression with a value? Is it flow-control construct that has no value after evaluation?
And, lastly, what modifications would have to be made to assignment to have it accept an if
condition like a do BLOCK
.