Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Beware $? and END

by Felonious (Chaplain)
on Apr 27, 2002 at 16:51 UTC ( #162532=perlmeditation: print w/ replies, xml ) Need Help??

Recently, I was bitten by a subtle bug. I had a script that I needed to return a value from to indicate success or failure. Simply enough, where I wanted to fail, I used "exit 1". The problem was that my script always returned success. After alot of frustration, it finally hit me what the problem was...
sub END { system("./dosomething"); } exit 1;
exit() will set $? to the supplied value then eventually terminate with that value, but not until it executes the code in the END block. If anything in there sets $? (as system does), it will overwrite the value set by exit. The solution is to make $? local in the END block.
sub END { local $?; system("./dosomething"); }

-- O thievish Night, Why should'st thou, but for some felonious end, In thy dark lantern thus close up the stars? --Milton

Comment on Beware $? and END
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://162532]
Approved by ybiC
Front-paged by jptxs
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2015-07-06 17:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (77 votes), past polls