in reply to return from subfunction
You could make your "success" return value(s) dual-type (see dualvar) with the numeric part being zero, in which case you could simply test if the value is less than zero:
use strict; use warnings; use Scalar::Util 'dualvar'; for(my $ii = 1; $ii < 4; $ii++) { my $a = foo($ii); if($a < 0) # <-- { print "Fail occured with code: $a\n"; } else { print "Success\n"; } } sub foo { my $num = shift; if($num == 1) { return -1; } elsif($num == 2) { return -2; } else { return dualvar 0, "true\n"; # <-- } }
The main purpose of this is that the code would run cleanly under strictures, i.e. without eliciting the warning 'Argument "true\n" isn't numeric in numeric lt (<)'.
In other words, your original regular scalars would in principle work, too (a string which doesn't look like a number, evaluates to zero), but you'd have to say no warnings 'numeric'; — which ultimately could get even more unwieldy than what you have now, in case you try to restrict it to the minimal scope required every time...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: return from subfunction
by fidesachates (Monk) on Apr 07, 2011 at 19:35 UTC |