in reply to return value of "if" (documentation?)
For your construction, I'd recommend a chained ternary expression:
The ternary operator ?: is essentially an if statement designed to return a value in a guaranteed-defined way. Breaking this down:print "$_ is ", $_ == 0 ? 'zero' : $_ > 0 ? 'pos' : 'neg', "\n" for -1..1;
- $_ == 0 ? 'zero' : ... does 'this is zero or something else'
- $_ > 0 ? 'pos' : 'neg' does the 'something else': positive or negative
print "$_ is ", $_ == 0 ? 'zero' : $_ == 2 ? 'two' : $_ == -1 ? 'minus one' : $_ > 0 ? 'pos' : 'neg', "\n" for -2..2;
|
---|
In Section
Seekers of Perl Wisdom