blib/lib/Data/Dumper/AutoEncode.pm

filter: Statementstmt 90.4% 95 / 105 Branchbran 77.7% 28 / 36 Conditioncond 62.5% 15 / 24 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 100% 18 / 18 Podpod 100% 2 / 2 totaltotal 82.4% 164 / 199 CC 31 SCAR 20.0
CC
31
Cov
84%
_apply
19.5
_can_exec
7.2
import
6.0
CRAP 7.4
↑ Summary
1 package Data::Dumper::AutoEncode;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Scalar::Util qw(blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Data::Dumper; # Dumper
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our $VERSION = '1.00';
11  
12 5 our $ENCODING = '';
13 5 our $CHECK_ALREADY_ENCODED = 0;
14 5 our $DO_NOT_PROCESS_NUMERIC_VALUE = 1;
15 5 our $FLAG_STR = '';
16  
17 5 our $BEFORE_HOOK;
18 5 our $AFTER_HOOK;
19  
20 5 ▶sub import {
Subroutine import: called CC=6 SCAR=17.9
21 5 my $class = shift;
22 5 my %args = map { $_ => 1 } @_;
23  
24 5 ▶ if (delete $args{'-dumper'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $args{"-dumper"}) 1 4 5
25 5 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 *{__PACKAGE__."::Dumper"} = *{__PACKAGE__."::eDumper"};
27 }
28  
29 5 my $export_all = !!(scalar(keys %args) == 0);
30  
31 5 my $pkg = caller;
32  
33 5 for my $f (qw/ Dumper eDumper /) {
34 10 ▶ if ( $export_all || (exists $args{$f} && $args{$f}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($export_all or exists $args{$f} and $args{$f}) 9 1 10
Condition83%5/6
$export_all or exists $args{$f} and $args{$f}
l!l&&r!l&&!r
811
exists $args{$f} and $args{$f}
!ll&&!rl&&r
101
MC/DC66%2/3
$export_all or exists $args{$f} and $args{$f}
$export_all exists $args{$f} $args{$f}
Truth table75%3/4
A or B and C
A= $export_allB= exists $args{$f}C= $args{$f}
ABCresult
1XX1
0111
00X0
0100
35 5 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
36 9 *{"${pkg}::${f}"} = \&{$f};
37 }
38 }
39 }
40  
41 12 ▶sub _dump {
Subroutine _dump: called CC=1 SCAR=0.0
42 12 my $d = Data::Dumper->new(\@_);
43 12 return $d->Dump;
44 }
45  
46 12 ▶sub eDumper {
Subroutine eDumper: called CC=3 SCAR=11.0
Pod documented
47 12 my @args;
48 12 for my $arg (@_) {
49 13 ▶ push @args, encode($ENCODING || 'utf8', $arg);
Condition100%2/2
$Data::Dumper::AutoEncode::ENCODING || 'utf8'
l!l
112
MC/DC100%1/1
$Data::Dumper::AutoEncode::ENCODING || 'utf8'
$Data::Dumper::AutoEncode::ENCODING
Truth table100%2/2
A
A= $Data::Dumper::AutoEncode::ENCODING
Aresult
11
00
50 }
51 12 _dump(@args);
52 }
53  
54 13 ▶sub encode {
Subroutine encode: called CC=3 SCAR=11.3
Pod documented
55 13 my ($encoding, $stuff, $check) = @_;
56 13 ▶ $encoding = Encode::find_encoding($encoding)
Condition50%1/2
&Encode::find_encoding($encoding) || Carp::croak("unknown encoding '${encoding}'")
l!l
130
MC/DC0%0/1
&Encode::find_encoding($encoding) || Carp::croak("unknown encoding '${encoding}'")
&Encode::find_encoding($encoding)
Truth table50%1/2
A
A= &Encode::find_encoding($encoding)
Aresult
11
00
57 || Carp::croak("unknown encoding '$encoding'");
58 13 ▶ $check ||= 0;
Condition50%1/2
$check ||= 0
l!l
013
MC/DC0%0/1
$check ||= 0
$check
Truth table50%1/2
A
A= $check
Aresult
11
00
59 13 ▶ _apply(sub { $encoding->encode($_[0], $check) }, {}, $stuff);
Subroutine __ANON__: called CC=1 SCAR=0.0
60 }
61  
62 # copied from Data::Recursive::Encode
63 15 ▶sub _apply {
Subroutine _apply: called CC=11 SCAR=29.7
64 15 my $code = shift;
65 15 my $seen = shift;
66  
67 15 my @retval;
68 15 for my $arg (@_) {
69 17 ▶ if(my $ref = ref $arg){
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $arg) { } 2 15 17
70 2 my $refaddr = refaddr($arg);
71 2 my $proto;
72  
73 2 ▶ if(defined($proto = $seen->{$refaddr})){
Branch: 4 branches
BranchTrueFalseTotal
if (defined($proto = $seen->{$refaddr})) { } 0 2 2
elsif ($ref eq 'ARRAY') { } 0 2 2
elsif ($ref eq 'HASH') { } 2 0 2
elsif ($ref eq 'REF' or $ref eq 'SCALAR') { } 0 0 0
74 # noop
75 }
76 elsif($ref eq 'ARRAY'){
77 0 $proto = $seen->{$refaddr} = [];
78 0 @{$proto} = _apply($code, $seen, @{$arg});
79 }
80 elsif($ref eq 'HASH'){
81 2 $proto = $seen->{$refaddr} = {};
82 2 %{$proto} = _apply($code, $seen, %{$arg});
83 }
84 ▶ elsif($ref eq 'REF' or $ref eq 'SCALAR'){
Condition0%0/3
$ref eq "REF" or $ref eq "SCALAR"
l!l&&r!l&&!r
000
MC/DC0%0/2
$ref eq "REF" or $ref eq "SCALAR"
$ref eq "REF" $ref eq "SCALAR"
Truth table0%0/3
A or B
A= $ref eq "REF"B= $ref eq "SCALAR"
ABresult
1X1
011
000
85 0 $proto = $seen->{$refaddr} = \do{ my $scalar };
86 0 ${$proto} = _apply($code, $seen, ${$arg});
87 }
88 else{ # CODE, GLOB, IO, LVALUE etc.
89 0 $proto = $seen->{$refaddr} = $arg;
90 }
91  
92 2 push @retval, $proto;
93 }
94 else{
95 15 ▶ if (_can_exec($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_can_exec($arg)) { } 12 3 15
96 12 ▶ push @retval, $FLAG_STR ? $FLAG_STR . _exec($code, $arg) : _exec($code, $arg);
Branch: 1 branch
BranchTrueFalseTotal
$Data::Dumper::AutoEncode::FLAG_STR ? : 1 11 12
97 }
98 else {
99 3 push @retval, $arg;
100 }
101 }
102 }
103  
104 15 ▶ return wantarray ? @retval : $retval[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 15 0 15
105 }
106  
107 12 ▶sub _exec {
Subroutine _exec: called CC=3 SCAR=11.0
108 12 my ($code, $arg) = @_;
109  
110 12 ▶ if (ref $BEFORE_HOOK eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $Data::Dumper::AutoEncode::BEFORE_HOOK eq "CODE") 1 11 12
111 1 $arg = $BEFORE_HOOK->($arg);
112 }
113  
114 12 my $result = $code->($arg);
115  
116 12 ▶ if (ref $AFTER_HOOK eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $Data::Dumper::AutoEncode::AFTER_HOOK eq "CODE") 1 11 12
117 1 return $AFTER_HOOK->($result);
118 }
119  
120 11 return $result;
121 }
122  
123 # copied from Data::Recursive::Encode
124 15 ▶sub _is_number {
Subroutine _is_number: called CC=4 SCAR=14.0
125 15 my $value = shift;
126 15 ▶ return 0 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 15 15
127  
128 15 my $b_obj = B::svref_2object(\$value);
129 15 my $flags = $b_obj->FLAGS;
130 15 ▶ return $flags & ( B::SVp_IOK | B::SVp_NOK ) && !( $flags & B::SVp_POK ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$flags & 12288 && !($flags & 16384) ? : 1 14 15
Condition66%2/3
$flags & 12288 and not $flags & 16384
!ll&&!rl&&r
1401
MC/DC50%1/2
$flags & 12288 and not $flags & 16384
$flags & 12288 not $flags & 16384
Truth table66%2/3
A and not(B)
A= $flags & 12288B= not $flags & 16384
ABresult
0X0
100
111
131 }
132  
133 15 ▶sub _can_exec {
Subroutine _can_exec: called CC=7 SCAR=19.7
134 15 my ($arg) = @_;
135  
136 15 ▶ return unless defined($arg);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 0 15 15
137 15 ▶ return if $DO_NOT_PROCESS_NUMERIC_VALUE && _is_number($arg);
Branch: 1 branch
BranchTrueFalseTotal
if $Data::Dumper::AutoEncode::DO_NOT_PROCESS_NUMERIC_VALUE and _is_number($arg) 1 14 15
Condition66%2/3
$Data::Dumper::AutoEncode::DO_NOT_PROCESS_NUMERIC_VALUE and _is_number($arg)
!ll&&!rl&&r
0141
MC/DC50%1/2
$Data::Dumper::AutoEncode::DO_NOT_PROCESS_NUMERIC_VALUE and _is_number($arg)
$Data::Dumper::AutoEncode::DO_NOT_PROCESS_NUMERIC_VALUE _is_number($arg)
Truth table66%2/3
A and B
A= $Data::Dumper::AutoEncode::DO_NOT_PROCESS_NUMERIC_VALUEB= _is_number($arg)
ABresult
0X0
100
111
138 14 ▶ return 1 if Encode::is_utf8($arg);
Branch: 1 branch
BranchTrueFalseTotal
if &Encode::is_utf8($arg) 11 3 14
139 3 ▶ return 1 if !$CHECK_ALREADY_ENCODED && !Encode::is_utf8($arg);
Branch: 1 branch
BranchTrueFalseTotal
unless $Data::Dumper::AutoEncode::CHECK_ALREADY_ENCODED or &Encode::is_utf8($arg) 1 2 3
Condition66%2/3
$Data::Dumper::AutoEncode::CHECK_ALREADY_ENCODED or &Encode::is_utf8($arg)
l!l&&r!l&&!r
201
MC/DC50%1/2
$Data::Dumper::AutoEncode::CHECK_ALREADY_ENCODED or &Encode::is_utf8($arg)
$Data::Dumper::AutoEncode::CHECK_ALREADY_ENCODED &Encode::is_utf8($arg)
Truth table66%2/3
A or B
A= $Data::Dumper::AutoEncode::CHECK_ALREADY_ENCODEDB= &Encode::is_utf8($arg)
ABresult
1X1
011
000
140  
141 2 return;
142 }
143  
144 5 1;
145  
146 __END__
↑ Summary