Evo/Lib/PP.pm untested

filter: Statementstmt 0.0% 0 / 46 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 90 SCAR 48.8
CC
11
Cov
0%
try
30.0
strict_opts
30.0
eval_want
12.0
CRAP 132.0
1 package Evo::Lib::PP;
2 use Evo '-Export *; Carp croak';
3  
4 sub eval_want : Export {
5 my ($want, $fn) = (shift, pop);
6 if (!defined $want) {
7 eval { $fn->(@_); 1 } or return;
8 return sub { };
9 }
10 elsif (!$want) {
11 my $res;
12 eval { $res = $fn->(@_); 1 } or return;
13 return sub {$res};
14 }
15 else {
16 my @res;
17 eval { @res = $fn->(@_); 1 } or return;
18 return sub {@res};
19 }
20 }
21  
22 sub try : prototype(&$;$) : Export {
23 my ($try, $catch, $fin) = @_;
24 my $call = eval_want wantarray, $try;
25 $call = eval_want wantarray, my $e = $@, $catch if !$call && $catch;
26 if ($call) { # in normal way we are here, so separate this branch to avoid copying $@ before fin
27 $fin->() if $fin;
28 return $call->();
29 }
30 $e = $@;
31 $fin->() if $fin;
32 die $e;
33 }
34  
35 sub uniq : Export {
36 my %seen;
37 return grep { !$seen{$_}++ } @_;
38 }
39  
40 sub strict_opts ($hash, @other) : Export {
41 croak 'Usage strict_opts($hash, qw(key1 key2))'
42 . 'or strict_opts($hash, ["key1", "key2"], $level)'
43 if !@other || (ref $other[0]) && @other > 2;
44 my ($level, @keys);
45 if (ref $other[0]) {
46 @keys = (shift @other)->@*;
47 $level = @other ? shift(@other) : 1;
48 }
49 else {
50 @keys = @other;
51 $level = 1;
52 }
53  
54 my %opts = %$hash;
55 my @opts = map { delete $opts{$_} } @keys;
56 if (my @remaining = keys %opts) {
57 local $Carp::CarpLevel = $level;
58 croak "Unknown options: ", join ',', @remaining;
59 }
60 @opts;
61 }
62  
63  
64 1;
65  
66 __END__