blib/lib/TryCatch.pm

filter: Statementstmt 93.7% 149 / 159 Branchbran 86.8% 33 / 38 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 94.1% 32 / 34 Podpod 0.0% 0 / 11 totaltotal 87.1% 216 / 248 CC 21 SCAR 10.0
CC
21
Cov
92%
parse_proto_using_pms
6.0
__ANON__
6.0
_parse_catch
5.0
CRAP 2.7
↑ Summary TryCatch.xs »
1 package TryCatch;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 10 ▶use Devel::Declare ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use B::Hooks::EndOfScope;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use B::Hooks::OP::PPAddr;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Devel::Declare::Context::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Parse::Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Scope::Upper qw/localize unwind want_at :words/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 10 ▶use base qw/Devel::Declare::Context::Simple/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 10 our $VERSION = '1.003002';
21  
22 # Signal to the xs PL_check hooks.
23 10 our $NEXT_EVAL_IS_TRY = 0;
24  
25 # Constants
26 10 my ($LOOKAHEAD_TRY, $LOOKAHEAD_CATCH) = (0,1);
27  
28 10 XSLoader::load(__PACKAGE__, $VERSION);
29  
30 10 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 10 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
33 exports => [qw/try/],
34 groups => { default => [qw/try/] },
35 22 ▶ installer => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
36 22 my ($args, $to_export) = @_;
37 22 my $pack = $args->{into};
38 22 ▶ my $ctx_class = $args->{parser} || 'TryCatch';
Condition50%1/2
$args->{'parser'} || 'TryCatch'
l!l
022
MC/DC0%0/1
$args->{'parser'} || 'TryCatch'
$args->{'parser'}
Truth table50%1/2
A
A= $args->{'parser'}
Aresult
11
00
39  
40 22 foreach my $name (@$to_export) {
41 44 ▶ if (my $parser = __PACKAGE__->can("_parse_${name}")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parser = __PACKAGE__->can("_parse_$name")) 22 22 44
42 Devel::Declare->setup_for(
43 $pack,
44 39 ▶ { $name => { const => sub { $ctx_class->$parser($pack, @_) } } },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 22 );
46 }
47 }
48 22 Sub::Exporter::default_installer(@_);
49  
50 }
51 10 };
52  
53  
54 # The actual try call itself. Nothing to do with parsing.
55 0 ▶sub try () {
Subroutine try: not called CC=1 SCAR=6.9
Pod undocumented
56 0 return;
57 }
58  
59 # Where we store all the TCs for catch blocks created at compile time
60 # Not sure we really want to do this, but we will for now.
61 10 our $TC_LIBRARY = {};
62  
63 11 ▶sub check_tc {
Subroutine check_tc: called CC=2 SCAR=7.1
Pod undocumented
64 11 my ($class, $tc) = @_;
65  
66 11 ▶ my $type = $TC_LIBRARY->{$tc} or die "Unable to find parse TC for '$tc'";
Branch: 1 branch
BranchTrueFalseTotal
my $type = $TryCatch::TC_LIBRARY->{$tc} or die "Unable to find parse TC for '${tc}'" 11 0 11
67  
68 11 return $type->check($TryCatch::Error);
69 }
70  
71 # From here on out its parsing methods.
72  
73 # Replace 'try {' with an 'try; { local $@; eval {'
74 39 ▶sub _parse_try {
Subroutine _parse_try: called CC=2 SCAR=6.9
75 39 my ($class,$pack, @args) = @_;
76  
77 # Hide from carp - report errors from line of 'try {' in user source.
78 39 local $Carp::Internal{'Devel::Declare'} = 1;
79  
80 39 my $ctx = $class->new->init(@args);
81  
82 # Move parse head past 'try ' (space is optional
83 39 $ctx->skip_declarator;
84 39 $ctx->skipspace;
85  
86 # Shadow try to be a constant no-op sub. Hopefully
87 39 ▶ $ctx->shadow(sub () { } );
Subroutine __ANON__: called CC=1 SCAR=0.0
88  
89 39 ▶ $ctx->inject_if_block(
Branch: 1 branch
BranchTrueFalseTotal
$ctx->inject_if_block($ctx->inject_into_try . $ctx->scope_injector_call($LOOKAHEAD_TRY), ";") or croak("block required after try") 38 1 39
90 $ctx->inject_into_try . $ctx->scope_injector_call( $LOOKAHEAD_TRY ),
91 ';'
92 ) or croak "block required after try";
93  
94 38 $ctx->debug_linestr('post try');
95 }
96  
97 73 ▶sub scope_injector_call {
Subroutine scope_injector_call: called CC=1 SCAR=0.0
Pod undocumented
98 73 my ($self, $state) = @_;
99 73 return ' BEGIN { ' . ref( $self ) . "->inject_scope($state) }; ";
100 }
101  
102  
103 71 ▶sub inject_scope {
Subroutine inject_scope: called CC=1 SCAR=0.0
Pod undocumented
104 71 my ($class, $opts) = @_;
105  
106 71 my $hooks = TryCatch::XS::install_op_checks();
107  
108 71 ▶ on_scope_end {
Subroutine __ANON__: called CC=1 SCAR=0.0
109 71 $class->lookahead_after_block( $opts );
110  
111 # TODO: Rethink how i install the hooks. If i uninstall(/disable) them here
112 # then they get removed before the LEAVETRY check gets called. Probably
113 # switch to a single global set of hooks at look at %^H (?) for lexical
114 # goodness.
115  
116 #TryCatch::XS::uninstall_op_checks( $hooks );
117 #undef $hooks;
118 }
119 71 }
120  
121 # Called after the block from try {} or catch {}
122 #
123 # Look ahead and determine what action to take based on wether or not we see
124 # a 'catch' token after the block
125 71 ▶sub lookahead_after_block {
Subroutine lookahead_after_block: called CC=4 SCAR=13.9
Pod undocumented
126 71 my ($class, $state) = @_;
127 71 my $orig_offset = Devel::Declare::get_linestr_offset();
128 71 my $ctx = $class->new->init( '', $orig_offset );
129  
130 71 my $offset = $ctx->skipspace;
131 71 my $linestr = $ctx->get_linestr;
132  
133 71 my $toke = '';
134 71 my $len = 0;
135  
136 # Since we're not being called from a normal D::D callback, we have to
137 # find this info manually.
138 71 ▶ if ($len = Devel::Declare::toke_scan_word($offset, 1 )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len = Devel::Declare::toke_scan_word($offset, 1)) 53 18 71
139 53 $toke = substr( $linestr, $offset, $len );
140 53 $ctx->{Declarator} = $toke;
141 }
142  
143 71 ▶ if ($toke eq 'catch') {
Branch: 1 branch
BranchTrueFalseTotal
if ($toke eq 'catch') { } 36 35 71
144 # We don't want the 'catch' token in the output since it messes up the
145 # if/else we build up. So dont let control go back to perl just yet.
146  
147 36 $ctx->_parse_catch( $state );
148  
149 } else {
150 # No (more) catch blocks, so write the postlude
151 35 my $code;
152 35 ▶ if ($state == $LOOKAHEAD_CATCH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state == $LOOKAHEAD_CATCH) { } 27 8 35
153 27 $code = $ctx->inject_post_catch;
154 }
155 else {
156 8 $code = $ctx->inject_when_no_catch;
157 8 $NEXT_EVAL_IS_TRY = 1;
158 }
159  
160 # Don't try this at home kids
161 #
162 # Since there was no 'catch' following, move back to the end of the
163 # closing brace (where offset was when we started). If we are after the
164 # skip space then the 'parse pointer' could be at the start of a POD line,
165 # and ";=head1" isn't valid perl ;)
166 #
167 # This seems to cause problems with nested try so taken out for now
168 #
169 #TryCatch::XS::set_linestr_offset($orig_offset);
170 #$ctx->{Offset} = $orig_offset;
171  
172 35 substr($linestr, $ctx->offset, 0, $code);
173  
174 35 $ctx->set_linestr($linestr);
175 35 $ctx->debug_linestr("finalizer");
176 }
177 }
178  
179 36 ▶sub _parse_catch {
Subroutine _parse_catch: called CC=5 SCAR=16.1
180 36 my ($ctx, $state) = @_;
181  
182 # Hide these things from carp - this makes C<croak> appear to come from the source line.
183 36 local $Carp::Internal{'TryCatch'} = 1;
184 36 local $Carp::Internal{'Devel::Declare'} = 1;
185 36 local $Carp::Internal{'B::Hooks::EndOfScope::XS'} = 1;
186 36 local $Carp::Internal{'B::Hooks::EndOfScope::PP'} = 1;
187  
188 # This isn't a normal DD-callback, so we can strip_name to get rid of 'catch'
189 36 my $offset = $ctx->offset;
190 36 $ctx->strip_name;
191 36 $ctx->skipspace;
192  
193 36 $ctx->debug_linestr('catch');
194 36 my $linestr = $ctx->get_linestr;
195  
196 36 my ($code, $var_code, @conditions) = ("","");
197  
198 # optional ()
199 36 ▶ if (substr($linestr, $ctx->offset, 1) eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($linestr, $ctx->offset, 1) eq "(") 31 5 36
200 31 ($var_code, @conditions) = $ctx->parse_proto()
201 }
202  
203 34 ▶ @conditions = ('1') unless @conditions;
Branch: 1 branch
BranchTrueFalseTotal
unless @conditions 20 14 34
204  
205 34 ▶ if ( $state != $LOOKAHEAD_CATCH ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state != $LOOKAHEAD_CATCH) { } 28 6 34
206 28 $NEXT_EVAL_IS_TRY = 1;
207 28 $code = $ctx->inject_after_try . "if (";
208 }
209 else {
210 6 $code = "elsif (";
211 }
212  
213 34 $var_code = $ctx->scope_injector_call( $LOOKAHEAD_CATCH ) . $var_code;
214  
215 34 ▶ $ctx->inject_if_block(
Branch: 1 branch
BranchTrueFalseTotal
$ctx->inject_if_block($var_code, $code . join(" && ", @conditions) . ')') or croak("block required after catch") 33 1 34
216 $var_code,
217 $code . join(' && ', @conditions) . ')'
218 ) or croak "block required after catch";
219  
220 33 $ctx->debug_linestr('post catch');
221  
222 }
223  
224 31 ▶sub parse_proto {
Subroutine parse_proto: called CC=2 SCAR=6.9
Pod undocumented
225 31 my ($self) = @_;
226  
227 31 my $proto = $self->strip_proto;
228 31 ▶ croak "Run-away catch signature"
Branch: 1 branch
BranchTrueFalseTotal
unless length $proto 1 30 31
229 unless (length $proto);
230  
231 30 return $self->parse_proto_using_pms($proto);
232 }
233  
234 13 ▶sub _string_to_tc {
Subroutine _string_to_tc: called CC=2 SCAR=6.9
235 13 my ($class, $name) = @_;
236  
237 13 my $tc = $class->find_registered_constraint($name);
238  
239 13 ▶ return $tc if ref $tc;
Branch: 1 branch
BranchTrueFalseTotal
if ref $tc 12 1 13
240  
241 1 return Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($name)
242 }
243  
244 30 ▶sub parse_proto_using_pms {
Subroutine parse_proto_using_pms: called CC=6 SCAR=17.9
Pod undocumented
245 30 my ($self, $proto) = @_;
246  
247 30 my @conditions;
248  
249 30 my $sig = Parse::Method::Signatures->new(
250 input => $proto,
251 from_namespace => $self->get_curstash_name,
252 type_constraint_callback => \&_string_to_tc,
253 );
254 30 my $errctx = $sig->ppi;
255 30 my $param = $sig->param;
256  
257 30 ▶ $sig->error( $errctx, "Parameter expected")
Branch: 1 branch
BranchTrueFalseTotal
unless $param 1 29 30
258 unless $param;
259  
260 29 my $left = $sig->remaining_input;
261  
262 29 my $var_code = '';
263  
264 29 ▶ if (my $var_name = $param->can('variable_name') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $var_name = $param->can("variable_name")) 28 1 29
265  
266 28 my $name = $param->$var_name();
267 28 $var_code = "my $name = \$TryCatch::Error;";
268 }
269  
270 # (TC $var)
271 29 ▶ if ($param->has_type_constraints) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->has_type_constraints) 9 20 29
272 9 my $tc = $param->meta_type_constraint;
273 9 $TryCatch::TC_LIBRARY->{"$tc"} = $tc;
274 9 push @conditions, "TryCatch->check_tc('$tc')";
275 }
276  
277 # ($var where { $_ } )
278 29 ▶ if ($param->has_constraints) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->has_constraints) 10 19 29
279 10 foreach my $con (@{$param->constraints}) {
280 10 $con =~ s/^{|}$//g;
281 10 push @conditions, "do {local \$_ = \$TryCatch::Error; $con }";
282 }
283 }
284  
285 29 return $var_code, @conditions;
286 }
287  
288  
289 #######################################################################
290 # Injected snippets
291  
292 39 ▶sub inject_into_try {
Subroutine inject_into_try: called CC=1 SCAR=0.0
Pod undocumented
293 # try { ...
294 # ->
295 # try; { local $@; eval { ...
296  
297 39 'local $@; eval {'
298 }
299  
300 28 ▶sub inject_after_try {
Subroutine inject_after_try: called CC=1 SCAR=0.0
Pod undocumented
301 # This semicolon is for the end of the eval
302 28 return ';$TryCatch::Error = $@; } if ($TryCatch::Error) { ';
303 }
304  
305 8 ▶sub inject_when_no_catch {
Subroutine inject_when_no_catch: called CC=1 SCAR=0.0
Pod undocumented
306 # This undef is to ensure that there is the eval{}; is called in void context
307 # i.e that its not the last op in a subroutine
308 8 return "};undef;";
309 }
310  
311 27 ▶sub inject_post_catch {
Subroutine inject_post_catch: called CC=1 SCAR=0.0
Pod undocumented
312 # We do it like this so that PROPGATE gets called, in case anyone is using it
313 27 return 'else { $@ = $TryCatch::Error; die } };undef;';
314 }
315  
316 #######################################################################
317  
318 10 ▶require Devel::PartialDump if $ENV{TRYCATCH_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRYCATCH_DEBUG"} 0 10 10
319  
320 *debug_linestr = !( ($ENV{TRYCATCH_DEBUG} || 0) & 1)
321 142 ▶ ? sub {}
Subroutine __ANON__: called CC=1 SCAR=0.0
322 0 ▶ : sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
323 0 my ($ctx, $message) = @_;
324  
325 0 local $Carp::Internal{'TryCatch'} = 1;
326 0 local $Carp::Internal{'TryCatch::Basic'} = 1;
327 0 local $Carp::Internal{'Devel::Declare'} = 1;
328 0 local $Carp::Internal{'B::Hooks::EndOfScope:XS'} = 1;
329 0 local $Carp::Internal{'B::Hooks::EndOfScope:PP'} = 1;
330 0 local $Carp::Internal{'Devel::PartialDump'} = 1;
331 0 ▶ Carp::cluck($message) if $message;
Branch: 1 branch
BranchTrueFalseTotal
if $message 0 0 0
332  
333 0 warn " Substr: ", Devel::PartialDump::dump(substr($ctx->get_linestr, $ctx->offset)),
334 "\n Whole: ", Devel::PartialDump::dump($ctx->get_linestr), "\n\n";
335 10 ▶};
Branch: 1 branch
BranchTrueFalseTotal
!(($ENV{'TRYCATCH_DEBUG'} || 0) & 1) ? : 10 0 10
Condition50%1/2
$ENV{'TRYCATCH_DEBUG'} || 0
l!l
010
MC/DC0%0/1
$ENV{'TRYCATCH_DEBUG'} || 0
$ENV{'TRYCATCH_DEBUG'}
Truth table50%1/2
A
A= $ENV{'TRYCATCH_DEBUG'}
Aresult
11
00
336  
337  
338 10 1;
339  
340 __END__
↑ Summary TryCatch.xs »