The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Filter::Cleanup - Execute cleanup statements when scope closes, regardless of errors

VERSION

version 0.03

SYNOPSIS

  use Filter::Cleanup;

  open my $fh, $file_path or die $!;
  cleanup { close $fh };
  do_risky_stuff_with_fh($fh);

DESCRIPTION

Defines a block of code to be evaluated once the current scope has completed execution. If an error is thrown during the execution of statements after the cleanup block, the error is trapped and rethrown after the cleanup block is executed.

NAME

Filter::Cleanup

CAVEATS

ORDERING OF CLEANUP BLOCKS

A cleanup block will execute ahead of any cleanups defined before it. That is, for a given scope, cleanup blocks will be called in the opposite order in which they were declared.

IMPLEMENTATION

This module was originally implemented as a source filter (hence the name), but now uses Keyword::Declare.

SEE ALSO

Guard

Guard is implemented in XS and attaches a code block to the actual stack frame, ensuring it is executed regardless of how the scope was exited. In many cases, this may be preferable to the behavior of Filter::Cleanup, which will not be executed if the block calls exit or goto (note that last is handled correctly).

AUTHOR

Jeff Ober sysread@fastmail.fm

LICENSE

Perl5

AUTHOR

Jeff Ober <sysread@fastmail.fm>

COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by Jeff Ober.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.