| 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.30; | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 51 | 91 | ▶ | if (! defined $msg[$i]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$tmp_level < $max_level and do {
package DB;
() = caller $tmp_level++
}
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++
}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 110 | 92 | $prog = abs_path($prog); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # Sub name. | ||||||||||||||||||||||||||||
| 114 | 94 | ▶ | if (defined $evaltext) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 115 | 2 | ▶ | if ($is_require) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$Error::Pure::Utils::MAX_EVAL and length $evaltext > $Error::Pure::Utils::MAX_EVAL
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 137 | 49 | $sub = 'err'; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | 51 | ▶ | if ($PROGRAM && $prog =~ m/^\(eval/sm) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$Error::Pure::Utils::PROGRAM and $prog =~ /^\(eval/ms
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
| |||||||||||||||||||||||||||||
| 140 | 0 | $prog = $PROGRAM; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # Args. | ||||||||||||||||||||||||||||
| 145 | 94 | my $i_args = $EMPTY_STR; | |||||||||||||||||||||||||||
| 146 | 94 | ▶ | if ($hargs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 51 | my @args = @DB::args; | |||||||||||||||||||||||||||
| 148 | 51 | ▶ | if ($MAX_ARGS && $#args > $MAX_ARGS) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$Error::Pure::Utils::MAX_ARGS and $#args > $Error::Pure::Utils::MAX_ARGS
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 156 | 6 | $arg = 'undef'; | |||||||||||||||||||||||||||
| 157 | 6 | next; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | 47 | ▶ | if (ref $arg) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$Error::Pure::Utils::MAX_ARG_LEN and length $arg > $Error::Pure::Utils::MAX_ARG_LEN
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||