blib/lib/Try/Lite.pm

filter: Statementstmt 90.2% 46 / 51 Branchbran 58.3% 14 / 24 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 72.1% 70 / 97 CC 16 SCAR 29.9
CC
16
Cov
74%
try
26.9
BEGIN
1.0
BEGIN
1.0
CRAP 19.8
↑ Summary
1 package Try::Lite;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 our $VERSION = '0.0.3';
7  
8 3 our @EXPORT = 'try';
9  
10 3 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 $Carp::Internal{+__PACKAGE__}++;
13  
14 5 ▶sub try (&;%) {
Subroutine try: called CC=16 SCAR=32.9
Pod documented
15 5 my($try, @catches) = @_;
16  
17 5 ▶ confess 'Unknown @catches values. Check your usage & try again'
Branch: 1 branch
BranchTrueFalseTotal
unless @catches 0 5 5
18 unless @catches;
19  
20 # check @catches
21 5 ▶ for (my $i = 0; $i < @catches; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @catches) 5 5 10
22 5 ▶ confess q{illegal @catches values. try {} 'ClassName' => sub {}, ...;}
Branch: 1 branch
BranchTrueFalseTotal
unless $catches[$i] and ref $catches[$i + 1] eq "CODE" 0 5 5
Condition33%1/3
$catches[$i] and ref $catches[$i + 1] eq "CODE"
!ll&&!rl&&r
005
MC/DC0%0/2
$catches[$i] and ref $catches[$i + 1] eq "CODE"
$catches[$i] ref $catches[$i + 1] eq "CODE"
Truth table33%1/3
A and B
A= $catches[$i]B= ref $catches[$i + 1] eq "CODE"
ABresult
0X0
100
111
23 unless $catches[$i] && ref($catches[$i + 1]) eq 'CODE';
24 }
25  
26 # we need to save this here, the eval block will be in scalar context due
27 # to $failed
28 5 my $wantarray = wantarray;
29  
30 # save the value of $@ so we can set $@ back to it in the beginning of the eval
31 # and restore $@ after the eval finishes
32 5 my $prev_error = $@;
33  
34 5 my @ret;
35  
36 # failed will be true if the eval dies, because 1 will not be returned
37 # from the eval body
38 5 my $failed = not eval {
39 5 $@ = $prev_error;
40  
41 # evaluate the try block in the correct context
42 5 ▶ if ( $wantarray ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wantarray) { } 0 5 5
elsif (defined $wantarray) { } 0 5 5
43 0 @ret = $try->();
44 } elsif ( defined $wantarray ) {
45 0 $ret[0] = $try->();
46 } else {
47 5 $try->();
48 };
49  
50 1 return 1; # properly set $fail to false
51 };
52  
53 # preserve the current error and reset the original value of $@
54 5 my $error = $@;
55 5 $@ = $prev_error;
56  
57 5 ▶ if ($failed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed) 4 1 5
58 4 ▶ for (my $i = 0;$i < @catches;$i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @catches) 4 0 4
59 4 my($class, $code) = ($catches[$i], $catches[$i + 1]);
60 4 ▶ next unless $class eq '*' || (Scalar::Util::blessed($error) && UNIVERSAL::isa($error, $class));
Branch: 1 branch
BranchTrueFalseTotal
unless $class eq "*" or builtin::blessed($error) and UNIVERSAL::isa($error, $class) 0 4 4
Condition16%1/6
$class eq "*" or builtin::blessed($error) and UNIVERSAL::isa($error, $class)
l!l&&r!l&&!r
400
builtin::blessed($error) and UNIVERSAL::isa($error, $class)
!ll&&!rl&&r
000
MC/DC0%0/3
$class eq "*" or builtin::blessed($error) and UNIVERSAL::isa($error, $class)
$class eq "*" builtin::blessed($error) UNIVERSAL::isa($error, $class)
Truth table25%1/4
A or B and C
A= $class eq "*"B= builtin::blessed($error)C= UNIVERSAL::isa($error, $class)
ABCresult
1XX1
0111
00X0
0100
61  
62 # evaluate the catch block in the correct context
63 4 $@ = $error;
64 4 my @ret;
65 4 ▶ if ( $wantarray ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wantarray) { } 0 4 4
elsif (defined $wantarray) { } 0 4 4
66 0 @ret = $code->();
67 } elsif ( defined $wantarray ) {
68 0 $ret[0] = $code->();
69 } else {
70 4 $code->();
71 };
72  
73 2 $@ = $prev_error;
74 2 ▶ return $wantarray ? @ret : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 2 2
75 }
76  
77 # rethrow
78 0 die $error;
79 }
80  
81 # no failure, $@ is back to what it was, everything is fine
82 1 ▶ return $wantarray ? @ret : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 1 1
83 }
84  
85  
86 3 1;
87 __END__
↑ Summary