autodie/Util.pm

filter: Statementstmt 98.7% 79 / 80 Branchbran 91.6% 22 / 24 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 95.9% 119 / 124 CC 17 SCAR 15.3
CC
17
Cov
96%
fill_protos
8.0
make_core_trampoline
5.0
install_subs
5.0
CRAP 4.6
« autodie/hints.pm ↑ Summary autodie.pm »
1 package autodie::Util;
2  
3 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 62 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 62 ▶use Exporter 5.57 qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 62 ▶use autodie::Scope::GuardStack;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 61 our @EXPORT_OK = qw(
11 fill_protos
12 install_subs
13 make_core_trampoline
14 on_end_of_compile_scope
15 );
16  
17 61 our $VERSION = '2.37'; # VERSION: Generated by DZP::OurPkg:Version
18  
19 # ABSTRACT: Internal Utility subroutines for autodie and Fatal
20  
21 # docs says we should pick __PACKAGE__ /<whatever>
22 61 my $H_STACK_KEY = __PACKAGE__ . '/stack';
23  
24 142 ▶sub on_end_of_compile_scope {
Subroutine on_end_of_compile_scope: called CC=2 SCAR=6.9
Pod documented
25 142 my ($hook) = @_;
26  
27 # Dark magic to have autodie work under 5.8
28 # Copied from namespace::clean, that copied it from
29 # autobox, that found it on an ancient scroll written
30 # in blood.
31  
32 # This magic bit causes %^H to be lexically scoped.
33 142 $^H |= 0x020000;
34  
35 142 my $stack = $^H{$H_STACK_KEY};
36 142 ▶ if (not defined($stack)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $stack) 112 30 142
37 112 $stack = autodie::Scope::GuardStack->new;
38 112 $^H{$H_STACK_KEY} = $stack;
39 }
40  
41 142 $stack->push_hook($hook);
42 142 return;
43 }
44  
45 # This code is based on code from the original Fatal. The "XXXX"
46 # remark is from the original code and its meaning is (sadly) unknown.
47 184 ▶sub fill_protos {
Subroutine fill_protos: called CC=8 SCAR=20.8
Pod documented
48 184 my ($proto) = @_;
49 184 my ($n, $isref, @out, @out1, $seen_semi) = -1;
50 184 ▶ if ($proto =~ m{^\s* (?: [;] \s*)? \@}x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto =~ /^\s* (?: [;] \s*)? \@/x) 62 122 184
51 # prototype is entirely slurply - special case that does not
52 # require any handling.
53 62 return ([0, '@_']);
54 }
55  
56 122 ▶ while ($proto =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto =~ /\S/) 394 75 469
57 394 $n++;
58 394 ▶ push(@out1,[$n,@out]) if $seen_semi;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_semi 117 277 394
59 394 ▶ push(@out, $1 . "{\$_[$n]}"), next if $proto =~ s/^\s*\\([\@%\$\&])//;
Branch: 1 branch
BranchTrueFalseTotal
if $proto =~ s/^\s*\\([\@%\$\&])// 13 381 394
60 381 ▶ push(@out, "\$_[$n]"), next if $proto =~ s/^\s*([_*\$&])//;
Branch: 1 branch
BranchTrueFalseTotal
if $proto =~ s/^\s*([_*\$&])// 266 115 381
61 115 ▶ push(@out, "\@_[$n..\$#_]"), last if $proto =~ s/^\s*(;\s*)?\@//;
Branch: 1 branch
BranchTrueFalseTotal
if $proto =~ s/^\s*(;\s*)?\@// 47 68 115
62 68 ▶ $seen_semi = 1, $n--, next if $proto =~ s/^\s*;//; # XXXX ????
Branch: 1 branch
BranchTrueFalseTotal
if $proto =~ s/^\s*;// 68 0 68
63 0 die "Internal error: Unknown prototype letters: \"$proto\"";
64 }
65 122 push(@out1,[$n+1,@out]);
66 122 return @out1;
67 }
68  
69  
70 4 ▶sub make_core_trampoline {
Subroutine make_core_trampoline: called CC=5 SCAR=16.1
Pod documented
71 4 my ($call, $pkg, $proto_str) = @_;
72 4 my $trampoline_code = 'sub {';
73 4 my $trampoline_sub;
74 4 my @protos = fill_protos($proto_str);
75  
76 4 foreach my $proto (@protos) {
77 8 local $" = ", "; # So @args is formatted correctly.
78 8 my ($count, @args) = @$proto;
79 8 ▶ if (@args && $args[-1] =~ m/[@#]_/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args and $args[-1] =~ /[\@#]_/) { } 3 5 8
Condition66%2/3
@args and $args[-1] =~ /[\@#]_/
!ll&&!rl&&r
053
MC/DC50%1/2
@args and $args[-1] =~ /[\@#]_/
@args $args[-1] =~ /[\@#]_/
Truth table66%2/3
A and B
A= @argsB= $args[-1] =~ /[\@#]_/
ABresult
0X0
100
111
80 3 $trampoline_code .= qq/
81 if (\@_ >= $count) {
82 return $call(@args);
83 }
84 /;
85 } else {
86 5 $trampoline_code .= qq<
87 if (\@_ == $count) {
88 return $call(@args);
89 }
90 >;
91 }
92 }
93  
94 4 $trampoline_code .= qq< require Carp; Carp::croak("Internal error in Fatal/autodie. Leak-guard failure"); } >;
95 4 my $E;
96  
97 {
98 4 local $@;
99 4 $trampoline_sub = eval "package $pkg;\n $trampoline_code"; ## no critic
100 4 $E = $@;
101 }
102 4 ▶ die "Internal error in Fatal/autodie: Leak-guard installation failure: $E"
Branch: 1 branch
BranchTrueFalseTotal
if $E 0 4 4
103 if $E;
104  
105 4 return $trampoline_sub;
106 }
107  
108 # The code here is originally lifted from namespace::clean,
109 # by Robert "phaylon" Sedlacek.
110 #
111 # It's been redesigned after feedback from ikegami on perlmonks.
112 # See http://perlmonks.org/?node_id=693338 . Ikegami rocks.
113 #
114 # Given a package, and hash of (subname => subref) pairs,
115 # we install the given subroutines into the package. If
116 # a subref is undef, the subroutine is removed. Otherwise
117 # it replaces any existing subs which were already there.
118  
119 286 ▶sub install_subs {
Subroutine install_subs: called CC=5 SCAR=16.1
Pod documented
120 286 my ($target_pkg, $subs_to_reinstate) = @_;
121  
122 286 my $pkg_sym = "${target_pkg}::";
123  
124 # It does not hurt to do this in a predictable order, and might help debugging.
125 286 foreach my $sub_name (sort keys(%{$subs_to_reinstate})) {
126  
127 # We will repeatedly mess with stuff that strict "refs" does
128 # not like. So lets just disable it once for this entire
129 # scope.
130 62 ▶ no strict qw(refs); ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
131  
132 6785 my $sub_ref = $subs_to_reinstate->{$sub_name};
133  
134 6785 my $full_path = ${pkg_sym}.${sub_name};
135 6785 my $oldglob = *$full_path;
136  
137 # Nuke the old glob.
138 6785 delete($pkg_sym->{$sub_name});
139  
140 # For some reason this local *alias = *$full_path triggers an
141 # "only used once" warning. Not entirely sure why, but at
142 # least it is easy to silence.
143 62 ▶ no warnings qw(once);
Subroutine BEGIN: called CC=1 SCAR=0.0
144 6785 local *alias = *$full_path;
145 62 ▶ use warnings qw(once);
Subroutine BEGIN: called CC=1 SCAR=0.0
146  
147 # Copy innocent bystanders back. Note that we lose
148 # formats; it seems that Perl versions up to 5.10.0
149 # have a bug which causes copying formats to end up in
150 # the scalar slot. Thanks to Ben Morrow for spotting this.
151  
152 6785 foreach my $slot (qw( SCALAR ARRAY HASH IO ) ) {
153 27140 ▶ next unless defined(*$oldglob{$slot});
Branch: 1 branch
BranchTrueFalseTotal
unless defined *$oldglob{$slot} 20349 6791 27140
154 6791 *alias = *$oldglob{$slot};
155 }
156  
157 6785 ▶ if ($sub_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub_ref) 3761 3024 6785
158 3761 *$full_path = $sub_ref;
159 }
160 }
161  
162 286 return;
163 }
164  
165 61 1;
166  
167 __END__
« autodie/hints.pm ↑ Summary autodie.pm »