sub func { my $arg = shift; die 'Invalid Argument' if $arg > 10; return $arg - 2; } eval { $var = func($var) }; # No exception catching. print "$var.\n";
Another possibility might be to change the function to return two values: the result and success. Upon failure, the result would be the same as the input argument, but the success would be 0. Otherwise, the function would return the new result and 1:
sub func { my $arg = shift; return($arg, 0) if $arg > 10; return($arg - 2, 1); } my $var = 10; ($var, my $succ) = func($var); print "$var.\n";
Update: For numeric return values, you can just add a string to the number to indicate failure:
sub func { my $arg = shift; return $arg . 'E0' if $arg > 10; return $arg - 2; }
See Range Operators for a similar trick.
In reply to Re: Variable assignment error checking
by choroba
in thread Variable assignment error checking
by nathaniels
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |