End(3pm)

End3pm

Generalized end {}.

Usage

    use End;
    {  my $foo = end {print "Leaving the block\n"};
       ...
       last;    # prints "Leaving the block\n".
       ...
    }

Overview

The module "End" exports a single subroutine "end", which allows you to set up some code that is run whenever the current block is exited, regardless whether that is due to a "return", "next", "last", "redo", "exit", "die", "goto" or just reaching the end of the block.

To be more precise, "end" returns an object, that will execute the code when the object is destroyed; that is, when the variable assigned to goes out of scope. If the variable is lexical to the current block, the code will be executed when leaving the block.

One can force premature execution of the code by undefining the variable assigned to, or assigning another value to the variable.

"end" only takes one argument, a code reference. If one wishes the code reference to take arguments, wrapping the code reference in a closure suffices.

Development

The current sources of this module are found on github, <git://github.com/Abigail/end.git>.

Releases

11 releases since Debian 3.1 (Sarge); the text changed 6 times.

  1. 3.1
  2. 4.0
  3. 5.0
  4. 6.0–7
  5. 8–10
  6. 11
  7. 12–13

Bugs

Due to a bug in Perl 5.6.0 (and perhaps before), anonymous subroutines that are not a closure will not go out of scope, not even on program termination. That is why "end" wraps the code fragment in a closure.

There is a second bug in Perl 5.6.0 (and perhaps before) why this is necessary. If the code fragment isn't wrapped in another code reference, the original subroutine will be blessed in the package, making that "ref" on that code no longer returns the right value.

Author

This package was written by Abigail, <mailto:[email protected]>

From libend-perl 2009110401-3 in Debian 13 (Trixie). Parsed by carta from the page's man source.