Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

RE: Populate $!

by Fastolfe (Vicar)
on Oct 11, 2000 at 21:50 UTC ( [id://36271]=note: print w/replies, xml ) Need Help??


in reply to Populate $!

(Note that this is less for tilly's benefit than it is for any casual user that is trying to take advantage of this trick.) Unfortunately $! and system return values have nothing in common. You can (and must) never assume that an arbitrary program's return code would or should ever map to any standard error message (such as via $!). However, if you are making use of some external program that does define its return code behavior in this fashion (such as something you write specifically to this criteria), by all means give it a shot.

Setting the value of $! is also useful for system-"like" functions of your own design, but generally setting this variable is only ever useful when your system-like function is doing some core-level operation that itself fails, in which case $! should already be set. Just don't change it when you return.

Replies are listed 'Best First'.
RE (tilly) 2: Populate $!
by tilly (Archbishop) on Oct 11, 2000 at 22:39 UTC
    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...
      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://36271]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2025-07-17 21:35 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.