File Coverage

blib/lib/D.pm
Criterion Covered Total %
statement 93 93 100.0
branch 16 18 88.8
condition 7 14 50.0
subroutine 20 20 100.0
pod 8 8 100.0
total 144 153 94.1


line stmt bran cond sub pod time code
1             package D;
2              
3 2     2   155746 use 5.008007;
  2         15  
4 2     2   10 use strict;
  2         4  
  2         37  
5 2     2   10 use warnings;
  2         3  
  2         78  
6              
7 2     2   643 use Data::Dumper;
  2         6960  
  2         102  
8 2     2   589 use Encode ();
  2         9790  
  2         46  
9 2     2   12 use Carp ();
  2         3  
  2         38  
10 2     2   9 use Scalar::Util qw(blessed refaddr);
  2         11  
  2         95  
11 2     2   11 use B;
  2         4  
  2         2038  
12              
13             require Exporter;
14              
15             our @ISA = qw(Exporter);
16              
17             our @EXPORT = qw(du dw de dn dustr dwstr destr dnstr);
18              
19             our $VERSION = '0.06';
20              
21             sub du {
22 4     4 1 5312 print STDERR dustr(@_);
23             }
24              
25             sub dustr {
26 5     5 1 2199 my ($ref_data) = @_;
27 5         11 $ref_data = _encode('UTF-8', $ref_data);
28 5         43 my $d = Data::Dumper->new([$ref_data]);
29 5         146 $d->Sortkeys(1)->Indent(1)->Terse(1);
30 5         111 my $ret = $d->Dump;
31 5         110 chomp $ret;
32 5         622 my $carp_short_message = Carp::shortmess($ret);
33              
34 5         150 return $carp_short_message;
35             }
36              
37             sub dw {
38 1     1 1 1858 print STDERR dwstr(@_);
39             }
40              
41             sub dwstr {
42 2     2 1 1847 my ($ref_data) = @_;
43 2         5 $ref_data = _encode("cp932",$ref_data);
44 2         18 my $d = Data::Dumper->new([$ref_data]);
45 2         57 $d->Sortkeys(1)->Indent(1)->Terse(1);
46 2         42 my $ret = $d->Dump;
47 2         50 chomp $ret;
48 2         217 my $carp_short_message = Carp::shortmess($ret);
49              
50 2         58 return $carp_short_message;
51             }
52              
53             sub de {
54 1     1 1 1792 print STDERR destr(@_);
55             }
56              
57             sub destr {
58 2     2 1 1784 my ($ref_data) = @_;
59 2         5 $ref_data = _encode("EUC-JP",$ref_data);
60 2         17 my $d = Data::Dumper->new([$ref_data]);
61 2         51 $d->Sortkeys(1)->Indent(1)->Terse(1);
62 2         38 my $ret = $d->Dump;
63 2         48 chomp $ret;
64 2         201 my $carp_short_message = Carp::shortmess($ret);
65              
66 2         58 return $carp_short_message;
67             }
68              
69             sub dn {
70 3     3 1 3232 print STDERR dnstr(@_);
71             }
72              
73             sub dnstr {
74 4     4 1 1922 my ($ref_data) = @_;
75 4         22 my $d = Data::Dumper->new([$ref_data]);
76 4         104 $d->Sortkeys(1)->Indent(1)->Terse(1);
77 4         75 my $ret = $d->Dump;
78 4         104 chomp $ret;
79 4         404 my $carp_short_message = Carp::shortmess($ret);
80              
81 4         109 return $carp_short_message;
82             }
83              
84             # Copy from Data::Recursive::Encode
85             our $DO_NOT_PROCESS_NUMERIC_VALUE = 0;
86             sub _apply {
87 17     17   35 my $code = shift;
88 17         22 my $seen = shift;
89            
90 17         20 my @retval;
91 17         29 for my $arg (@_) {
92 36 100       94 if(my $ref = ref $arg){
93 9         25 my $refaddr = refaddr($arg);
94 9         10 my $proto;
95            
96 9 50 66     42 if(defined($proto = $seen->{$refaddr})){
    100          
    100          
    100          
97             # noop
98             }
99             elsif($ref eq 'ARRAY'){
100 1         4 $proto = $seen->{$refaddr} = [];
101 1         4 @{$proto} = _apply($code, $seen, @{$arg});
  1         3  
  1         3  
102             }
103             elsif($ref eq 'HASH'){
104 6         13 $proto = $seen->{$refaddr} = {};
105 6         13 %{$proto} = _apply($code, $seen, %{$arg});
  6         18  
  6         37  
106             }
107             elsif($ref eq 'REF' or $ref eq 'SCALAR'){
108 1         2 $proto = $seen->{$refaddr} = \do{ my $scalar };
  1         4  
109 1         3 ${$proto} = _apply($code, $seen, ${$arg});
  1         2  
  1         4  
110             }
111             else{ # CODE, GLOB, IO, LVALUE etc.
112 1         4 $proto = $seen->{$refaddr} = $arg;
113             }
114            
115 9         22 push @retval, $proto;
116             }
117             else{
118 27 50 33     153 push @retval, defined($arg) && (! $DO_NOT_PROCESS_NUMERIC_VALUE || ! _is_number($arg)) ? $code->($arg) : $arg;
119             }
120             }
121            
122 17 100       49 return wantarray ? @retval : $retval[0];
123             }
124            
125             # Copy from Data::Recursive::Encode
126             sub _encode {
127 9     9   19 my ($encoding, $stuff, $check) = @_;
128 9   33     28 $encoding = Encode::find_encoding($encoding)
129             || Carp::croak("unknown encoding '$encoding'");
130 9   50     8175 $check ||= 0;
131 9     27   46 _apply(sub { $encoding->encode($_[0], $check) }, {}, $stuff);
  27         106  
132             }
133            
134             # Copy from Data::Recursive::Encode
135             sub _is_number {
136 12     12   616 my $value = shift;
137 12 100       31 return 0 unless defined $value;
138            
139 11         43 my $b_obj = B::svref_2object(\$value);
140 11         40 my $flags = $b_obj->FLAGS;
141 11 100 66     74 return $flags & ( B::SVp_IOK | B::SVp_NOK ) && !( $flags & B::SVp_POK ) ? 1 : 0;
142             }
143              
144             1;
145              
146             =encoding utf8
147              
148             =head1 NAME
149              
150             D - Provides utility functions to encode data and dump it to STDERR.
151              
152             =head1 SYNOPSIS
153            
154             use utf8;
155            
156             # Export du, dw, de, dn, dustr, dwstr, destr, dnstr functions
157             use D;
158            
159             # Reference data that contains decoded strings
160             my $data = [{name => 'あ'}, {name => 'い'}];
161            
162             # Encode all strings in reference data to UTF-8 and dump the reference data to STDERR.
163             du $data;
164              
165             # Encode all strings in reference data to cp932 and dump the reference data to STDERR.
166             dw $data;
167              
168             # Encode all strings in reference data to EUC-JP and dump the reference data to STDERR.
169             de $data;
170              
171             # Dump reference data to STDERR without encoding.
172             dn $data;
173              
174             # Examples of useful oneliner.
175             use D;du $data;
176             use D;dw $data;
177             use D;de $data;
178             use D;dn $data;
179              
180             # Output example of du function.
181             [
182             {
183             'name' => 'あ'
184             },
185             {
186             'name' => 'い'
187             }
188             ] at test.pl line 7.
189              
190             =head1 DESCRIPTION
191              
192             D module provides utility functions to encode data and dump it to STDERR.
193              
194             =head1 FEATURES
195              
196             =over 2
197              
198             =item * Export C, C, C, and C functions. Don't conflict debug command such as 'p' because these function names are consist of two characters.
199              
200             =item * Encode all strings in reference data in C, C, and C function.
201              
202             =item * C is a short name of "dump UTF-8"
203              
204             =item * C is a short name of "dump Windows cp932"
205              
206             =item * C is a short name of "dump EUC-JP"
207              
208             =item * C is a short name of "dump no encoding"
209              
210             =item * Use C method of L to dump data
211              
212             =item * Print line number and file name to STDERR
213              
214             =item * Keys of hash of dumped data is sorted.
215              
216             =item * Don't print "$VAR1 =" unlike L default.
217              
218             =back
219              
220             =head1 FUNCTIONS
221              
222             =head2 du
223              
224             Encode all strings in reference data to UTF-8 and return string the reference data with file name and line number.
225              
226             If the argument is not reference data such as a string, it is also dumped in the same way as reference data.
227              
228             This function is exported.
229              
230             use D;
231             my $data = [{name => 'あ'}, {name => 'い'}];
232             du $data;
233              
234             Following example is oneliner used. It can be used all functions.
235              
236             my $data = [{name => 'あ'}, {name => 'い'}];
237             use D;du $data;
238              
239             =head2 dw
240              
241             Encode all strings in reference data to cp932 and dump the reference data to STDERR with file name and line number.
242              
243             If the argument is not reference data such as a string, it is also dumped in the same way as reference data.
244              
245             This function is exported.
246              
247             use D;
248             my $data = [{name => 'あ'}, {name => 'い'}];
249             dw $data;
250              
251             =head2 de
252              
253             Encode all strings in reference data to EUC-JP and dump the reference data to STDERR with file name and line number.
254              
255             If the argument is not reference data such as a string, it is also dumped in the same way as reference data.
256              
257             This function is exported.
258              
259             use D;
260             my $data = [{name => 'あ'}, {name => 'い'}];
261             de $data;
262              
263             =head2 dn
264              
265             Dump reference data to STDERR without encoding with file name and line number.
266              
267             If the argument is not reference data such as a string, it is also dumped in the same way as reference data.
268              
269             This function is exported.
270              
271             use D;
272             my $data = [{name => 'あ'}, {name => 'い'}];
273             dn $data;
274              
275             =head2 dustr
276              
277             This function is return that UTF-8 encoded string.
278              
279             This function is exported.
280              
281             Following example is get the UTF-8 encoded string.
282              
283             use D;
284             my $data = [{name => 'あ'}, {name => 'い'}];
285             my $str = dustr $data;
286              
287             =head2 dwstr
288              
289             This function is return that cp932 encoded string.
290              
291             This function is exported.
292              
293             Following example is get the cp932 encoded string.
294              
295             use D;
296             my $data = [{name => 'あ'}, {name => 'い'}];
297             my $str = dwstr $data;
298              
299             =head2 destr
300              
301             This function is return that EUC-JP encoded string.
302              
303             This function is exported.
304              
305             Following example is get the EUC-JP encoded string.
306              
307             use D;
308             my $data = [{name => 'あ'}, {name => 'い'}];
309             my $str = destr $data;
310              
311             =head2 dnstr
312              
313             This function is return that without encoded string.
314              
315             This function is exported.
316              
317             Following example is get the without encoded string.
318              
319             use D;
320             my $data = [{name => 'あ'}, {name => 'い'}];
321             my $str = dnstr $data;
322              
323             =head1 Bug Report
324              
325             L
326              
327             =head1 SEE ALSO
328              
329             L, L, L
330              
331             =head1 AUTHOR
332              
333             Yoshiyuki Ito, Eyoshiyuki.ito.biz@gmail.comE
334              
335             Yuki Kimoto, Ekimoto.yuki@gmail.comE
336              
337             =head1 COPYRIGHT AND LICENSE
338              
339             Copyright (C) 2019 by Yoshiyuki Ito, Yuki Kimoto
340              
341             This library is free software; you can redistribute it and/or modify
342             it under the same terms as Perl itself, either Perl version 5.08.7 or,
343             at your option, any later version of Perl 5 you may have available.
344              
345             =cut