Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / B / Hooks / EndOfScope.pm
1 use strict;
2 use warnings;
3
4 package B::Hooks::EndOfScope;
5
6 use 5.008000;
7 use Variable::Magic;
8
9 our $VERSION = '0.08';
10
11 use Sub::Exporter -setup => {
12     exports => ['on_scope_end'],
13     groups  => { default => ['on_scope_end'] },
14 };
15
16 =head1 NAME
17
18 B::Hooks::EndOfScope - Execute code after a scope finished compilation
19
20 =head1 SYNOPSIS
21
22     on_scope_end { ... };
23
24 =head1 DESCRIPTION
25
26 This module allows you to execute code when perl finished compiling the
27 surrounding scope.
28
29 =head1 FUNCTIONS
30
31 =head2 on_scope_end
32
33     on_scope_end { ... };
34
35     on_scope_end $code;
36
37 Registers C<$code> to be executed after the surrounding scope has been
38 compiled.
39
40 This is exported by default. See L<Sub::Exporter> on how to customize it.
41
42 =cut
43
44 {
45     my $wiz = Variable::Magic::wizard
46         data => sub { [$_[1]] },
47         free => sub { $_->() for @{ $_[1] }; () };
48
49     sub on_scope_end (&) {
50         my $cb = shift;
51
52         $^H |= 0x020000;
53
54         if (my $stack = Variable::Magic::getdata %^H, $wiz) {
55             push @{ $stack }, $cb;
56         }
57         else {
58             Variable::Magic::cast %^H, $wiz, $cb;
59         }
60     }
61 }
62
63 =head1 SEE ALSO
64
65 L<Sub::Exporter>
66
67 L<Variable::Magic>
68
69 =head1 AUTHOR
70
71 Florian Ragwitz E<lt>rafl@debian.orgE<gt>
72
73 =head1 COPYRIGHT AND LICENSE
74
75 Copyright (c) 2008  Florian Ragwitz
76
77 This module is free software.
78
79 You may distribute this code under the same terms as Perl itself.
80
81 =cut
82
83 1;