Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

exit() calls END{} blocks, but these blocks can exit()

by meonkeys (Chaplain)
on Jul 08, 2002 at 18:37 UTC ( [id://180265]=perlquestion: print w/replies, xml ) Need Help??

meonkeys has asked for the wisdom of the Perl Monks concerning the following question:

perldoc -f exit says:
The exit() function does not always exit immediately. It calls any defined "END" routines first, but these "END" routines may not themselves abort the exit.
But the following code seems to exit() out of the END{} block (I can tell because a status code of 1 is returned to the OS):
#!/usr/bin/perl -Tw use strict; print "I'm going to exit now.\n"; exit(255); sub END { print "In END block\n"; exit(1); }
Am I missing something simple?

As an aside: I never see exit() used. Is is best to avoid it because of the catchability of die()?

---
"A Jedi uses the Force for knowledge and defense, never for attack."

Replies are listed 'Best First'.
Re: exit() calls END{} blocks, but these blocks can exit()
by Anonymous Monk on Jul 08, 2002 at 18:54 UTC
    What exit does inside of an END block is set the exit value ($?) and exits the remainder of that END block, any remaining END blocks are still called in reverse order of definition.
    print "Hello World\n"; exit 0; print "Goodbye World\n"; END{ print "End 1\n"; } END{ print "End 2\n"; exit 1; print "End of End 2\n"; } END{ print "End 3\n"; } # output: Hello World End 3 End 2 End 1 # exit status: 256
      Thank you, this example explains a lot. From your example I get an exit status of 1, however. I'm checking the exit status like so:
      $ perl -Tw check_exit.plx; echo $?
      I don't know if it's relevant, but I'm using Linux.

      ---
      "A Jedi uses the Force for knowledge and defense, never for attack."
Re: exit() calls END{} blocks, but these blocks can exit()
by Zaxo (Archbishop) on Jul 08, 2002 at 19:04 UTC
    I never see exit() used. Is is best to avoid it because of the catchability of die()?

    It is commonly used in fork-ed child code, where the child must not go on to execute parent code. The alternative is exec.

    The ability to set exit status, as you do, is a good diagnostic tool. I'm not sure what you expected your code to do. The behavior seems correct and unsurprising to me. An exit call clearly never returns, so the innermost one gets to set the exit status.

    You've raised my curiosity, I never thought to check behavior of multiple END blocks under a construction like this.

    After Compline,
    Zaxo

Re: exit() calls END{} blocks, but these blocks can exit()
by Aristotle (Chancellor) on Jul 08, 2002 at 18:45 UTC
    When writing a library or module, it is indeed best to avoid exit, unless for when it actually makes sense. (The Mail::Audit module f.ex exits when you call a function that actually takes action with a mail.)

    Makeshifts last the longest.

      When writing a library or module, it is indeed best to avoid exit, unless for when it actually makes sense.
      Yes, I read this in the docs. But what about my other question? Why is the exit() in END() getting called?

      Update: (Wed Jul 10 22:09:21 PDT 2002)
      Silly me, the exit() in the END{} block is definitely not aborting the program, but is still setting $?, which is what I'd expect. This is clear after reading this.

      ---
      "A Jedi uses the Force for knowledge and defense, never for attack."

        Why is the exit() in END() getting called?

        Because it's there.

        - Yes, I reinvent wheels.
        - Spam: Visit eurotraQ.
        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-24 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found