Absolutely. In fact after some playing around with it, I came up with the following massive improvement:
# Turns numerical return codes into text. See perlvar.
sub get_system_error {
my $code = shift;
print "Code: $code\n";
unless ($code < 2**8) {
$code >>= 8;
}
local $! = $code;
return "$!";
}
The win here is that you are automagically correcting the return codes that you get from wait or close in case the error is being passed through from an external program.
And yes, this only makes sense if you are careful to preserve the return codes. However a lot of Perl code uses die, which tries to do that by default, so you have a shot. This applies if you are trying to figure out why something exited (note that the STDERR of that program is probably gone) so an educated guess for the problem is better than nothing. Not perfect, but pass *it* out in your log message, pointing out that this is a possible guess, and it will often be helpful. So the message would be something like this:
if (0 < $?) {
my $sys_msg = get_system_error($?);
die "'$cmd' failed. ret code $?. (Guess: '$sys_msg'?)";
}
You don't lose any information, but also try to make it easy for a mere human to understand you... |