File Coverage

blib/lib/D.pm
Criterion Covered Total %
statement 84 84 100.0
branch 16 18 88.8
condition 7 14 50.0
subroutine 18 18 100.0
pod 6 6 100.0
total 131 140 93.5


line stmt bran cond sub pod time code
1             package D;
2              
3 3     3   228072 use 5.008007;
  3         41  
4 3     3   16 use strict;
  3         6  
  3         69  
5 3     3   21 use warnings;
  3         6  
  3         86  
6              
7 3     3   1311 use Data::Dumper;
  3         13689  
  3         176  
8 3     3   1113 use Encode ();
  3         19130  
  3         58  
9 3     3   18 use Carp ();
  3         4  
  3         64  
10 3     3   14 use Scalar::Util qw(blessed refaddr);
  3         6  
  3         168  
11 3     3   19 use B;
  3         5  
  3         2688  
12              
13             require Exporter;
14              
15             our @ISA = qw(Exporter);
16              
17             our @EXPORT = qw(du dw dn dustr dwstr dnstr);
18              
19             our $VERSION = '0.04';
20              
21             sub du {
22 4     4 1 5441 print STDERR dustr(@_);
23             }
24              
25             sub dustr {
26 5     5 1 2217 my ($ref_data) = @_;
27 5         13 $ref_data = _encode('UTF-8', $ref_data);
28 5         45 my $d = Data::Dumper->new([$ref_data]);
29 5         151 $d->Sortkeys(1)->Indent(1)->Terse(1);
30 5         121 my $ret = $d->Dump;
31 5         118 chomp $ret;
32 5         659 my $carp_short_message = Carp::shortmess($ret);
33              
34 5         165 return $carp_short_message;
35             }
36              
37             sub dw {
38 1     1 1 1894 print STDERR dwstr(@_);
39             }
40              
41             sub dwstr {
42 2     2 1 1893 my ($ref_data) = @_;
43 2         5 $ref_data = _encode("cp932",$ref_data);
44 2         17 my $d = Data::Dumper->new([$ref_data]);
45 2         64 $d->Sortkeys(1)->Indent(1)->Terse(1);
46 2         42 my $ret = $d->Dump;
47 2         53 chomp $ret;
48 2         253 my $carp_short_message = Carp::shortmess($ret);
49              
50 2         61 return $carp_short_message;
51             }
52              
53             sub dn {
54 3     3 1 3182 print STDERR dnstr(@_);
55             }
56              
57             sub dnstr {
58 4     4 1 1939 my ($ref_data) = @_;
59 4         22 my $d = Data::Dumper->new([$ref_data]);
60 4         107 $d->Sortkeys(1)->Indent(1)->Terse(1);
61 4         74 my $ret = $d->Dump;
62 4         93 chomp $ret;
63 4         393 my $carp_short_message = Carp::shortmess($ret);
64              
65 4         113 return $carp_short_message;
66             }
67              
68             # Copy from Data::Recursive::Encode
69             our $DO_NOT_PROCESS_NUMERIC_VALUE = 0;
70             sub _apply {
71 13     13   34 my $code = shift;
72 13         16 my $seen = shift;
73            
74 13         16 my @retval;
75 13         23 for my $arg (@_) {
76 26 100       51 if(my $ref = ref $arg){
77 7         17 my $refaddr = refaddr($arg);
78 7         10 my $proto;
79            
80 7 50 66     36 if(defined($proto = $seen->{$refaddr})){
    100          
    100          
    100          
81             # noop
82             }
83             elsif($ref eq 'ARRAY'){
84 1         3 $proto = $seen->{$refaddr} = [];
85 1         4 @{$proto} = _apply($code, $seen, @{$arg});
  1         4  
  1         3  
86             }
87             elsif($ref eq 'HASH'){
88 4         12 $proto = $seen->{$refaddr} = {};
89 4         6 %{$proto} = _apply($code, $seen, %{$arg});
  4         10  
  4         18  
90             }
91             elsif($ref eq 'REF' or $ref eq 'SCALAR'){
92 1         3 $proto = $seen->{$refaddr} = \do{ my $scalar };
  1         4  
93 1         2 ${$proto} = _apply($code, $seen, ${$arg});
  1         2  
  1         4  
94             }
95             else{ # CODE, GLOB, IO, LVALUE etc.
96 1         7 $proto = $seen->{$refaddr} = $arg;
97             }
98            
99 7         20 push @retval, $proto;
100             }
101             else{
102 19 50 33     75 push @retval, defined($arg) && (! $DO_NOT_PROCESS_NUMERIC_VALUE || ! _is_number($arg)) ? $code->($arg) : $arg;
103             }
104             }
105            
106 13 100       38 return wantarray ? @retval : $retval[0];
107             }
108            
109             # Copy from Data::Recursive::Encode
110             sub _encode {
111 7     7   16 my ($encoding, $stuff, $check) = @_;
112 7   33     23 $encoding = Encode::find_encoding($encoding)
113             || Carp::croak("unknown encoding '$encoding'");
114 7   50     8153 $check ||= 0;
115 7     19   36 _apply(sub { $encoding->encode($_[0], $check) }, {}, $stuff);
  19         78  
116             }
117            
118             # Copy from Data::Recursive::Encode
119             sub _is_number {
120 12     12   130 my $value = shift;
121 12 100       32 return 0 unless defined $value;
122            
123 11         42 my $b_obj = B::svref_2object(\$value);
124 11         36 my $flags = $b_obj->FLAGS;
125 11 100 66     81 return $flags & ( B::SVp_IOK | B::SVp_NOK ) && !( $flags & B::SVp_POK ) ? 1 : 0;
126             }
127              
128             1;
129              
130             =encoding utf8
131              
132             =head1 NAME
133              
134             D - Provides utility functions to encode data and dump it to STDERR.
135              
136             =head1 SYNOPSIS
137            
138             use utf8;
139            
140             # Export du, dw, dn, dustr, dwstr, dnstr functions
141             use D;
142            
143             # Reference data that contains decoded strings
144             my $data = [{name => 'あ'}, {name => 'い'}];
145            
146             # Encode all strings in reference data to UTF-8 and return string the reference data.
147             my $str = dustr $data;
148              
149             # Encode all strings in reference data to cp932 and return string the reference data.
150             my $str = dwstr $data;
151              
152             # Return string the reference data to without encoding.
153             my $str = dnstr $data;
154              
155             # Dump the result of dustr function to STDERR.
156             du $data;
157            
158             # Dump the result of dwstr function to STDERR.
159             dw $data;
160              
161             # Dump the result of dnstr function to STDERR.
162             dn $data;
163              
164             =head1 DESCRIPTION
165              
166             D module provides utility functions to encode data and dump it to STDERR.
167              
168             =head1 FEATURES
169              
170             =over 2
171              
172             =item * Export C and C and C and C and C and C functions. Don't conflict debug command such as 'p' because these function names are consist of two characters.
173              
174             =item * Encode all strings in reference data in C and C function.
175              
176             =item * C is a short name of "dump UTF-8"
177              
178             =item * C is a short name of "dump Windows cp932"
179              
180             =item * C is a short name of "dump no encoding"
181              
182             =item * Onliner is useful. "useD;du $data;" or "useD;dw $data;" or "useD;dn $data;"
183              
184             =item * Use C method of L to dump data
185              
186             =item * Print line number and file name to STDERR
187              
188             =item * Keys of hash of dumped data is sorted.
189              
190             =item * Don't print "$VAR1 =" unlike L default.
191              
192             =back
193              
194             =head1 EXPORT
195              
196             Export C and C and C and C and C and C functions.
197              
198             =head1 FUNCTIONS
199              
200             =head2 dustr
201              
202             Encode all strings in reference data to UTF-8 and return string the reference data with file name and line number.
203              
204             If the argument is not reference data such as a string, it is also dumped in the same way as reference data.
205              
206             =head2 du
207              
208             Dump the result of dustr function to STDERR.
209              
210             =head2 dwstr
211              
212             Encode all strings in reference data to cp932 and dump the reference data to STDERR with file name and line number.
213              
214             If the argument is not reference data such as a string, it is also dumped in the same way as reference data.
215              
216             =head2 dw
217              
218             Dump the result of dwstr function to STDERR.
219              
220             =head2 dnstr
221              
222             Dump reference data to STDERR without encoding with file name and line number.
223              
224             If the argument is not reference data such as a string, it is also dumped in the same way as reference data.
225              
226             =head2 dn
227              
228             Dump the result of dnstr function to STDERR.
229              
230             =head1 Bug Report
231              
232             L
233              
234             =head1 SEE ALSO
235              
236             L, L, L
237              
238             =head1 AUTHOR
239              
240             Yoshiyuki Ito, Eyoshiyuki.ito.biz@gmail.comE
241              
242             Yuki Kimoto, Ekimoto.yuki@gmail.comE
243              
244             =head1 COPYRIGHT AND LICENSE
245              
246             Copyright (C) 2019 by Yoshiyuki Ito, Yuki Kimoto
247              
248             This library is free software; you can redistribute it and/or modify
249             it under the same terms as Perl itself, either Perl version 5.08.7 or,
250             at your option, any later version of Perl 5 you may have available.
251              
252             =cut