note
pemungkah
For your construction, I'd recommend a chained ternary expression:
<code>
print "$_ is ",
$_ == 0 ? 'zero'
: $_ > 0 ? 'pos'
: 'neg',
"\n" for -1..1;
</code>
The ternary operator <tt>?:</tt> is essentially an <tt>if</tt> statement designed to return a value in a guaranteed-defined way.
Breaking this down:
<ul>
<li><tt> $_ == 0 ? 'zero' : ...</tt> does 'this is zero or something else'
<li><tt>$_ > 0 ? 'pos' : 'neg'</tt> does the 'something else': positive or negative
</ul>
I've inserted the line breaks in this particular way to make it easy to see how the chain of logical expressions is evaluated. This also makes it easy to see where to edit new tests in:
<code>
print "$_ is ",
$_ == 0 ? 'zero'
: $_ == 2 ? 'two'
: $_ == -1 ? 'minus one'
: $_ > 0 ? 'pos'
: 'neg',
"\n" for -2..2;
</code>
1069710
1069710