Next | Tricks of the Wizards | 129 |
Most folks know about the ?: operator
*{$p . '::' . $n} = (ref $v ? $v : \$v);
It's a compact version of an if-else block
What if you want a compact version of an if-elsif-else block?
sub sign { my $x = shift; if ($x < 0) { return -1 } elsif ($x == 0) { return 0 } else { return +1 } }
No problem:
sub sign { $_[0] < 0 ? -1 : $_[0] == 0 ? 0 : 1 ; }
Everything is as it should be
The precedence is fine, the short-circuiting is fine
The folks who designed the ?: operator are very smart
So chain together as many as you want
Next | Copyright © 2003 M. J. Dominus |