/usr/local/lib/perl5/site_perl/5.44.0/Try/Tiny.pm

filter: Statementstmt 62.3% 53 / 85 Branchbran 28.9% 11 / 38 Conditioncond 26.6% 4 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 69.2% 9 / 13 Podpod 100% 3 / 3 totaltotal 48.7% 80 / 164 CC 28 SCAR 32.1
CC
28
Cov
49%
BEGIN
49.8
try
29.4
DESTROY
12.0
CRAP 24.9
« /usr/local/lib/perl5/5.44.0/CPAN/Meta/Requirements/Range.pm ↑ Summary /usr/local/lib/perl5/5.44.0/x86_64-linux/Socket.pm »
1 package Try::Tiny; # git description: v0.31-2-gc8e3a47
2 7 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 # ABSTRACT: Minimal try/catch with proper preservation of $@
4  
5 7 our $VERSION = '0.32';
6  
7 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 ▶use Exporter 5.57 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 our @EXPORT = our @EXPORT_OK = qw(try catch finally);
12  
13 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 $Carp::Internal{+__PACKAGE__}++;
15  
16 7 ▶BEGIN {
Subroutine BEGIN: called CC=11 SCAR=39.1
17 7 ▶ my $su = $INC{'Sub/Util.pm'} && defined &Sub::Util::set_subname;
Condition33%1/3
$INC{'Sub/Util.pm'} && defined &Sub::Util::set_subname
!ll&&!rl&&r
007
MC/DC0%0/2
$INC{'Sub/Util.pm'} && defined &Sub::Util::set_subname
$INC{'Sub/Util.pm'} defined &Sub::Util::set_subname
Truth table33%1/3
A && B
A= $INC{'Sub/Util.pm'}B= defined &Sub::Util::set_subname
ABresult
0X0
100
111
18 0 ▶ my $sn = $INC{'Sub/Name.pm'} && eval { Sub::Name->VERSION(0.08) };
Condition33%1/3
$INC{'Sub/Name.pm'} && eval { do { 'Sub::Name'->VERSION(0.08) } }
!ll&&!rl&&r
700
MC/DC0%0/2
$INC{'Sub/Name.pm'} && eval { do { 'Sub::Name'->VERSION(0.08) } }
$INC{'Sub/Name.pm'} eval { do { 'Sub::Name'->VERSION(0.08) } }
Truth table33%1/3
A && B
A= $INC{'Sub/Name.pm'}B= eval { do { 'Sub::Name'->VERSION(0.08) } }
ABresult
0X0
100
111
19 7 ▶ unless ($su || $sn) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($su or $sn) 0 7 7
Condition33%1/3
$su or $sn
l!l&&r!l&&!r
700
MC/DC0%0/2
$su or $sn
$su $sn
Truth table33%1/3
A or B
A= $suB= $sn
ABresult
1X1
011
000
20 0 ▶ $su = eval { require Sub::Util; } && defined &Sub::Util::set_subname;
Condition0%0/3
eval { do { require Sub::Util } } && defined &Sub::Util::set_subname
!ll&&!rl&&r
000
MC/DC0%0/2
eval { do { require Sub::Util } } && defined &Sub::Util::set_subname
eval { do { require Sub::Util } } defined &Sub::Util::set_subname
Truth table0%0/3
A && B
A= eval { do { require Sub::Util } }B= defined &Sub::Util::set_subname
ABresult
0X0
100
111
21 0 ▶ unless ($su) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($su) 0 0 0
22 0 $sn = eval { require Sub::Name; Sub::Name->VERSION(0.08) };
23 }
24 }
25  
26 *_subname = $su ? \&Sub::Util::set_subname
27 : $sn ? \&Sub::Name::subname
28 0 ▶ : sub { $_[1] };
Subroutine __ANON__: not called CC=1 SCAR=2.6
Branch: 2 branches
BranchTrueFalseTotal
$su ? : 7 0 7
$sn ? : 0 0 0
29 7 ▶ *_HAS_SUBNAME = ($su || $sn) ? sub(){1} : sub(){0};
Branch: 1 branch
BranchTrueFalseTotal
$su || $sn ? : 7 0 7
Condition33%1/3
$su or $sn
l!l&&r!l&&!r
700
MC/DC0%0/2
$su or $sn
$su $sn
Truth table33%1/3
A or B
A= $suB= $sn
ABresult
1X1
011
000
30 }
31  
32 7 my %_finally_guards;
33  
34 # Need to prototype as @ not $$ because of the way Perl evaluates the prototype.
35 # Keeping it at $$ means you only ever get 1 sub because we need to eval in a list
36 # context & not a scalar one
37  
38 14 ▶sub try (&;@) {
Subroutine try: called CC=13 SCAR=33.8
Pod documented
39 14 my ( $try, @code_refs ) = @_;
40  
41 # we need to save this here, the eval block will be in scalar context due
42 # to $failed
43 14 my $wantarray = wantarray;
44  
45 # work around perl bug by explicitly initializing these, due to the likelyhood
46 # this will be used in global destruction (perl rt#119311)
47 14 my ( $catch, @finally ) = ();
48  
49 # find labeled blocks in the argument list.
50 # catch and finally tag the blocks by blessing a scalar reference to them.
51 14 foreach my $code_ref (@code_refs) {
52  
53 14 ▶ if ( ref($code_ref) eq 'Try::Tiny::Catch' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $code_ref eq 'Try::Tiny::Catch') { } 14 0 14
elsif (ref $code_ref eq 'Try::Tiny::Finally') { } 0 0 0
54 14 ▶ croak 'A try() may not be followed by multiple catch() blocks'
Branch: 1 branch
BranchTrueFalseTotal
if $catch 0 14 14
55 if $catch;
56 14 $catch = ${$code_ref};
57 } elsif ( ref($code_ref) eq 'Try::Tiny::Finally' ) {
58 0 push @finally, ${$code_ref};
59 } else {
60 0 ▶ croak(
Branch: 1 branch
BranchTrueFalseTotal
defined $code_ref ? : 0 0 0
61 'try() encountered an unexpected argument ('
62 . ( defined $code_ref ? $code_ref : 'undef' )
63 . ') - perhaps a missing semi-colon before or'
64 );
65 }
66 }
67  
68 # FIXME consider using local $SIG{__DIE__} to accumulate all errors. It's
69 # not perfect, but we could provide a list of additional errors for
70 # $catch->();
71  
72 # name the blocks if we have Sub::Name installed
73 14 _subname(caller().'::try {...} ' => $try)
74 if _HAS_SUBNAME;
75  
76 # set up scope guards to invoke the finally blocks at the end.
77 # this should really be a function scope lexical variable instead of
78 # file scope + local but that causes issues with perls < 5.20 due to
79 # perl rt#119311
80 local $_finally_guards{guards} = [
81 14 map Try::Tiny::ScopeGuard->_new($_),
82 @finally
83 ];
84  
85 # save the value of $@ so we can set $@ back to it in the beginning of the eval
86 # and restore $@ after the eval finishes
87 14 my $prev_error = $@;
88  
89 14 my ( @ret, $error );
90  
91 # failed will be true if the eval dies, because 1 will not be returned
92 # from the eval body
93 14 my $failed = not eval {
94 14 $@ = $prev_error;
95  
96 # evaluate the try block in the correct context
97 14 ▶ if ( $wantarray ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wantarray) { } 0 14 14
elsif (defined $wantarray) { } 0 14 14
98 0 @ret = $try->();
99 } elsif ( defined $wantarray ) {
100 0 $ret[0] = $try->();
101 } else {
102 14 $try->();
103 };
104  
105 14 return 1; # properly set $failed to false
106 };
107  
108 # preserve the current error and reset the original value of $@
109 14 $error = $@;
110 14 $@ = $prev_error;
111  
112 # at this point $failed contains a true value if the eval died, even if some
113 # destructor overwrote $@ as the eval was unwinding.
114 14 ▶ if ( $failed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed) { } 0 14 14
115 # pass $error to the finally blocks
116 0 push @$_, $error for @{$_finally_guards{guards}};
117  
118 # if we got an error, invoke the catch block.
119 0 ▶ if ( $catch ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($catch) 0 0 0
120 # This works like given($error), but is backwards compatible and
121 # sets $_ in the dynamic scope for the body of C<$catch>
122 0 for ($error) {
123 0 return $catch->($error);
124 }
125  
126 # in case when() was used without an explicit return, the C<for>
127 # loop will be aborted and there's no useful return value
128 }
129  
130 0 return;
131 } else {
132 # no failure, $@ is back to what it was, everything is fine
133 14 ▶ return $wantarray ? @ret : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 14 14
134 }
135 }
136  
137 14 ▶sub catch (&;@) {
Subroutine catch: called CC=2 SCAR=7.0
Pod documented
138 14 my ( $block, @rest ) = @_;
139  
140 14 ▶ croak 'Useless bare catch()' unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 14 14
141  
142 14 _subname(caller().'::catch {...} ' => $block)
143 if _HAS_SUBNAME;
144 return (
145 14 bless(\$block, 'Try::Tiny::Catch'),
146 @rest,
147 );
148 }
149  
150 0 ▶sub finally (&;@) {
Subroutine finally: not called CC=2 SCAR=17.9
Pod documented
151 0 my ( $block, @rest ) = @_;
152  
153 0 ▶ croak 'Useless bare finally()' unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
154  
155 0 _subname(caller().'::finally {...} ' => $block)
156 if _HAS_SUBNAME;
157 return (
158 0 bless(\$block, 'Try::Tiny::Finally'),
159 @rest,
160 );
161 }
162  
163 {
164 package # hide from PAUSE
165 Try::Tiny::ScopeGuard;
166  
167 7 ▶ use constant UNSTABLE_DOLLARAT => ("$]" < '5.013002') ? 1 : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
"$]" < 5.013002 ? : 0 7 7
168  
169 0 ▶ sub _new {
Subroutine _new: not called CC=1 SCAR=6.9
170 0 shift;
171 0 bless [ @_ ];
172 }
173  
174 0 ▶ sub DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
175 0 my ($code, @args) = @{ $_[0] };
176  
177 0 local $@ if UNSTABLE_DOLLARAT;
178 eval {
179 0 $code->(@args);
180 0 1;
181 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &$code(@args); 1 } } or do { warn "Execution of finally() block $code resulted in an exception, which " . '*CAN NOT BE PROPAGATED* due to fundamental limitations of Perl. ' . "Your program will continue as if this event never took place. " . "Original exception text follows:\n\n" . (defined $@ ? $@ : "\$\@ left undefined...") . "\n" } 0 0 0
182 0 ▶ warn
Branch: 1 branch
BranchTrueFalseTotal
defined $@ ? : 0 0 0
183 "Execution of finally() block $code resulted in an exception, which "
184 . '*CAN NOT BE PROPAGATED* due to fundamental limitations of Perl. '
185 . 'Your program will continue as if this event never took place. '
186 . "Original exception text follows:\n\n"
187 . (defined $@ ? $@ : '$@ left undefined...')
188 . "\n"
189 ;
190 }
191 }
192 }
193  
194 7 __PACKAGE__
195  
196 7 __END__
« /usr/local/lib/perl5/5.44.0/CPAN/Meta/Requirements/Range.pm ↑ Summary /usr/local/lib/perl5/5.44.0/x86_64-linux/Socket.pm »