| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package App::DPath; |
|
2
|
|
|
|
|
|
|
# git description: v0.11-4-gbbe97b0 |
|
3
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:SCHWIGON'; |
|
5
|
|
|
|
|
|
|
# ABSTRACT: Cmdline tool around Data::DPath |
|
6
|
|
|
|
|
|
|
$App::DPath::VERSION = '0.12'; |
|
7
|
34
|
|
|
34
|
|
651665
|
use 5.008; # Data::DPath requires it |
|
|
34
|
|
|
|
|
121
|
|
|
8
|
34
|
|
|
34
|
|
794
|
use strict; |
|
|
34
|
|
|
|
|
67
|
|
|
|
34
|
|
|
|
|
1064
|
|
|
9
|
34
|
|
|
34
|
|
263
|
use warnings; |
|
|
34
|
|
|
|
|
157
|
|
|
|
34
|
|
|
|
|
10114
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
34
|
|
|
34
|
|
269
|
use Scalar::Util 'reftype'; |
|
|
34
|
|
|
|
|
77
|
|
|
|
34
|
|
|
|
|
29565
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub read_in |
|
14
|
|
|
|
|
|
|
{ |
|
15
|
|
|
|
|
|
|
#my ($c, $file) = @_; |
|
16
|
41
|
|
|
41
|
1
|
7894
|
my ($file, $intype, $yamlmod) = @_; |
|
17
|
|
|
|
|
|
|
|
|
18
|
41
|
|
100
|
|
|
207
|
$intype ||= 'yaml'; |
|
19
|
41
|
|
|
|
|
114
|
my $data; |
|
20
|
|
|
|
|
|
|
my $filecontent; |
|
21
|
|
|
|
|
|
|
{ |
|
22
|
41
|
|
|
|
|
87
|
local $/; |
|
|
41
|
|
|
|
|
216
|
|
|
23
|
41
|
50
|
|
|
|
166
|
if ($file eq '-') { |
|
24
|
0
|
|
|
|
|
0
|
$filecontent = ; |
|
25
|
|
|
|
|
|
|
} |
|
26
|
|
|
|
|
|
|
else |
|
27
|
|
|
|
|
|
|
{ |
|
28
|
41
|
100
|
|
|
|
2708
|
open (my $FH, "<", $file) or die "dpath: cannot open input file $file.\n"; |
|
29
|
40
|
|
|
|
|
1706
|
$filecontent = <$FH>; |
|
30
|
40
|
|
|
|
|
847
|
close $FH; |
|
31
|
|
|
|
|
|
|
} |
|
32
|
|
|
|
|
|
|
} |
|
33
|
|
|
|
|
|
|
|
|
34
|
40
|
100
|
100
|
|
|
485
|
if (not defined $filecontent or $filecontent !~ /[^\s\t\r\n]/ms) { |
|
35
|
3
|
|
|
|
|
578
|
die "dpath: no meaningful input to read.\n"; |
|
36
|
|
|
|
|
|
|
} |
|
37
|
|
|
|
|
|
|
|
|
38
|
37
|
100
|
|
|
|
314
|
if ($intype eq "yaml") { |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
39
|
8
|
|
|
|
|
3169
|
require YAML::Any; |
|
40
|
8
|
50
|
|
|
|
10913
|
if ($yamlmod) { |
|
41
|
0
|
|
|
|
|
0
|
@YAML::Any::_TEST_ORDER=($yamlmod); |
|
42
|
|
|
|
|
|
|
} else { |
|
43
|
8
|
|
|
|
|
36
|
@YAML::Any::_TEST_ORDER=(qw(YAML::XS YAML::Old YAML YAML::Tiny)); # no YAML::Syck |
|
44
|
|
|
|
|
|
|
} |
|
45
|
8
|
|
|
|
|
41
|
$data = [YAML::Any::Load($filecontent)]; |
|
46
|
|
|
|
|
|
|
} |
|
47
|
|
|
|
|
|
|
elsif ($intype eq "json") { |
|
48
|
2
|
|
|
|
|
870
|
require JSON; |
|
49
|
2
|
|
|
|
|
11940
|
$data = JSON::decode_json($filecontent); |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
elsif ($intype eq "xml") |
|
52
|
|
|
|
|
|
|
{ |
|
53
|
3
|
|
|
|
|
1954
|
require XML::Simple; |
|
54
|
3
|
|
|
|
|
26343
|
my $xs = new XML::Simple; |
|
55
|
3
|
|
|
|
|
241
|
$data = $xs->XMLin($filecontent, KeepRoot => 1); |
|
56
|
|
|
|
|
|
|
} |
|
57
|
|
|
|
|
|
|
elsif ($intype eq "ini") { |
|
58
|
6
|
|
|
|
|
2427
|
require Config::INI::Serializer; |
|
59
|
6
|
|
|
|
|
10319
|
my $ini = Config::INI::Serializer->new; |
|
60
|
6
|
|
|
|
|
44
|
$data = $ini->deserialize($filecontent); |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
elsif ($intype eq "cfggeneral") { |
|
63
|
9
|
|
|
|
|
6604
|
require Config::General; |
|
64
|
9
|
|
|
|
|
249706
|
my %data = Config::General->new(-String => $filecontent, |
|
65
|
|
|
|
|
|
|
-InterPolateVars => 1, |
|
66
|
|
|
|
|
|
|
)->getall; |
|
67
|
9
|
|
|
|
|
60366
|
$data = \%data; |
|
68
|
|
|
|
|
|
|
} |
|
69
|
|
|
|
|
|
|
elsif ($intype eq "dumper") { |
|
70
|
1
|
|
|
|
|
641
|
eval '$data = my '.$filecontent; |
|
71
|
|
|
|
|
|
|
} |
|
72
|
|
|
|
|
|
|
elsif ($intype eq "tap") { |
|
73
|
2
|
|
|
|
|
1756
|
require TAP::DOM; |
|
74
|
2
|
|
|
|
|
179928
|
require TAP::Parser; |
|
75
|
2
|
50
|
|
|
|
30
|
$data = new TAP::DOM( tap => $filecontent, trim_fieldvalues => 1, noempty_tap => 1, $TAP::Parser::VERSION > 3.22 ? (version => 13) : () ); |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
elsif ($intype eq "taparchive") { |
|
78
|
5
|
|
|
|
|
2055
|
require TAP::DOM::Archive; |
|
79
|
5
|
|
|
|
|
6772
|
require TAP::Parser; |
|
80
|
5
|
50
|
|
|
|
256724
|
$data = new TAP::DOM::Archive( filecontent => $filecontent, trim_fieldvalues => 1, noempty_tap => 1, $TAP::Parser::VERSION > 3.22 ? (version => 13) : () ); |
|
81
|
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
else |
|
83
|
|
|
|
|
|
|
{ |
|
84
|
1
|
|
|
|
|
9
|
die "dpath: unrecognized input format: $intype.\n"; |
|
85
|
|
|
|
|
|
|
} |
|
86
|
34
|
|
|
|
|
2540427
|
return $data; |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub _format_flat_inner_scalar |
|
90
|
|
|
|
|
|
|
{ |
|
91
|
9
|
|
|
9
|
|
379707
|
my ($result) = @_; |
|
92
|
|
|
|
|
|
|
|
|
93
|
34
|
|
|
34
|
|
287
|
no warnings 'uninitialized'; |
|
|
34
|
|
|
|
|
81
|
|
|
|
34
|
|
|
|
|
5060
|
|
|
94
|
|
|
|
|
|
|
|
|
95
|
9
|
|
|
|
|
48
|
return "$result"; |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub _format_flat_inner_array |
|
99
|
|
|
|
|
|
|
{ |
|
100
|
9
|
|
|
9
|
|
25
|
my ($opt, $result) = @_; |
|
101
|
|
|
|
|
|
|
|
|
102
|
34
|
|
|
34
|
|
222
|
no warnings 'uninitialized'; |
|
|
34
|
|
|
|
|
69
|
|
|
|
34
|
|
|
|
|
6220
|
|
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
return |
|
105
|
|
|
|
|
|
|
join($opt->{separator}, |
|
106
|
|
|
|
|
|
|
map { |
|
107
|
|
|
|
|
|
|
# only SCALARS allowed (where reftype returns undef) |
|
108
|
9
|
100
|
|
|
|
34
|
die "dpath: unsupported innermost nesting (".reftype($_).") for 'flat' output.\n" if defined reftype($_); |
|
|
11
|
|
|
|
|
60
|
|
|
109
|
10
|
|
|
|
|
57
|
"".$_ |
|
110
|
|
|
|
|
|
|
} @$result); |
|
111
|
|
|
|
|
|
|
} |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub _format_flat_inner_hash |
|
114
|
|
|
|
|
|
|
{ |
|
115
|
10
|
|
|
10
|
|
2290
|
my ($opt, $result) = @_; |
|
116
|
|
|
|
|
|
|
|
|
117
|
34
|
|
|
33
|
|
246
|
no warnings 'uninitialized'; |
|
|
33
|
|
|
|
|
138
|
|
|
|
33
|
|
|
|
|
7693
|
|
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
return |
|
120
|
|
|
|
|
|
|
join($opt->{separator}, |
|
121
|
10
|
|
|
|
|
45
|
map { my $v = $result->{$_}; |
|
|
9
|
|
|
|
|
22
|
|
|
122
|
|
|
|
|
|
|
# only SCALARS allowed (where reftype returns undef) |
|
123
|
9
|
100
|
|
|
|
747
|
die "dpath: unsupported innermost nesting (".reftype($v).") for 'flat' output.\n" if defined reftype($v); |
|
124
|
6
|
|
|
|
|
76
|
"$_=".$v |
|
125
|
|
|
|
|
|
|
} keys %$result); |
|
126
|
|
|
|
|
|
|
} |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub _format_flat_outer |
|
129
|
|
|
|
|
|
|
{ |
|
130
|
15
|
|
|
15
|
|
3257
|
my ($opt, $result) = @_; |
|
131
|
|
|
|
|
|
|
|
|
132
|
33
|
|
|
33
|
|
243
|
no warnings 'uninitialized'; |
|
|
33
|
|
|
|
|
110
|
|
|
|
33
|
|
|
|
|
39356
|
|
|
133
|
|
|
|
|
|
|
|
|
134
|
15
|
|
|
|
|
29
|
my $output = ""; |
|
135
|
15
|
100
|
|
|
|
60
|
die "dpath: can not flatten data structure (undef) - try other output format.\n" unless defined $result; |
|
136
|
|
|
|
|
|
|
|
|
137
|
14
|
100
|
|
|
|
29
|
my $A = ""; my $B = ""; if ($opt->{fb}) { $A = "["; $B = "]" } |
|
|
14
|
|
|
|
|
22
|
|
|
|
14
|
|
|
|
|
45
|
|
|
|
4
|
|
|
|
|
8
|
|
|
|
4
|
|
|
|
|
16
|
|
|
138
|
14
|
|
|
|
|
28
|
my $fi = $opt->{fi}; |
|
139
|
|
|
|
|
|
|
|
|
140
|
14
|
100
|
|
|
|
88
|
if (!defined reftype $result) { # SCALAR |
|
|
|
50
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
141
|
2
|
|
|
|
|
4
|
$output .= $result."\n"; # stringify |
|
142
|
|
|
|
|
|
|
} |
|
143
|
|
|
|
|
|
|
elsif (reftype $result eq 'SCALAR') { # blessed SCALAR |
|
144
|
0
|
|
|
|
|
0
|
$output .= $result."\n"; # stringify |
|
145
|
|
|
|
|
|
|
} |
|
146
|
|
|
|
|
|
|
elsif (reftype $result eq 'ARRAY') { |
|
147
|
5
|
|
|
|
|
17
|
for (my $i=0; $i<@$result; $i++) { |
|
148
|
8
|
|
|
|
|
15
|
my $entry = $result->[$i]; |
|
149
|
8
|
100
|
|
|
|
22
|
my $prefix = $fi ? "$i:" : ""; |
|
150
|
8
|
100
|
|
|
|
28
|
if (!defined reftype $entry) { # SCALAR |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
151
|
3
|
|
|
|
|
10
|
$output .= $prefix.$A._format_flat_inner_scalar($entry)."$B\n"; |
|
152
|
|
|
|
|
|
|
} |
|
153
|
|
|
|
|
|
|
elsif (reftype $entry eq 'ARRAY') { |
|
154
|
2
|
|
|
|
|
7
|
$output .= $prefix.$A._format_flat_inner_array($opt, $entry)."$B\n"; |
|
155
|
|
|
|
|
|
|
} |
|
156
|
|
|
|
|
|
|
elsif (reftype $entry eq 'HASH') { |
|
157
|
2
|
|
|
|
|
6
|
$output .= $prefix.$A._format_flat_inner_hash($opt, $entry)."$B\n"; |
|
158
|
|
|
|
|
|
|
} |
|
159
|
|
|
|
|
|
|
else { |
|
160
|
1
|
|
|
|
|
10
|
die "dpath: can not flatten data structure (".reftype($entry).").\n"; |
|
161
|
|
|
|
|
|
|
} |
|
162
|
|
|
|
|
|
|
} |
|
163
|
|
|
|
|
|
|
} |
|
164
|
|
|
|
|
|
|
elsif (reftype $result eq 'HASH') { |
|
165
|
6
|
|
|
|
|
21
|
my @keys = keys %$result; |
|
166
|
6
|
|
|
|
|
17
|
foreach my $key (@keys) { |
|
167
|
7
|
|
|
|
|
44
|
my $entry = $result->{$key}; |
|
168
|
7
|
100
|
|
|
|
41
|
if (!defined reftype $entry) { # SCALAR |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
169
|
2
|
|
|
|
|
9
|
$output .= "$key:"._format_flat_inner_scalar($entry)."\n"; |
|
170
|
|
|
|
|
|
|
} |
|
171
|
|
|
|
|
|
|
elsif (reftype $entry eq 'ARRAY') { |
|
172
|
1
|
|
|
|
|
9
|
$output .= "$key:"._format_flat_inner_array($opt, $entry)."\n"; |
|
173
|
|
|
|
|
|
|
} |
|
174
|
|
|
|
|
|
|
elsif (reftype $entry eq 'HASH') { |
|
175
|
3
|
|
|
|
|
17
|
$output .= "$key:"._format_flat_inner_hash($opt, $entry)."\n"; |
|
176
|
|
|
|
|
|
|
} |
|
177
|
|
|
|
|
|
|
else { |
|
178
|
1
|
|
|
|
|
12
|
die "dpath: can not flatten data structure (".reftype($entry).").\n"; |
|
179
|
|
|
|
|
|
|
} |
|
180
|
|
|
|
|
|
|
} |
|
181
|
|
|
|
|
|
|
} |
|
182
|
|
|
|
|
|
|
else { |
|
183
|
1
|
|
|
|
|
9
|
die "dpath: can not flatten data structure (".reftype($result).") - try other output format.\n"; |
|
184
|
|
|
|
|
|
|
} |
|
185
|
|
|
|
|
|
|
|
|
186
|
9
|
|
|
|
|
48
|
return $output; |
|
187
|
|
|
|
|
|
|
} |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
sub _format_flat |
|
190
|
|
|
|
|
|
|
{ |
|
191
|
3
|
|
|
3
|
|
818
|
my ($opt, $resultlist) = @_; |
|
192
|
|
|
|
|
|
|
|
|
193
|
3
|
|
|
|
|
9
|
my $output = ""; |
|
194
|
3
|
50
|
|
|
|
22
|
$opt->{separator} = ";" unless defined $opt->{separator}; |
|
195
|
3
|
|
|
|
|
18
|
$output .= _format_flat_outer($opt, $_) foreach @$resultlist; |
|
196
|
1
|
|
|
|
|
6
|
return $output; |
|
197
|
|
|
|
|
|
|
} |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub write_out |
|
200
|
|
|
|
|
|
|
{ |
|
201
|
62
|
|
|
62
|
1
|
22468
|
my ($opt, $resultlist) = @_; |
|
202
|
|
|
|
|
|
|
|
|
203
|
62
|
|
|
|
|
146
|
my $output = ""; |
|
204
|
62
|
|
100
|
|
|
454
|
my $outtype = $opt->{outtype} || 'yaml'; |
|
205
|
62
|
100
|
|
|
|
374
|
if ($outtype eq "yaml") |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
{ |
|
207
|
14
|
|
|
|
|
3697
|
require YAML::Any; |
|
208
|
14
|
50
|
|
|
|
10395
|
if ($opt->{'yaml-module'}) { |
|
209
|
0
|
|
|
|
|
0
|
@YAML::Any::_TEST_ORDER=($opt->{'yaml-module'}); |
|
210
|
|
|
|
|
|
|
} else { |
|
211
|
14
|
|
|
|
|
71
|
@YAML::Any::_TEST_ORDER=(qw(YAML::XS YAML::Old YAML YAML::Tiny)); # no YAML::Syck |
|
212
|
|
|
|
|
|
|
} |
|
213
|
14
|
|
|
|
|
64
|
$output .= YAML::Any::Dump($resultlist); |
|
214
|
|
|
|
|
|
|
} |
|
215
|
|
|
|
|
|
|
elsif ($outtype eq "json") |
|
216
|
|
|
|
|
|
|
{ |
|
217
|
15
|
|
|
9
|
|
1674
|
eval "use JSON -convert_blessed_universally"; |
|
|
9
|
|
|
|
|
6676
|
|
|
|
9
|
|
|
|
|
91869
|
|
|
|
9
|
|
|
|
|
63
|
|
|
218
|
15
|
|
|
|
|
5037
|
my $json = JSON->new->allow_nonref->pretty->allow_blessed->convert_blessed; |
|
219
|
15
|
|
|
|
|
423
|
$output .= $json->encode($resultlist); |
|
220
|
|
|
|
|
|
|
} |
|
221
|
|
|
|
|
|
|
elsif ($outtype eq "ini") { |
|
222
|
10
|
|
|
|
|
1992
|
require Config::INI::Serializer; |
|
223
|
10
|
|
|
|
|
5589
|
my $ini = Config::INI::Serializer->new; |
|
224
|
10
|
|
|
|
|
73
|
$output .= $ini->serialize($resultlist); |
|
225
|
|
|
|
|
|
|
} |
|
226
|
|
|
|
|
|
|
elsif ($outtype eq "dumper") |
|
227
|
|
|
|
|
|
|
{ |
|
228
|
13
|
|
|
|
|
124
|
require Data::Dumper; |
|
229
|
13
|
|
|
|
|
115
|
$output .= Data::Dumper::Dumper($resultlist); |
|
230
|
|
|
|
|
|
|
} |
|
231
|
|
|
|
|
|
|
elsif ($outtype eq "xml") |
|
232
|
|
|
|
|
|
|
{ |
|
233
|
7
|
|
|
|
|
897
|
require XML::Simple; |
|
234
|
7
|
|
|
|
|
8494
|
my $xs = new XML::Simple; |
|
235
|
7
|
|
|
|
|
522
|
$output .= $xs->XMLout($resultlist, AttrIndent => 1, KeepRoot => 1); |
|
236
|
|
|
|
|
|
|
} |
|
237
|
|
|
|
|
|
|
elsif ($outtype eq "flat") { |
|
238
|
2
|
|
|
|
|
10
|
$output .= _format_flat( $opt, $resultlist ); |
|
239
|
|
|
|
|
|
|
} |
|
240
|
|
|
|
|
|
|
else |
|
241
|
|
|
|
|
|
|
{ |
|
242
|
1
|
|
|
|
|
12
|
die "dpath: unrecognized output format: $outtype."; |
|
243
|
|
|
|
|
|
|
} |
|
244
|
59
|
|
|
|
|
1021497
|
return $output; |
|
245
|
|
|
|
|
|
|
} |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
1; |
|
250
|
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
__END__ |