blib/lib/Language/Farnsworth/Error.pm

filter: Statementstmt 89.7% 61 / 68 Branchbran 77.7% 14 / 18 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 8 totaltotal 79.8% 95 / 119 CC 12 SCAR 11.6
CC
12
Cov
86%
perlwrap
6.6
error
5.0
debug
3.0
CRAP 3.2
« blib/lib/Language/Farnsworth/Functions/Standard.pm ↑ Summary blib/lib/Language/Farnsworth/Units.pm »
1 #!/usr/bin/perl
2  
3 package Language::Farnsworth::Error;
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use enum qw(RETURN EINTERP EPERL EPARI);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 require Exporter;
13 4 our @ISA = qw(Exporter);
14  
15 4 our @EXPORT = qw(error debug perlwrap farnsreturn RETURN EINTERP EPERL EPARI);
16  
17 4 ▶use overload '""' => \&tostring,
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 'eq' => \&eq;
19  
20 4 our $level = 0; #debugging level, 0 means nothing, 1 means informative, 2 means all kinds of shit.
21  
22 sub error
23 16 ▶{
Subroutine error: called CC=5 SCAR=16.1
Pod undocumented
24 16 my $type;
25 16 ▶ $type = shift if @_==2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 4 12 16
26 16 ▶ $type = EINTERP unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 12 4 16
27 16 my $err = shift;
28
29 #make already existing errors pass through transparently, fixes bug with return[], but i should find more direct route
30 #i had originally thought this might be a bug but now that i think about it, what is going on is...
31 # foo{x} := {return[1]; 2};
32 # Function Dispatch, evaluate the function;
33 # We then end up calling return[] which is a perl function, so it gets wrapped with perlwrap()
34 # This then causes the error to get wrapped by perlwrap()
35 #Circumventing this also allows perl code to correctly use error() to signify an error to the script rather than die
36 16 ▶ if (ref($err) && $err->isa("Language::Farnsworth::Error"))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $err and $err->isa("Language::Farnsworth::Error")) 9 7 16
Condition66%2/3
ref $err and $err->isa("Language::Farnsworth::Error")
!ll&&!rl&&r
709
MC/DC50%1/2
ref $err and $err->isa("Language::Farnsworth::Error")
ref $err $err->isa("Language::Farnsworth::Error")
Truth table66%2/3
A and B
A= ref $errB= $err->isa("Language::Farnsworth::Error")
ABresult
0X0
100
111
37 {
38 9 die $err;
39 }
40
41 7 my $eobj = {};
42 7 $eobj->{msg} = $err;
43 7 $eobj->{type} = $type;
44 7 $eobj->{caller} = [caller()];
45 7 bless $eobj;
46  
47 7 die $eobj;
48 }
49  
50 sub farnsreturn
51 3 ▶{
Subroutine farnsreturn: called CC=1 SCAR=0.0
Pod undocumented
52 3 my $return = shift;
53  
54 3 my $eobj = {};
55 3 $eobj->{msg} = $return;
56 3 $eobj->{type} = RETURN;
57 3 $eobj->{caller} = [caller()];
58 3 bless $eobj;
59  
60 3 die $eobj;
61 }
62  
63 sub isreturn
64 9 ▶{
Subroutine isreturn: called CC=2 SCAR=6.9
Pod undocumented
65 9 my $self = shift;
66 9 ▶ return 1 if ($self->{type} == RETURN);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"type"} == 0 3 6 9
67 6 return 0;
68 }
69  
70 sub getmsg
71 3 ▶{
Subroutine getmsg: called CC=1 SCAR=0.0
Pod undocumented
72 3 $_[0]->{msg};
73 }
74  
75 #wraps code and catches die() and wraps the error in our class
76 sub perlwrap(&;$)
77 63 ▶{
Subroutine perlwrap: called CC=5 SCAR=18.9
Pod undocumented
78 # print "INPERLWRAP\n";
79 63 my $code=shift;
80 63 my $default=shift;
81 63 ▶ $default=EPERL unless defined $default;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default 63 0 63
82
83 # print "WANTARRAY: ", wantarray(), "\n";
84 #preserve the context, makes things easier
85 63 ▶ if (wantarray) #array context
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 63 63
86 {
87 0 my @ret = eval {$code->()};
88 # print "DUMPER: ", Dumper(@ret), "\n";
89 # print "DUMP ERR: ", Dumper($@), "\n";
90 0 ▶ error $default, $@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
91 0 return @ret;
92 }
93 else #scalar context
94 {
95 63 my $ret = eval {$code->()};
96 # print "DUMPER: ", Dumper($ret), "\n";
97 # print "DUMP ERR: ", Dumper($@), "\n";
98 63 ▶ error $default, $@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 3 60 63
99 60 return $ret;
100 }
101 }
102  
103 sub tostring
104 16 ▶{
Subroutine tostring: called CC=1 SCAR=0.0
Pod undocumented
105 16 my $self = shift;
106 16 return $self->{msg};
107 }
108  
109 sub eq
110 0 ▶{
Subroutine eq: not called CC=1 SCAR=6.9
Pod undocumented
111 0 my ($one, $two, $rev) = @_;
112  
113 0 my $str = $one->tostring();
114 0 return $str eq $two;
115 }
116  
117 #i'd love something a little more efficient but, oh well.
118 sub debug
119 1336 ▶{
Subroutine debug: called CC=3 SCAR=11.0
Pod undocumented
120 1336 my ($mlevel, @messages) = @_;
121
122 4 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
123 1336 ▶ print @messages,"\n" if ($mlevel <= $level && @messages);
Branch: 1 branch
BranchTrueFalseTotal
if $mlevel <= $Language::Farnsworth::Error::level and @messages 47 1289 1336
Condition66%2/3
$mlevel <= $Language::Farnsworth::Error::level and @messages
!ll&&!rl&&r
1289047
MC/DC50%1/2
$mlevel <= $Language::Farnsworth::Error::level and @messages
$mlevel <= $Language::Farnsworth::Error::level @messages
Truth table66%2/3
A and B
A= $mlevel <= $Language::Farnsworth::Error::levelB= @messages
ABresult
0X0
100
111
124 }
125  
126 4 1;
127 __END__
« blib/lib/Language/Farnsworth/Functions/Standard.pm ↑ Summary blib/lib/Language/Farnsworth/Units.pm »