This also works if you need a return value, making things a very little bit more complicated:sub add_user { if (condition) { return 'Oops'; } else { return 'Argh'; } return undef; } my $Result = &add_user; if ($Result) { print "Something failed: $Result\n"; }
Here is the multilanguage style which is also good whenever you want a program to parse your result (parsing text error messages is ugly):sub add_user { if (condition) { return 0,'Oops'; } else { return 0,'Argh'; } return 1; } my ($Result, $ErrorText) = &add_user; if ( ! $Result) { print "Something failed: $Error_Text\n"; }
Finally, there is an OOP approch where you define a package and bless it on a hash where the error message and other things could be stored, but this is way too big for your problem...sub add_user { if (condition) { return 1; } else { return 2; } return 0; } my $Result = &add_user; if ($Result) { print "Something failed: ".$Texts{'Error_'.$Result}."\n"; }
In reply to Re: Dealing with errors in subroutines
by Sewi
in thread Dealing with errors in subroutines
by walkingthecow
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |