in reply to Re^4: printing unitialized value of the 'do BLOCK'
in thread printing unitialized value of the 'do BLOCK'

Nothing ever wants the value of an if statement.

A plain if? Probably not. An if/elsif/else statement though?

my @classifications = map { if ($_ > 0) { 'positive' } elsif ($_ < 0) { 'negative' } else { 'zero' } } @numbers;

Yes, you can write that using the ternary operator, but if/elsif/else can sometimes be clearer.

Replies are listed 'Best First'.
Re^6: printing unitialized value of the 'do BLOCK'
by haukex (Archbishop) on Dec 17, 2019 at 21:42 UTC

    Yes, this is exactly the kind of code that is the reason for my wish to have this behavior of if more clearly defined! Of course, given as a topicalizer would be even cooler, since for ($x) { if (/a/) { "A" } ... } doesn't work for this purpose...

Re^6: printing unitialized value of the 'do BLOCK'
by ikegami (Patriarch) on Dec 18, 2019 at 19:09 UTC

    Nothing ever wants the value of an if statement, even if it has an elsif or else clause. In your example, map is not getting the value of the if statement --there's no such thing-- it's getting the value of the last expression evaluated.