Click here to get back home

FAQ 8.18 How can I do an atexit() or setjmp()/longjmp()? (Exception handling)

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
FAQ 8.18 How can I do an atexit() or setjmp()/longjmp()? (Exception handling) PerlFAQ Server 04-05-2008
Posted by PerlFAQ Server on April 5, 2008, 9:03 am
Please log in for more thread options
This is an excerpt from the latest version perlfaq8.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

8.18: How can I do an atexit() or setjmp()/longjmp()? (Exception handling)

Release 5 of Perl added the END block, which can be used to simulate
atexit(). Each package's END block is called when the program or thread
ends (see perlmod manpage for more details).

For example, you can use this to make sure your filter program managed
to finish its output without filling up the disk:

END {
close(STDOUT) || die "stdout close failed: $!";
}

The END block isn't called when untrapped signals kill the program,
though, so if you use END blocks you should also use

use sigtrap qw(die normal-signals);

Perl's exception-handling mechanism is its eval() operator. You can use
eval() as setjmp and die() as longjmp. For details of this, see the
section on signals, especially the time-out handler for a blocking
flock() in "Signals" in perlipc or the section on "Signals" in the Camel
Book.

If exception handling is all you're interested in, try the exceptions.pl
library (part of the standard perl distribution).

If you want the atexit() syntax (and an rmexit() as well), try the
AtExit module available from CPAN.



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

Similar ThreadsPosted
FAQ: How can I do an atexit() or setjmp()/longjmp()? (Exception handling) October 16, 2004, 11:01 pm
FAQ 8.18 How can I do an atexit() or setjmp()/longjmp()? (Exception handling) March 12, 2005, 12:03 am
FAQ 8.18 How can I do an atexit() or setjmp()/longjmp()? (Exception handling) May 22, 2005, 5:03 am
FAQ 8.18 How can I do an atexit() or setjmp()/longjmp()? (Exception handling) August 6, 2005, 10:03 pm
FAQ 8.18 How can I do an atexit() or setjmp()/longjmp()? (Exception handling) October 19, 2005, 4:03 pm
FAQ 8.18 How can I do an atexit() or setjmp()/longjmp()? (Exception handling) February 6, 2006, 12:03 pm
FAQ 8.18 How can I do an atexit() or setjmp()/longjmp()? (Exception handling) September 19, 2006, 9:03 pm
FAQ 8.18 How can I do an atexit() or setjmp()/longjmp()? (Exception handling) March 25, 2007, 3:03 am
FAQ 8.18 How can I do an atexit() or setjmp()/longjmp()? (Exception handling) June 8, 2007, 9:03 pm
FAQ 8.18 How can I do an atexit() or setjmp()/longjmp()? (Exception handling) August 17, 2007, 9:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap