blib/lib/D.pm

filter: Statementstmt 94.2% 82 / 87 Branchbran 66.6% 12 / 18 Conditioncond 35.2% 6 / 17 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 93.3% 14 / 15 Podpod 100% 3 / 3 totaltotal 78.6% 118 / 150 CC 17 SCAR 20.9
CC
17
Cov
82%
_is_number
20.0
_apply
12.4
_encode
3.2
CRAP 8.1
↑ Summary
1 package D;
2  
3 2 ▶use 5.008007;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Scalar::Util qw(blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 require Exporter;
14  
15 2 our @ISA = qw(Exporter);
16  
17 2 our @EXPORT = qw(du dw dn);
18  
19 2 our $VERSION = '0.02';
20  
21 sub du {
22 4 ▶ my ($ref_data) = @_;
Subroutine du: called CC=1 SCAR=0.0
Pod documented
23 4 $ref_data = _encode('UTF-8', $ref_data);
24 4 my $d = Data::Dumper->new([$ref_data]);
25 4 $d->Sortkeys(1)->Indent(1)->Terse(1);
26 4 my $ret = $d->Dump;
27 4 chomp $ret;
28 4 my $carp_short_message = Carp::shortmess($ret);
29 4 print STDERR $carp_short_message;
30 }
31  
32 sub dw {
33 1 ▶ my ($ref_data) = @_;
Subroutine dw: called CC=1 SCAR=0.0
Pod documented
34 1 $ref_data = _encode("cp932",$ref_data);
35 1 my $d = Data::Dumper->new([$ref_data]);
36 1 $d->Sortkeys(1)->Indent(1)->Terse(1);
37 1 my $ret = $d->Dump;
38 1 chomp $ret;
39 1 my $carp_short_message = Carp::shortmess($ret);
40 1 print STDERR $carp_short_message;
41 }
42  
43 sub dn {
44 3 ▶ my ($ref_data) = @_;
Subroutine dn: called CC=1 SCAR=0.0
Pod documented
45 3 my $d = Data::Dumper->new([$ref_data]);
46 3 $d->Sortkeys(1)->Indent(1)->Terse(1);
47 3 my $ret = $d->Dump;
48 3 chomp $ret;
49 3 my $carp_short_message = Carp::shortmess($ret);
50 3 print STDERR $carp_short_message;
51 }
52  
53 # Copy from Data::Recursive::Encode
54 2 our $DO_NOT_PROCESS_NUMERIC_VALUE = 0;
55 sub _apply {
56 9 ▶ my $code = shift;
Subroutine _apply: called CC=12 SCAR=25.2
57 9 my $seen = shift;
58
59 9 my @retval;
60 9 for my $arg (@_) {
61 16 ▶ if(my $ref = ref $arg){
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $arg) { } 5 11 16
62 5 my $refaddr = refaddr($arg);
63 5 my $proto;
64
65 5 ▶ if(defined($proto = $seen->{$refaddr})){
Branch: 4 branches
BranchTrueFalseTotal
if (defined($proto = $seen->{$refaddr})) { } 0 5 5
elsif ($ref eq 'ARRAY') { } 1 4 5
elsif ($ref eq 'HASH') { } 2 2 4
elsif ($ref eq 'REF' or $ref eq 'SCALAR') { } 1 1 2
66 # noop
67 }
68 elsif($ref eq 'ARRAY'){
69 1 $proto = $seen->{$refaddr} = [];
70 1 @{$proto} = _apply($code, $seen, @{$arg});
71 }
72 elsif($ref eq 'HASH'){
73 2 $proto = $seen->{$refaddr} = {};
74 2 %{$proto} = _apply($code, $seen, %{$arg});
75 }
76 ▶ elsif($ref eq 'REF' or $ref eq 'SCALAR'){
Condition66%2/3
$ref eq "REF" or $ref eq "SCALAR"
l!l&&r!l&&!r
011
MC/DC50%1/2
$ref eq "REF" or $ref eq "SCALAR"
$ref eq "REF" $ref eq "SCALAR"
Truth table66%2/3
A or B
A= $ref eq "REF"B= $ref eq "SCALAR"
ABresult
1X1
011
000
77 1 $proto = $seen->{$refaddr} = \do{ my $scalar };
78 1 ${$proto} = _apply($code, $seen, ${$arg});
79 }
80 else{ # CODE, GLOB, IO, LVALUE etc.
81 1 $proto = $seen->{$refaddr} = $arg;
82 }
83
84 5 push @retval, $proto;
85 }
86 else{
87 11 ▶ push @retval, defined($arg) && (! $DO_NOT_PROCESS_NUMERIC_VALUE || ! _is_number($arg)) ? $code->($arg) : $arg;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg && !($D::DO_NOT_PROCESS_NUMERIC_VALUE && _is_number($arg)) ? : 11 0 11
Condition33%2/6
defined $arg and not $D::DO_NOT_PROCESS_NUMERIC_VALUE && _is_number($arg)
!ll&&!rl&&r
0011
$D::DO_NOT_PROCESS_NUMERIC_VALUE && _is_number($arg)
!ll&&!rl&&r
1100
MC/DC0%0/3
defined $arg and not $D::DO_NOT_PROCESS_NUMERIC_VALUE && _is_number($arg)
defined $arg $D::DO_NOT_PROCESS_NUMERIC_VALUE _is_number($arg)
Truth table25%1/4
A and not(B && C)
A= defined $argB= $D::DO_NOT_PROCESS_NUMERIC_VALUEC= _is_number($arg)
ABCresult
0XX0
1110
10X1
1101
88 }
89 }
90
91 9 ▶ return wantarray ? @retval : $retval[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 6 9
92 }
93
94 # Copy from Data::Recursive::Encode
95 sub _encode {
96 5 ▶ my ($encoding, $stuff, $check) = @_;
Subroutine _encode: called CC=3 SCAR=11.8
97 5 ▶ $encoding = Encode::find_encoding($encoding)
Condition33%1/3
&Encode::find_encoding($encoding) || Carp::croak("unknown encoding '${encoding}'")
l!l&&r!l&&!r
500
MC/DC0%0/2
&Encode::find_encoding($encoding) || Carp::croak("unknown encoding '${encoding}'")
&Encode::find_encoding($encoding) Carp::croak("unknown encoding '${encoding}'")
Truth table33%1/3
A || B
A= &Encode::find_encoding($encoding)B= Carp::croak("unknown encoding '${encoding}'")
ABresult
1X1
011
000
98 || Carp::croak("unknown encoding '$encoding'");
99 5 ▶ $check ||= 0;
Condition50%1/2
$check ||= 0
l!l
05
MC/DC0%0/1
$check ||= 0
$check
Truth table50%1/2
A
A= $check
Aresult
11
00
100 5 ▶ _apply(sub { $encoding->encode($_[0], $check) }, {}, $stuff);
Subroutine __ANON__: called CC=1 SCAR=0.0
101 }
102
103 # Copy from Data::Recursive::Encode
104 sub _is_number {
105 0 ▶ my $value = shift;
Subroutine _is_number: not called CC=4 SCAR=30.0
106 0 ▶ return 0 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
107
108 0 my $b_obj = B::svref_2object(\$value);
109 0 my $flags = $b_obj->FLAGS;
110 0 ▶ return $flags & ( B::SVp_IOK | B::SVp_NOK ) && !( $flags & B::SVp_POK ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$flags & 12288 && !($flags & 16384) ? : 0 0 0
Condition0%0/3
$flags & 12288 and not $flags & 16384
!ll&&!rl&&r
000
MC/DC0%0/2
$flags & 12288 and not $flags & 16384
$flags & 12288 not $flags & 16384
Truth table0%0/3
A and not(B)
A= $flags & 12288B= not $flags & 16384
ABresult
0X0
100
111
111 }
112  
113 2 1;
114  
115 =encoding utf8
116  
117 =head1 NAME
118  
119 D - Provides utility functions to encode data and dump it to STDERR.
120  
121 =head1 SYNOPSIS
122
123 use utf8;
124
125 # Export du and dw functions
126 use D;
127
128 # Reference data that contains decoded strings
129 my $data = [{name => 'あ'}, {name => 'い'}];
130
131 # Encode all strings in reference data to UTF-8 and dump the reference data to STDERR
132 du $data;
133
134 # Encode all strings in reference data to cp932 and dump the reference data.
135 dw $data;
136  
137 # Dump reference data to STDERR without encoding.
138 dn $data;
139  
140 =head1 DESCRIPTION
141  
142 D module provides utility functions to encode data and dump it to STDERR.
143  
144 =head1 FEATURES
145  
146 =over 2
147  
148 =item * Export C<du> and C<dw> and C<dn> functions. Don't conflict debug command such as 'p' because these function names are consist of two characters.
149  
150 =item * Encode all strings in reference data in C<du> and C<dw> function.
151  
152 =item * C<du> is a short name of "dump UTF-8"
153  
154 =item * C<dw> is a short name of "dump Windows cp932"
155  
156 =item * C<dn> is a short name of "dump no encoding"
157  
158 =item * Onliner is useful. "useD;du $data;" or "useD;dw $data;" or "useD;dn $data;"
159  
160 =item * Use C<Dump> method of L<Data::Dumper> to dump data
161  
162 =item * Print line number and file name to STDERR
163  
164 =item * Keys of hash of dumped data is sorted.
165  
166 =item * Don't print "$VAR1 =" unlike L<Data::Dumper> default.
167  
168 =back
169  
170 =head1 EXPORT
171  
172 Export C<du> and C<dw> and C<dn> functions.
173  
174 =head1 FUNCTIONS
175  
176 =head2 du
177  
178 du $data;
179  
180 Encode all strings in reference data to UTF-8 and dump the reference data to STDERR with file name and line number.
181  
182 If the argument is not reference data such as a string, it is also dumped in the same way as reference data.
183  
184 =head2 dw
185  
186 Encode all strings in reference data to cp932 and dump the reference data to STDERR with file name and line number.
187  
188 If the argument is not reference data such as a string, it is also dumped in the same way as reference data.
189  
190 =head2 dn
191  
192 Dump reference data to STDERR without encoding with file name and line number.
193  
194 If the argument is not reference data such as a string, it is also dumped in the same way as reference data.
195  
196 =head1 Bug Report
197  
198 L<https://github.com/YoshiyukiItoh/D>
199  
200 =head1 SEE ALSO
201  
202 L<Data::Dumper>, L<Carp>, L<Data::Recursive::Encode>
203  
204 =head1 AUTHOR
205  
206 yoshiyuki ito, E<lt>yoshiyuki.ito.biz@gmail.comE<gt>
207  
208 Yuki Kimoto, E<lt>kimoto.yuki@gmail.comE<gt>
209  
210 =head1 COPYRIGHT AND LICENSE
211  
212 Copyright (C) 2019 by yoshi, Yuki Kimoto
213  
214 This library is free software; you can redistribute it and/or modify
215 it under the same terms as Perl itself, either Perl version 5.08.7 or,
216 at your option, any later version of Perl 5 you may have available.
217  
218 =cut
↑ Summary