in reply to style for returning errors from subroutines
Here, the OS error is shuffled into the $! global, which is specifically there to hold perl-level errors.open FILE, ">file.txt" or die $!;
Again, while not storing the error in a global, it does store it in a package-level variable, and that value is only changed when there is an error. You still get back useful returns from the call (an sth object or undef).my $sth = $dbi->prepare("SELECT * FROM TABLE") or die DBi->errstr;
So I'd say the best way to do it is to make sure you use packages and for each one, create a package-level variable that is to store any error messages. Then your functions should return any defined value if no error occured, or undef if one did, then you can use the typcal $var=function() or die $msg pattern above. The only gotcha to all this is if 0 or '' is a valid return from a function in addition to undef, in which case you might have to handle the checking via defined($var=function()) or die $msg.
|
|---|