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 perlipc/"Signals" 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.