File Coverage

blib/lib/B/Hooks/AtRuntime/OnlyCoreDependencies.pm
Criterion Covered Total %
statement 57 71 80.2
branch 8 14 57.1
condition 1 5 20.0
subroutine 16 19 84.2
pod 0 7 0.0
total 82 116 70.6


line stmt bran cond sub pod time code
1 2     2   393892 use 5.008003;
  2         10  
2 2     2   16 use strict;
  2         5  
  2         152  
3 2     2   14 use warnings;
  2         6  
  2         210  
4              
5             package B::Hooks::AtRuntime::OnlyCoreDependencies;
6              
7 2     2   1316 use Sub::Util 'set_subname'; # not Sub::Name
  2         780  
  2         178  
8 2     2   21 use XSLoader;
  2         4  
  2         185  
9              
10             our @EXPORT = qw/ at_runtime /;
11             our @EXPORT_OK = qw/ at_runtime after_runtime lex_stuff /;
12              
13             # Don't depend on Exporter::Tiny; try to handle exporting
14             # internally. If @_ seems too complex, load Exporter or
15             # Exporter::Tiny, whichever seems to be necessary.
16             sub import {
17 2 50 0 2   21 if ( @_ == 1 ) {
    0          
18 2         5 my $caller = caller;
19 2     2   15 no strict 'refs';
  2         4  
  2         364  
20 2         5 *{"$caller\::at_runtime"} = \&at_runtime;
  2         13  
21 2         187 return;
22             }
23             elsif ( grep ref||/^-/, @_ ) {
24 0         0 require Exporter::Tiny;
25 0         0 our @ISA = qw/ Exporter::Tiny /;
26 2     2   16 no warnings 'redefine';
  2         7  
  2         569  
27 0         0 *import = \&Exporter::Tiny::import;
28 0         0 *unimport = \&Exporter::Tiny::unimport;
29 0         0 goto \&Exporter::Tiny::import;
30             }
31 0         0 require Exporter;
32 0         0 goto \&Exporter::import;
33             }
34              
35             # Delay loading Carp too.
36 0     0 0 0 sub croak { require Carp; goto \&Carp::croak; }
  0         0  
37 0     0 0 0 sub carp { require Carp; goto \&Carp::carp; }
  0         0  
38              
39             BEGIN {
40 2     2   9 our $AUTHORITY = 'cpan:TOBYINK';
41 2         14 our $VERSION = '8.000001';
42 2         1805 __PACKAGE__->XSLoader::load( $VERSION );
43             }
44              
45             # Also let's not load constant.pm.
46             BEGIN {
47             my $USE_FILTER =
48             defined $ENV{PERL_B_HOOKS_ATRUNTIME}
49 2 50   2   38 ? $ENV{PERL_B_HOOKS_ATRUNTIME} eq "filter"
50             : not defined &lex_stuff;
51 2 50       134 *USE_FILTER = $USE_FILTER ? sub(){!!1} : sub(){!!0};
52             };
53              
54             if (USE_FILTER) {
55             require Filter::Util::Call;
56              
57 2     2   16 no warnings "redefine";
  2         13  
  2         767  
58             *lex_stuff = set_subname "lex_stuff", sub {
59             my ($str) = @_;
60              
61             compiling_string_eval() and croak
62             "Can't stuff into a string eval";
63              
64             if (defined(my $extra = remaining_text())) {
65             $extra =~ s/\n+\z//;
66             carp "Extra text '$extra' after call to lex_stuff";
67             }
68              
69             Filter::Util::Call::filter_add(sub {
70             $_ = $str;
71             Filter::Util::Call::filter_del();
72             return 1;
73             });
74             };
75             }
76              
77             my @Hooks;
78              
79             sub replace_hooks {
80 26     26 0 55 my ($new) = @_;
81              
82 26         58 delete $B::Hooks::AtRuntime::OnlyCoreDependencies::{hooks};
83              
84 2     2   17 no strict "refs";
  2         4  
  2         1082  
85 26 100       3416 $new and *{"hooks"} = $new;
  14         98  
86             }
87              
88             sub clear {
89 13     13 0 124 my ($depth) = @_;
90 13         28 $Hooks[$depth] = undef;
91 13         39 replace_hooks $Hooks[$depth - 1];
92             }
93              
94             sub find_hooks {
95 16   50 16 0 52 my $func = shift || 'at_runtime';
96              
97 16         29 USE_FILTER and compiling_string_eval()
98             and croak "Can't use $func from a string eval";
99              
100 16 50       73 my $depth = count_BEGINs()
101             or croak "You must call $func at compile time";
102              
103 16         27 my $hk;
104 16 100       46 unless ($hk = $Hooks[$depth]) {
105 13         24 my @hooks;
106 13         33 $hk = $Hooks[$depth] = \@hooks;
107 13         35 replace_hooks $hk;
108 13         29 my $pkg = __PACKAGE__;
109 13         58 lex_stuff("${pkg}::run(\@${pkg}::hooks);BEGIN{${pkg}::clear($depth)}");
110             }
111              
112 16         39 return $hk;
113             }
114              
115             sub at_runtime (&) {
116 16     16 0 679 my ($cv) = @_;
117 16         39 my $hk = find_hooks('at_runtime');
118 16         813 push @$hk, set_subname scalar(caller) . "::(at_runtime)", $cv;
119             }
120              
121             sub after_runtime (&) {
122 0     0 0   my ($cv) = @_;
123 0           my $hk = find_hooks('after_runtime');
124 0           push @$hk, \set_subname scalar(caller) . "::(after_runtime)", $cv;
125             }
126              
127             1;
128              
129             __END__