1End(3) User Contributed Perl Documentation End(3)
2
3
4
6 End - generalized END {}.
7
9 use End;
10
11 { my $foo = end {print "Leaving the block\n"};
12 ...
13 last; # prints "Leaving the block\n".
14 ...
15 }
16
18 The module "End" exports a single subroutine "end", which allows you to
19 set up some code that is run whenever the current block is exited,
20 regardless whether that is due to a "return", "next", "last", "redo",
21 "exit", "die", "goto" or just reaching the end of the block.
22
23 To be more precise, "end" returns an object, that will execute the code
24 when the object is destroyed; that is, when the variable assigned to
25 goes out of scope. If the variable is lexical to the current block, the
26 code will be executed when leaving the block.
27
28 One can force premature execution of the code by undefining the
29 variable assigned to, or assigning another value to the variable.
30
31 "end" only takes one argument, a code reference. If one wishes the code
32 reference to take arguments, wrapping the code reference in a closure
33 suffices.
34
36 Due to a bug in Perl 5.6.0 (and perhaps before), anonymous subroutines
37 that are not a closure will not go out of scope, not even on program
38 termination. That is why "end" wraps the code fragment in a closure.
39
40 There is a second bug in Perl 5.6.0 (and perhaps before) why this is
41 necessary. If the code fragment isn't wrapped in another code
42 reference, the original subroutine will be blessed in the package,
43 making that "ref" on that code no longer returns the right value.
44
46 The current sources of this module are found on github,
47 <git://github.com/Abigail/end.git>.
48
50 This package was written by Abigail, <mailto:cpan@abigail.be>
51
53 Copyright (C) 2000 - 2009, Abigail
54
55 Permission is hereby granted, free of charge, to any person obtaining a
56 copy of this software and associated documentation files (the
57 "Software"), to deal in the Software without restriction, including
58 without limitation the rights to use, copy, modify, merge, publish,
59 distribute, sublicense, and/or sell copies of the Software, and to
60 permit persons to whom the Software is furnished to do so, subject to
61 the following conditions:
62
63 The above copyright notice and this permission notice shall be included
64 in all copies or substantial portions of the Software.
65
66 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
67 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
68 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
69 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
70 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
71 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
72 DEALINGS IN THE SOFTWARE.
73
74
75
76perl v5.36.0 2022-07-22 End(3)