blib/lib/Lexical/Failure.pm

filter: Statementstmt 97.1% 134 / 138 Branchbran 62.9% 39 / 62 Conditioncond 40.0% 6 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 25 / 25 Podpod 100% 2 / 2 totaltotal 81.7% 206 / 252 CC 38 SCAR 22.2
CC
38
Cov
83%
_process_import_args
23.2
_uplevel_die
8.5
_find_callers_handler
7.1
CRAP 9.2
↑ Summary blib/lib/Lexical/Failure/Objects.pm »
1 package Lexical::Failure;
2  
3 9 ▶use 5.014; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶no if $] >= 5.018, 'warnings', "experimental";
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Scope::Upper qw< want_at unwind uplevel UP SUB CALLER >;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Carp qw< carp croak confess cluck >;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Keyword::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 ▶use Lexical::Failure::Objects;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 our $VERSION = '0.000007';
12  
13 # Be invisible to Carp...
14 9 our @CARP_NOT = __PACKAGE__;
15  
16 # Lexical hints are always at index 10 of caller()...
17 9 my $HINTS = 10;
18  
19 # How to fail...
20 my %STD_FAILURE_HANDLERS = (
21 2 ▶ 'die' => sub { _uplevel_die(@_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 6 ▶ 'croak' => sub { uplevel { croak(@_) } @_, CALLER(2); },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 2 ▶ 'confess' => sub { uplevel { confess(@_) } @_, CALLER(2); },
Subroutine __ANON__: called CC=1 SCAR=0.0
24 4 ▶ 'null' => sub { return; },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 4 ▶ 'undef' => sub { return undef; },
Subroutine __ANON__: called CC=1 SCAR=0.0
26  
27 'failobj' => sub {
28 4 ▶ uplevel { croak(@_) } @_, CALLER(2) if !defined wantarray;
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 1 4 5
29 4 ▶ return Lexical::Failure::Objects->new(
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 4 0 4
30 msg => (@_==1 ? $_[0] : "@_"),
31 context => [caller 2],
32 );
33 },
34 9 );
35  
36 # Track handlers for lexical installations of fail()
37 9 my @ACTIVE_FAILURE_HANDLER_FOR_SCOPE;
38 my @VALID_FAILURE_HANDLERS_FOR_SCOPE;
39  
40 # # 'croak' is the universal default failure handler...
41 9 my $DEF_NAMED_HANDLER = 'croak';
42 9 my $DEFAULT_SCOPE_ID = 0;
43 9 $ACTIVE_FAILURE_HANDLER_FOR_SCOPE[$DEFAULT_SCOPE_ID] = $STD_FAILURE_HANDLERS{$DEF_NAMED_HANDLER};
44  
45 # Load the module...
46 sub import {
47 9 ▶ my ($fail, $ON_FAILURE, $default, $handlers) = _process_import_args(@_);
Subroutine import: called CC=3 SCAR=11.0
48  
49 # Export API...
50 9 Keyword::Simple::define $ON_FAILURE, _replace_keyword_with('Lexical::Failure::ON_FAILURE');
51 9 Keyword::Simple::define $fail, _replace_keyword_with('Lexical::Failure::fail');
52  
53 # Install specified failure handlers for the caller's scope...
54 9 my $handlers_scope_ID = scalar @VALID_FAILURE_HANDLERS_FOR_SCOPE;
55 9 $^H{'Lexical::Failure::handlers_scope_ID'} = $handlers_scope_ID;
56 9 push @VALID_FAILURE_HANDLERS_FOR_SCOPE, { %STD_FAILURE_HANDLERS, %{$handlers} };
57  
58 # Install default failure handler for the caller's scope...
59 9 ▶ if (ref($default) ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $default ne "CODE") 9 0 9
60 croak "Unknown default failure handler: '$default'"
61 9 ▶ if !exists $VALID_FAILURE_HANDLERS_FOR_SCOPE[-1]{$default};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $VALID_FAILURE_HANDLERS_FOR_SCOPE[-1]{$default} 0 9 9
62 9 $default = $VALID_FAILURE_HANDLERS_FOR_SCOPE[-1]{$default};
63 }
64 9 my $default_scope_ID = scalar @ACTIVE_FAILURE_HANDLER_FOR_SCOPE;
65 9 $^H{'Lexical::Failure::default_scope_ID'} = $default_scope_ID;
66 9 push @ACTIVE_FAILURE_HANDLER_FOR_SCOPE, $default;
67  
68 9 return;
69 }
70  
71 sub _process_import_args {
72 9 ▶ my $package = shift;
Subroutine _process_import_args: called CC=17 SCAR=31.4
73  
74 # What we're looking for (and their values if we don't find them)...
75 9 my $fail = 'fail';
76 9 my $ON_FAILURE = 'ON_FAILURE';
77 9 my $default = 'croak';
78 9 my $handlers = {};
79  
80 # Trawl through the argument list...
81 9 ▶ while (defined( my $next_arg = shift @_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $next_arg = shift @_)) 4 9 13
82 4 ▶ if ($next_arg eq 'fail') {
Branch: 4 branches
BranchTrueFalseTotal
if ($next_arg eq 'fail') { } 1 3 4
elsif ($next_arg eq 'ON_FAILURE') { } 1 2 3
elsif ($next_arg eq 'default') { } 1 1 2
elsif ($next_arg eq 'handlers') { } 1 0 1
83 1 ▶ $fail = shift(@_)
Branch: 1 branch
BranchTrueFalseTotal
$fail = shift @_ or croak("Missing rename for 'fail' in use $package") 0 1 1
84 or croak "Missing rename for 'fail' in use $package";
85 1 ▶ croak "Value for 'fail' option must be a string"
Branch: 1 branch
BranchTrueFalseTotal
if ref $fail 0 1 1
86 if ref $fail;
87 }
88 elsif ($next_arg eq 'ON_FAILURE') {
89 1 ▶ $ON_FAILURE = shift(@_)
Branch: 1 branch
BranchTrueFalseTotal
$ON_FAILURE = shift @_ or croak("Missing rename for 'fail_width' in use $package") 0 1 1
90 or croak "Missing rename for 'fail_width' in use $package";
91 1 ▶ croak "Value for 'ON_FAILURE' option must be a string"
Branch: 1 branch
BranchTrueFalseTotal
if ref $fail 0 1 1
92 if ref $fail;
93 }
94 elsif ($next_arg eq 'default') {
95 1 ▶ $default = shift(@_)
Branch: 1 branch
BranchTrueFalseTotal
$default = shift @_ or croak("Missing specification for 'default' in use $package") 0 1 1
96 or croak "Missing specification for 'default' in use $package";
97 1 ▶ croak "Value for 'default' option must be a string or subroutine reference"
Branch: 1 branch
BranchTrueFalseTotal
if ref $fail and ref $fail ne "CODE" 0 1 1
Condition33%1/3
ref $fail and ref $fail ne "CODE"
!ll&&!rl&&r
100
MC/DC0%0/2
ref $fail and ref $fail ne "CODE"
ref $fail ref $fail ne "CODE"
Truth table33%1/3
A and B
A= ref $failB= ref $fail ne "CODE"
ABresult
0X0
100
111
98 if ref $fail && ref $fail ne 'CODE';
99 }
100 elsif ($next_arg eq 'handlers') {
101 1 ▶ $handlers = shift(@_)
Branch: 1 branch
BranchTrueFalseTotal
$handlers = shift @_ or croak("Missing specification for 'handlers' in use $package") 0 1 1
102 or croak "Missing specification for 'handlers' in use $package";
103 1 ▶ croak "Value for 'handlers' option must be a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
if not ref $handlers or ref $handlers ne "HASH" 0 1 1
Condition33%1/3
not ref $handlers or ref $handlers ne "HASH"
l!l&&r!l&&!r
001
MC/DC0%0/2
not ref $handlers or ref $handlers ne "HASH"
not ref $handlers ref $handlers ne "HASH"
Truth table33%1/3
not(A) or B
A= not ref $handlersB= ref $handlers ne "HASH"
ABresult
1X1
011
000
104 if !ref $handlers || ref $handlers ne 'HASH';
105 croak "Handlers in 'handlers' hash must all be code references"
106 1 ▶ if grep { ref($_) ne 'CODE' } values %{$handlers};
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "CODE";} values %{$handlers;} 0 1 1
107 }
108 else {
109 0 croak "Unexpected argument ($next_arg) in use $package"
110 }
111 }
112  
113 9 return ($fail, $ON_FAILURE, $default, $handlers);
114 }
115  
116 sub _replace_keyword_with {
117 18 ▶ my $replacement = shift;
Subroutine _replace_keyword_with: called CC=1 SCAR=0.0
118  
119 return sub {
120 16 ▶ my ($src_ref) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
121 16 substr(${$src_ref}, 0, 0) = $replacement;
122 }
123 18 }
124  
125 sub ON_FAILURE {
126 34 ▶ my $handler = shift;
Subroutine ON_FAILURE: called CC=6 SCAR=18.0
Pod documented
127  
128 # No arg or undef arg --> no-op...
129 34 ▶ return if !defined $handler;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $handler 4 30 34
130  
131 # Can't be called at runtime...
132 30 ▶ if (${^GLOBAL_PHASE} ne 'START') {
Branch: 1 branch
BranchTrueFalseTotal
if (${^GLOBAL_PHASE} ne "START") 1 29 30
133 1 croak "Can't call ON_FAILURE after compilation"
134 }
135  
136 # Can't be called outside a subroutine...
137 29 ▶ if ((caller 1)[3] eq '(eval)') {
Branch: 1 branch
BranchTrueFalseTotal
if ((caller 1)[3] eq "(eval)") 0 29 29
138 0 croak "Can't call ON_FAILURE outside a subroutine"
139 }
140  
141 # Can only be called with certain types of arguments...
142 29 my $handler_type = ref $handler;
143 29 ▶ croak "Invalid handler type ($handler_type ref) in call to ON_FAILURE"
Branch: 1 branch
BranchTrueFalseTotal
unless $handler_type =~ /\A (?: CODE | SCALAR | ARRAY | HASH | (?#STRING) ) \z/msux 0 29 29
144 if $handler_type !~ m{\A (?: CODE | SCALAR | ARRAY | HASH | (?#STRING) ) \z}xms;
145  
146 # Which package is setting this handler???
147 29 my $owner = caller;
148  
149 # Locate valid failure handlers...
150 29 my $handlers_scope_ID = (caller 0)[$HINTS]{'Lexical::Failure::handlers_scope_ID'};
151 29 my $valid_handlers_ref = $VALID_FAILURE_HANDLERS_FOR_SCOPE[$handlers_scope_ID];
152  
153 # Translate failure handlers (if necessary)...
154 29 given (ref $handler) {
155 # Find handler for symbolic failure modes ('die', 'confess', etc.)...
156 29 when (q{}) {
157 croak "Unknown failure handler: '$handler'"
158 14 ▶ if !exists $valid_handlers_ref->{$handler};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid_handlers_ref->{$handler} 0 14 14
159 14 $handler = $valid_handlers_ref->{$handler};
160 }
161  
162 15 my $target_var = $handler;
163 # _check_scoping_of($target_var); # Experimentally removed (may not be necessary)
164  
165 # Scalars are simply assigned to...
166 15 when ('SCALAR') {
167 6 ▶ $handler = sub { ${$target_var} = [@_]; return; }
Subroutine __ANON__: called CC=1 SCAR=0.0
168 6 }
169  
170 # Arrays are simply pushed onto...
171 9 when ('ARRAY') {
172 5 ▶ $handler = sub { push @{$target_var}, [@_]; return; }
Subroutine __ANON__: called CC=1 SCAR=0.0
173 3 }
174  
175 # Hashes are simply added to...
176 6 when ('HASH') {
177 $handler = sub {
178 3 ▶ my $caller_sub = (caller 2)[3];
Subroutine __ANON__: called CC=1 SCAR=0.0
179 3 $target_var->{$caller_sub} = [@_];
180 3 return;
181 }
182 3 }
183 }
184  
185 # Install failure handler for the scope...
186 29 my $scope_ID = scalar @ACTIVE_FAILURE_HANDLER_FOR_SCOPE;
187 29 $^H{"Lexical::Failure::scope_ID::$owner"} = $scope_ID;
188 29 push @ACTIVE_FAILURE_HANDLER_FOR_SCOPE, $handler;
189  
190 29 return;
191 }
192  
193 # Fail by calling the appropriate handler...
194 sub fail {
195 43 ▶ my (@msg) = @_;
Subroutine fail: called CC=3 SCAR=11.0
Pod documented
196  
197 # Find the requested lexical handler...
198 43 my $caller = caller;
199 43 my $fail_handler = _find_callers_handler($caller);
200  
201 # Determine original context of sub that's failing...
202 43 my $context = want_at(UP SUB);
203  
204 # Ignore this code when croaking/carping from a handler
205 package
206 Carp;
207 9 ▶ use Scope::Upper qw< unwind UP SUB>;
Subroutine BEGIN: called CC=1 SCAR=0.0
208  
209 # Simulate a return...
210 43 ▶ unwind +( !defined $context ? do{ $fail_handler->(@msg); undef; }
Branch: 2 branches
BranchTrueFalseTotal
!defined($context) ? : 12 31 43
!$context ? : 26 5 31
211 : ! $context ? scalar $fail_handler->(@msg)
212 : $fail_handler->(@msg)
213 ) => UP SUB;
214 }
215  
216 # (Experimentally remove these checks as they may not be necessary...or reliable)
217 #
218 #sub _check_scoping_of {
219 # my ($target_var) = @_;
220 #
221 # # Is this something we can check???
222 # my $var_type = ref $target_var;
223 # return if $var_type !~ m{\A (?: SCALAR | ARRAY | HASH ) \z}x;
224 #
225 # # Look up the potential variables it could be...
226 # use PadWalker qw< peek_my peek_our var_name >;
227 # my %vars = ( %{peek_our(3)}, %{peek_my(3)} );
228 #
229 # # If it isn't any of them, warn us...
230 # if (!grep { $vars{$_} == $target_var } keys %vars) {
231 # return if _is_package_var($target_var);
232 #
233 # cluck 'Lexical ' . lc($var_type) . ' used as failure handler may not stay shared at runtime';
234 # }
235 #}
236 #
237 #sub _is_package_var {
238 # my ($target_ref) = @_;
239 #
240 # my @packages = ('main');
241 # my %seen;
242 #
243 # while (my $package = shift @packages) {
244 # no strict;
245 # while (($name, $entry) = each(%{*{"$package\::"}})) {
246 # local(*ENTRY) = $entry // next;
247 #
248 # # Check for match...
249 # return 1 if defined *ENTRY{SCALAR} && *ENTRY{SCALAR} == $target_ref
250 # || defined *ENTRY{ARRAY} && *ENTRY{ARRAY} == $target_ref
251 # || defined *ENTRY{HASH} && *ENTRY{HASH} == $target_ref;
252 #
253 # # Check down tree...
254 # if (defined *ENTRY{HASH} && $name =~ m{ (?<child> .* ) :: \z }xms) {
255 # next if $seen{$+{child}}++;
256 # push @packages, $+{child};
257 # }
258 # }
259 # }
260 #
261 # return 0;
262 #}
263  
264 # Locate hints hash of first scope outside caller (if any)...
265 sub _find_callers_handler {
266 43 ▶ my ($immediate_caller_package) = @_;
Subroutine _find_callers_handler: called CC=6 SCAR=19.6
267  
268 # Scope ID for default handler...
269 my $default_scope_ID
270 43 ▶ = (caller 1)[$HINTS]{'Lexical::Failure::default_scope_ID'}
Condition33%1/3
+(caller 1)[$HINTS]->{"Lexical::Failure::default_scope_ID"} // $DEFAULT_SCOPE_ID
l!l&&r!l&&!r
4300
MC/DC0%0/2
+(caller 1)[$HINTS]->{"Lexical::Failure::default_scope_ID"} // $DEFAULT_SCOPE_ID
+(caller 1)[$HINTS]->{"Lexical::Failure::default_scope_ID"} $DEFAULT_SCOPE_ID
Truth table33%1/3
A // B
A= +(caller 1)[$HINTS]->{"Lexical::Failure::default_scope_ID"}B= $DEFAULT_SCOPE_ID
ABresult
1X1
011
000
271 // $DEFAULT_SCOPE_ID;
272  
273 # Search upwards for first namespace different from $caller...
274 LEVEL:
275 43 for my $uplevel (2..10000) {
276 43 my @uplevel_caller = caller($uplevel);
277  
278 # Give up if no higher contexts...
279 43 ▶ last LEVEL if !@uplevel_caller;
Branch: 1 branch
BranchTrueFalseTotal
unless @uplevel_caller 0 43 43
280  
281 # Return handler for first different namespace (or else default handler)...
282 43 ▶ if ($uplevel_caller[0] ne $immediate_caller_package) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uplevel_caller[0] ne $immediate_caller_package) 43 0 43
283 my $target_scope_ID
284 43 ▶ = $uplevel_caller[10]{"Lexical::Failure::scope_ID::$immediate_caller_package"}
Condition66%2/3
$uplevel_caller[10]{"Lexical::Failure::scope_ID::$immediate_caller_package"} // $default_scope_ID
l!l&&r!l&&!r
3850
MC/DC0%0/2
$uplevel_caller[10]{"Lexical::Failure::scope_ID::$immediate_caller_package"} // $default_scope_ID
$uplevel_caller[10]{"Lexical::Failure::scope_ID::$immediate_caller_package"} $default_scope_ID
Truth table66%2/3
A // B
A= $uplevel_caller[10]{"Lexical::Failure::scope_ID::$immediate_caller_package"}B= $default_scope_ID
ABresult
1X1
011
000
285 // $default_scope_ID;
286 43 return $ACTIVE_FAILURE_HANDLER_FOR_SCOPE[ $target_scope_ID ];
287 }
288 }
289  
290 # If no such uplevel context, return a "null" hints hash...
291 0 return $ACTIVE_FAILURE_HANDLER_FOR_SCOPE[ $default_scope_ID ];
292 }
293  
294  
295 # Simulate a die() called at 2 levels higher up the stack...
296 sub _uplevel_die {
297 2 ▶ my $exception = @_ ? join(q{},@_)
Subroutine _uplevel_die: called CC=6 SCAR=21.4
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 2 0 2
$@ ? : 0 0 0
298 : $@ ? qq{$@\t...propagated}
299 : q{Died};
300  
301 2 ▶ die $exception if ref $exception;
Branch: 1 branch
BranchTrueFalseTotal
if ref $exception 0 2 2
302  
303 2 ▶ if (!ref $exception && substr($exception, -1) ne "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $exception and substr($exception, -1) ne "\n") 2 0 2
Condition33%1/3
not ref $exception and substr($exception, -1) ne "\n"
!ll&&!rl&&r
002
MC/DC0%0/2
not ref $exception and substr($exception, -1) ne "\n"
not ref $exception substr($exception, -1) ne "\n"
Truth table33%1/3
not(A) and B
A= not ref $exceptionB= substr($exception, -1) ne "\n"
ABresult
0X0
100
111
304 2 my (undef, $file, $line) = caller(2);
305 2 $exception .= " at $file line $line\n";
306 }
307  
308 2 die $exception;
309 }
310  
311 9 1; # Magic true value required at end of module
312  
313 __END__
↑ Summary blib/lib/Lexical/Failure/Objects.pm »