Pure/Utils.pm

filter: Statementstmt 94.2% 98 / 104 Branchbran 85.0% 34 / 40 Conditioncond 38.8% 7 / 18 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 100% 11 / 11 Podpod 100% 5 / 5 totaltotal 82.1% 156 / 190 SCAR 34.1
CC
28
Cov
86%
_get_stack
30.4
err_helper
4.0
err_get
2.0
CRAP 30.2
↑ Summary Pure.pm »
1 package Error::Pure::Utils;
2  
3 34 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 34 ▶use Cwd qw(abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 34 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 34 our $VERSION = 0.31;
11  
12 34 Readonly::Array our @EXPORT_OK => qw(clean err_get err_helper err_msg err_msg_hr);
13 34 Readonly::Scalar my $DOTS => '...';
14 34 Readonly::Scalar my $EMPTY_STR => q{};
15 34 Readonly::Scalar my $EVAL => 'eval {...}';
16 34 Readonly::Scalar my $UNDEF => 'undef';
17  
18 # Errors array.
19 34 our @ERRORS;
20  
21 # Default initialization.
22 34 our $LEVEL = 2;
23 34 our $MAX_LEVELS = 50;
24 34 our $MAX_EVAL = 100;
25 34 our $MAX_ARGS = 10;
26 34 our $MAX_ARG_LEN = 50;
27 34 our $PROGRAM = $EMPTY_STR; # Program name in stack information.
28  
29 # Clean internal structure.
30 sub clean {
31 11 ▶ @ERRORS = ();
Subroutine clean: called CC=1 SCAR=0.0
Pod documented
32 11 return;
33 }
34  
35 # Get and clean processed errors.
36 sub err_get {
37 10 ▶ my $clean = shift;
Subroutine err_get: called CC=2 SCAR=6.9
Pod documented
38 10 my @ret = @ERRORS;
39 10 ▶ if ($clean) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean) 1 9 10
40 1 clean();
41 }
42 10 return @ret;
43 }
44  
45 # Process error without die.
46 sub err_helper {
47 61 ▶ my @msg = @_;
Subroutine err_helper: called CC=4 SCAR=13.9
Pod documented
48  
49 # Check to undefined values in @msg and chomp.
50 61 ▶ for (my $i = 0; $i < @msg; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @msg) 91 61 152
51 91 ▶ if (! defined $msg[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $msg[$i]) { } 7 84 91
52 7 $msg[$i] = $UNDEF;
53 } else {
54 84 chomp $msg[$i];
55 }
56 }
57  
58 # When is list blank, add undef.
59 61 ▶ if (! @msg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@msg) 6 55 61
60 6 push @msg, $UNDEF;
61 }
62  
63 # Get calling stack.
64 61 my @stack = _get_stack();
65  
66 # Create errors message.
67 61 push @ERRORS, {
68 'msg' => \@msg,
69 'stack' => \@stack,
70 };
71  
72 61 return @ERRORS;
73 }
74  
75 # Get first error messages array.
76 sub err_msg {
77 3 ▶ my $index = shift;
Subroutine err_msg: called CC=2 SCAR=6.9
Pod documented
78 3 ▶ if (! defined $index) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $index) 2 1 3
79 2 $index = -1;
80 }
81 3 my @err = err_get();
82 3 my @ret = @{$err[$index]->{'msg'}};
83 3 return @ret;
84 }
85  
86 # Get first error message key, value pairs as hash reference.
87 sub err_msg_hr {
88 3 ▶ my $index = shift;
Subroutine err_msg_hr: called CC=2 SCAR=6.9
Pod documented
89 3 ▶ if (! defined $index) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $index) 2 1 3
90 2 $index = -1;
91 }
92 3 my @err = err_get();
93 3 my @ret = @{$err[$index]->{'msg'}};
94 3 shift @ret;
95 3 return {@ret};
96 }
97  
98 # Get information about place of error.
99 sub _get_stack {
100 61 ▶ my $max_level = shift || $MAX_LEVELS;
Subroutine _get_stack: called CC=22 SCAR=34.1
Condition33%1/3
shift() || $Error::Pure::Utils::MAX_LEVELS
l!l&&r!l&&!r
0610
MC/DC0%0/2
shift() || $Error::Pure::Utils::MAX_LEVELS
shift() $Error::Pure::Utils::MAX_LEVELS
Truth table33%1/3
A || B
A= shift()B= $Error::Pure::Utils::MAX_LEVELS
ABresult
1X1
011
000
101 61 my @stack;
102 61 my $tmp_level = $LEVEL;
103 61 my ($class, $prog, $line, $sub, $hargs, $evaltext, $is_require);
104 61 ▶ while ($tmp_level < $max_level
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp_level < $max_level and do { package DB; () = caller $tmp_level++ }) 94 61 155
Condition66%2/3
$tmp_level < $max_level and do { package DB; () = caller $tmp_level++ }
!ll&&!rl&&r
06194
MC/DC50%1/2
$tmp_level < $max_level and do { package DB; () = caller $tmp_level++ }
$tmp_level < $max_level do { package DB; () = caller $tmp_level++ }
Truth table66%2/3
A and B
A= $tmp_level < $max_levelB= do { package DB; () = caller $tmp_level++ }
ABresult
0X0
100
111
105 155 && do { package DB; ($class, $prog, $line, $sub, $hargs,
106 undef, $evaltext, $is_require) = caller($tmp_level++); }) {
107  
108 # Prog to absolute path.
109 94 ▶ if (-e $prog) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $prog) 92 2 94
110 92 $prog = abs_path($prog);
111 }
112  
113 # Sub name.
114 94 ▶ if (defined $evaltext) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $evaltext) { } 2 92 94
elsif ($sub eq '(eval)') { } 41 51 92
115 2 ▶ if ($is_require) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_require) { } 0 2 2
116 0 $sub = "require $evaltext";
117 } else {
118 2 $evaltext =~ s/\n;//sm;
119 2 $evaltext =~ s/([\'])/\\$1/gsm;
120 2 ▶ if ($MAX_EVAL
Branch: 1 branch
BranchTrueFalseTotal
if ($Error::Pure::Utils::MAX_EVAL and length $evaltext > $Error::Pure::Utils::MAX_EVAL) 2 0 2
Condition33%1/3
$Error::Pure::Utils::MAX_EVAL and length $evaltext > $Error::Pure::Utils::MAX_EVAL
!ll&&!rl&&r
002
MC/DC0%0/2
$Error::Pure::Utils::MAX_EVAL and length $evaltext > $Error::Pure::Utils::MAX_EVAL
$Error::Pure::Utils::MAX_EVAL length $evaltext > $Error::Pure::Utils::MAX_EVAL
Truth table33%1/3
A and B
A= $Error::Pure::Utils::MAX_EVALB= length $evaltext > $Error::Pure::Utils::MAX_EVAL
ABresult
0X0
100
111
121 && length($evaltext) > $MAX_EVAL) {
122  
123 2 substr($evaltext, $MAX_EVAL, -1,
124 $DOTS);
125 }
126 2 $sub = "eval '$evaltext'";
127 }
128  
129 # My eval name.
130 } elsif ($sub eq '(eval)') {
131 41 $sub = $EVAL;
132  
133 # Other transformation.
134 } else {
135 51 $sub =~ s/^$class\:\:([^:]+)$/$1/gsmx;
136 51 ▶ if ($sub =~ m/^Error::Pure::(.*)err$/smx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub =~ /^Error::Pure::(.*)err$/msx) 49 2 51
137 49 $sub = 'err';
138 }
139 51 ▶ if ($PROGRAM && $prog =~ m/^\(eval/sm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Error::Pure::Utils::PROGRAM and $prog =~ /^\(eval/ms) 0 51 51
Condition33%1/3
$Error::Pure::Utils::PROGRAM and $prog =~ /^\(eval/ms
!ll&&!rl&&r
5100
MC/DC0%0/2
$Error::Pure::Utils::PROGRAM and $prog =~ /^\(eval/ms
$Error::Pure::Utils::PROGRAM $prog =~ /^\(eval/ms
Truth table33%1/3
A and B
A= $Error::Pure::Utils::PROGRAMB= $prog =~ /^\(eval/ms
ABresult
0X0
100
111
140 0 $prog = $PROGRAM;
141 }
142 }
143  
144 # Args.
145 94 my $i_args = $EMPTY_STR;
146 94 ▶ if ($hargs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hargs) 51 43 94
147 51 my @args = @DB::args;
148 51 ▶ if ($MAX_ARGS && $#args > $MAX_ARGS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Error::Pure::Utils::MAX_ARGS and $#args > $Error::Pure::Utils::MAX_ARGS) 0 51 51
Condition33%1/3
$Error::Pure::Utils::MAX_ARGS and $#args > $Error::Pure::Utils::MAX_ARGS
!ll&&!rl&&r
0510
MC/DC0%0/2
$Error::Pure::Utils::MAX_ARGS and $#args > $Error::Pure::Utils::MAX_ARGS
$Error::Pure::Utils::MAX_ARGS $#args > $Error::Pure::Utils::MAX_ARGS
Truth table33%1/3
A and B
A= $Error::Pure::Utils::MAX_ARGSB= $#args > $Error::Pure::Utils::MAX_ARGS
ABresult
0X0
100
111
149 0 $#args = $MAX_ARGS;
150 0 $args[-1] = $DOTS;
151 }
152  
153 # Get them all.
154 51 foreach my $arg (@args) {
155 53 ▶ if (! defined $arg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $arg) 6 47 53
156 6 $arg = 'undef';
157 6 next;
158 }
159 47 ▶ if (ref $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg) 0 47 47
160  
161 # Force string representation.
162 0 $arg .= $EMPTY_STR;
163 }
164 47 $arg =~ s/'/\\'/gms;
165 47 ▶ if ($MAX_ARG_LEN && length $arg> $MAX_ARG_LEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Error::Pure::Utils::MAX_ARG_LEN and length $arg > $Error::Pure::Utils::MAX_ARG_LEN) 0 47 47
Condition33%1/3
$Error::Pure::Utils::MAX_ARG_LEN and length $arg > $Error::Pure::Utils::MAX_ARG_LEN
!ll&&!rl&&r
0470
MC/DC0%0/2
$Error::Pure::Utils::MAX_ARG_LEN and length $arg > $Error::Pure::Utils::MAX_ARG_LEN
$Error::Pure::Utils::MAX_ARG_LEN length $arg > $Error::Pure::Utils::MAX_ARG_LEN
Truth table33%1/3
A and B
A= $Error::Pure::Utils::MAX_ARG_LENB= length $arg > $Error::Pure::Utils::MAX_ARG_LEN
ABresult
0X0
100
111
166 0 substr $arg, $MAX_ARG_LEN, -1, $DOTS;
167 }
168  
169 # Quote (not for numbers).
170 47 ▶ if ($arg !~ m/^-?[\d.]+$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg =~ /^-?[\d.]+$/ms) 45 2 47
171 45 $arg = "'$arg'";
172 }
173 }
174 51 $i_args = '('.(join ', ', @args).')';
175 }
176  
177 # Information to stack.
178 94 $sub =~ s/\n$//ms;
179 94 push @stack, {
180 'class' => $class,
181 'prog' => $prog,
182 'line' => $line,
183 'sub' => $sub,
184 'args' => $i_args
185 };
186 }
187  
188 # Stack.
189 61 return @stack;
190 }
191  
192 34 1;
193  
194 __END__
↑ Summary Pure.pm »