line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package D; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
163518
|
use 5.008007; |
|
2
|
|
|
|
|
16
|
|
4
|
2
|
|
|
2
|
|
12
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
55
|
|
5
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
69
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
646
|
use Data::Dumper; |
|
2
|
|
|
|
|
6968
|
|
|
2
|
|
|
|
|
118
|
|
8
|
2
|
|
|
2
|
|
592
|
use Encode (); |
|
2
|
|
|
|
|
9806
|
|
|
2
|
|
|
|
|
38
|
|
9
|
2
|
|
|
2
|
|
12
|
use Carp (); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
52
|
|
10
|
2
|
|
|
2
|
|
11
|
use Scalar::Util qw(blessed refaddr); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
122
|
|
11
|
2
|
|
|
2
|
|
13
|
use B; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1938
|
|
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.05'; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub du { |
22
|
4
|
|
|
4
|
1
|
5546
|
print STDERR dustr(@_); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub dustr { |
26
|
5
|
|
|
5
|
1
|
2229
|
my ($ref_data) = @_; |
27
|
5
|
|
|
|
|
10
|
$ref_data = _encode('UTF-8', $ref_data); |
28
|
5
|
|
|
|
|
45
|
my $d = Data::Dumper->new([$ref_data]); |
29
|
5
|
|
|
|
|
152
|
$d->Sortkeys(1)->Indent(1)->Terse(1); |
30
|
5
|
|
|
|
|
109
|
my $ret = $d->Dump; |
31
|
5
|
|
|
|
|
113
|
chomp $ret; |
32
|
5
|
|
|
|
|
656
|
my $carp_short_message = Carp::shortmess($ret); |
33
|
|
|
|
|
|
|
|
34
|
5
|
|
|
|
|
156
|
return $carp_short_message; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub dw { |
38
|
1
|
|
|
1
|
1
|
1853
|
print STDERR dwstr(@_); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub dwstr { |
42
|
2
|
|
|
2
|
1
|
2056
|
my ($ref_data) = @_; |
43
|
2
|
|
|
|
|
6
|
$ref_data = _encode("cp932",$ref_data); |
44
|
2
|
|
|
|
|
20
|
my $d = Data::Dumper->new([$ref_data]); |
45
|
2
|
|
|
|
|
113
|
$d->Sortkeys(1)->Indent(1)->Terse(1); |
46
|
2
|
|
|
|
|
43
|
my $ret = $d->Dump; |
47
|
2
|
|
|
|
|
64
|
chomp $ret; |
48
|
2
|
|
|
|
|
276
|
my $carp_short_message = Carp::shortmess($ret); |
49
|
|
|
|
|
|
|
|
50
|
2
|
|
|
|
|
64
|
return $carp_short_message; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub dn { |
54
|
3
|
|
|
3
|
1
|
3389
|
print STDERR dnstr(@_); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub dnstr { |
58
|
4
|
|
|
4
|
1
|
1982
|
my ($ref_data) = @_; |
59
|
4
|
|
|
|
|
24
|
my $d = Data::Dumper->new([$ref_data]); |
60
|
4
|
|
|
|
|
105
|
$d->Sortkeys(1)->Indent(1)->Terse(1); |
61
|
4
|
|
|
|
|
74
|
my $ret = $d->Dump; |
62
|
4
|
|
|
|
|
94
|
chomp $ret; |
63
|
4
|
|
|
|
|
419
|
my $carp_short_message = Carp::shortmess($ret); |
64
|
|
|
|
|
|
|
|
65
|
4
|
|
|
|
|
115
|
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
|
|
32
|
my $code = shift; |
72
|
13
|
|
|
|
|
16
|
my $seen = shift; |
73
|
|
|
|
|
|
|
|
74
|
13
|
|
|
|
|
16
|
my @retval; |
75
|
13
|
|
|
|
|
23
|
for my $arg (@_) { |
76
|
26
|
100
|
|
|
|
62
|
if(my $ref = ref $arg){ |
77
|
7
|
|
|
|
|
23
|
my $refaddr = refaddr($arg); |
78
|
7
|
|
|
|
|
11
|
my $proto; |
79
|
|
|
|
|
|
|
|
80
|
7
|
50
|
66
|
|
|
39
|
if(defined($proto = $seen->{$refaddr})){ |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# noop |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
elsif($ref eq 'ARRAY'){ |
84
|
1
|
|
|
|
|
4
|
$proto = $seen->{$refaddr} = []; |
85
|
1
|
|
|
|
|
3
|
@{$proto} = _apply($code, $seen, @{$arg}); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
4
|
|
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
elsif($ref eq 'HASH'){ |
88
|
4
|
|
|
|
|
13
|
$proto = $seen->{$refaddr} = {}; |
89
|
4
|
|
|
|
|
8
|
%{$proto} = _apply($code, $seen, %{$arg}); |
|
4
|
|
|
|
|
12
|
|
|
4
|
|
|
|
|
19
|
|
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
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
4
|
|
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
else{ # CODE, GLOB, IO, LVALUE etc. |
96
|
1
|
|
|
|
|
2
|
$proto = $seen->{$refaddr} = $arg; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
7
|
|
|
|
|
17
|
push @retval, $proto; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
else{ |
102
|
19
|
50
|
33
|
|
|
70
|
push @retval, defined($arg) && (! $DO_NOT_PROCESS_NUMERIC_VALUE || ! _is_number($arg)) ? $code->($arg) : $arg; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
13
|
100
|
|
|
|
36
|
return wantarray ? @retval : $retval[0]; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# Copy from Data::Recursive::Encode |
110
|
|
|
|
|
|
|
sub _encode { |
111
|
7
|
|
|
7
|
|
15
|
my ($encoding, $stuff, $check) = @_; |
112
|
7
|
|
33
|
|
|
28
|
$encoding = Encode::find_encoding($encoding) |
113
|
|
|
|
|
|
|
|| Carp::croak("unknown encoding '$encoding'"); |
114
|
7
|
|
50
|
|
|
8635
|
$check ||= 0; |
115
|
7
|
|
|
19
|
|
40
|
_apply(sub { $encoding->encode($_[0], $check) }, {}, $stuff); |
|
19
|
|
|
|
|
77
|
|
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
# Copy from Data::Recursive::Encode |
119
|
|
|
|
|
|
|
sub _is_number { |
120
|
12
|
|
|
12
|
|
648
|
my $value = shift; |
121
|
12
|
100
|
|
|
|
30
|
return 0 unless defined $value; |
122
|
|
|
|
|
|
|
|
123
|
11
|
|
|
|
|
46
|
my $b_obj = B::svref_2object(\$value); |
124
|
11
|
|
|
|
|
47
|
my $flags = $b_obj->FLAGS; |
125
|
11
|
100
|
66
|
|
|
83
|
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 dump the reference data to STDERR. |
147
|
|
|
|
|
|
|
du $data; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
# Encode all strings in reference data to cp932 and dump the reference data to STDERR. |
150
|
|
|
|
|
|
|
dw $data; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
# Dump reference data to STDERR without encoding. |
153
|
|
|
|
|
|
|
dn $data; |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
# Examples of useful oneliner. |
156
|
|
|
|
|
|
|
use D;du $data; |
157
|
|
|
|
|
|
|
use D;dw $data; |
158
|
|
|
|
|
|
|
use D;dn $data; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
# Output example of du function. |
161
|
|
|
|
|
|
|
[ |
162
|
|
|
|
|
|
|
{ |
163
|
|
|
|
|
|
|
'name' => 'あ' |
164
|
|
|
|
|
|
|
}, |
165
|
|
|
|
|
|
|
{ |
166
|
|
|
|
|
|
|
'name' => 'い' |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
] at test.pl line 7. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 DESCRIPTION |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
D module provides utility functions to encode data and dump it to STDERR. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head1 FEATURES |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=over 2 |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=item * Export C and C and C functions. Don't conflict debug command such as 'p' because these function names are consist of two characters. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=item * Encode all strings in reference data in C and C function. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=item * C is a short name of "dump UTF-8" |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=item * C is a short name of "dump Windows cp932" |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=item * C is a short name of "dump no encoding" |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=item * Use C method of L to dump data |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=item * Print line number and file name to STDERR |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=item * Keys of hash of dumped data is sorted. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=item * Don't print "$VAR1 =" unlike L default. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=back |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=head1 FUNCTIONS |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head2 du |
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
|
|
|
|
|
|
|
This function is exported. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
use D; |
208
|
|
|
|
|
|
|
my $data = [{name => 'あ'}, {name => 'い'}]; |
209
|
|
|
|
|
|
|
du $data; |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Following example is oneliner used. It can be used all functions. |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
my $data = [{name => 'あ'}, {name => 'い'}]; |
214
|
|
|
|
|
|
|
use D;du $data; |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=head2 dw |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
Encode all strings in reference data to cp932 and dump the reference data to STDERR with file name and line number. |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
If the argument is not reference data such as a string, it is also dumped in the same way as reference data. |
221
|
|
|
|
|
|
|
This function is exported. |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
use D; |
224
|
|
|
|
|
|
|
my $data = [{name => 'あ'}, {name => 'い'}]; |
225
|
|
|
|
|
|
|
dw $data; |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head2 dn |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
Dump reference data to STDERR without encoding with file name and line number. |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
If the argument is not reference data such as a string, it is also dumped in the same way as reference data. |
232
|
|
|
|
|
|
|
This function is exported. |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
use D; |
235
|
|
|
|
|
|
|
my $data = [{name => 'あ'}, {name => 'い'}]; |
236
|
|
|
|
|
|
|
dn $data; |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=head2 dustr |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
This function is return that UTF-8 encoded string. |
241
|
|
|
|
|
|
|
This function is exported. |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
Following example is get the UTF-8 encoded string. |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
use D; |
246
|
|
|
|
|
|
|
my $data = [{name => 'あ'}, {name => 'い'}]; |
247
|
|
|
|
|
|
|
my $str = dustr $data; |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=head2 dwstr |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
This function is return that cp932 encoded string. |
252
|
|
|
|
|
|
|
This function is exported. |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
Following example is get the cp932 encoded string. |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
use D; |
257
|
|
|
|
|
|
|
my $data = [{name => 'あ'}, {name => 'い'}]; |
258
|
|
|
|
|
|
|
my $str = dwstr $data; |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
=head2 dnstr |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
This function is return that without encoded string. |
263
|
|
|
|
|
|
|
This function is exported. |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
Following example is get the without encoded string. |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
use D; |
268
|
|
|
|
|
|
|
my $data = [{name => 'あ'}, {name => 'い'}]; |
269
|
|
|
|
|
|
|
my $str = dnstr $data; |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=head1 Bug Report |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
L |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=head1 SEE ALSO |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
L, L, L |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=head1 AUTHOR |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
Yoshiyuki Ito, Eyoshiyuki.ito.biz@gmail.comE |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
Yuki Kimoto, Ekimoto.yuki@gmail.comE |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
Copyright (C) 2019 by Yoshiyuki Ito, Yuki Kimoto |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
290
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.08.7 or, |
291
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=cut |