Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

RE (tilly) 2: Populate $!

by tilly (Archbishop)
on Oct 11, 2000 at 22:39 UTC ( [id://36280]=note: print w/replies, xml ) Need Help??


in reply to RE: Populate $!
in thread Populate $!

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...

Replies are listed 'Best First'.
For Perl kids only!
by merlyn (Sage) on Oct 11, 2000 at 22:48 UTC
    I think you're digging yourself a velvet-lined grave here.

    Yes, for many Perl child programs, the trivial use of die in that child program propogates their errno to their exit code.

    But for the rest of the real world, no such propogation ever takes place. I think if you survey the exit status of nearly all other programs, you'll find a simple +1 or -1 (255) exit status, which your code will correctly misinterpret. {grin}

    So, feel free to post your code, but be sure the reader is clear that it is intended for Perl child processes only.

    -- Randal L. Schwartz, Perl hacker

      Excellent points.

      This thread should stand as warning to those thinking of using that snippet. In the example that brought this up I will clearly document that both in the function, and in the name. The new name will be guess_system_error.

      OTOH I want to use this for Perl children, so it is still useful for me.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://36280]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2025-07-12 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.