| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | unless @catches; | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | # check @catches | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 5 | ▶ | for (my $i = 0; $i < @catches; $i += 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 5 | ▶ | confess q{illegal @catches values. try {} 'ClassName' => sub {}, ...;} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$catches[$i] and ref $catches[$i + 1] eq "CODE"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 4 | ▶ | for (my $i = 0;$i < @catches;$i += 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition16%1/6
$class eq "*" or builtin::blessed($error) and UNIVERSAL::isa($error, $class)
builtin::blessed($error) and UNIVERSAL::isa($error, $class)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | # evaluate the catch block in the correct context | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 4 | $@ = $error; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 4 | my @ret; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 4 | ▶ | if ( $wantarray ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 87 | __END__ | ||||||||||||||||||||||||||||||||||||||||||