man End () - generalized END {}.
NAME
End - generalized END {}.
SYNOPSIS
use End;
{ my $foo = end {print "Leaving the block\n"}; ... last; # prints "Leaving the block\n". ... }
DESCRIPTION
The module CWEnd exports a single subroutine CWend, which allows you to set up some code that is run whenever the current block is exited, regardless whether that is due to a CWreturn, CWnext, CWlast, CWredo, CWexit, CWdie, CWgoto or just reaching the end of the block.
To be more precise, CWend 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.
CWend only takes one argument, a code reference. If one wishes the code reference to take arguments, wrapping the code reference in a closure suffices.
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 CWend 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 CWref on that code no longer returns the right value.
REVISION HISTORY
$Log: End.pm,v $ Revision 1.2 2000/05/31 20:25:33 abigail Added the license paragraph in the POD section.
Revision 1.1 2000/05/31 19:35:01 abigail Initial revision
AUTHOR
This package was written by Abigail, abigail@delanet.com.
COPYRIGHT & LICENSE
This program is copyright 2000 by Abigail.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.