line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
17
|
|
|
17
|
|
40991
|
use 5.008001; |
|
17
|
|
|
|
|
33
|
|
2
|
17
|
|
|
17
|
|
66
|
use strict; |
|
17
|
|
|
|
|
13
|
|
|
17
|
|
|
|
|
996
|
|
3
|
17
|
|
|
17
|
|
46
|
use warnings; |
|
17
|
|
|
|
|
12
|
|
|
17
|
|
|
|
|
650
|
|
4
|
|
|
|
|
|
|
package Parse::CPAN::Meta; |
5
|
|
|
|
|
|
|
# ABSTRACT: Parse META.yml and META.json CPAN metadata files |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '2.150010'; |
8
|
|
|
|
|
|
|
|
9
|
17
|
|
|
17
|
|
51
|
use Exporter; |
|
17
|
|
|
|
|
14
|
|
|
17
|
|
|
|
|
508
|
|
10
|
17
|
|
|
17
|
|
47
|
use Carp 'croak'; |
|
17
|
|
|
|
|
17
|
|
|
17
|
|
|
|
|
3877
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @ISA = qw/Exporter/; |
13
|
|
|
|
|
|
|
our @EXPORT_OK = qw/Load LoadFile/; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub load_file { |
16
|
149
|
|
|
149
|
1
|
20006
|
my ($class, $filename) = @_; |
17
|
|
|
|
|
|
|
|
18
|
149
|
|
|
|
|
306
|
my $meta = _slurp($filename); |
19
|
|
|
|
|
|
|
|
20
|
149
|
100
|
|
|
|
835
|
if ($filename =~ /\.ya?ml$/) { |
|
|
100
|
|
|
|
|
|
21
|
103
|
|
|
|
|
517
|
return $class->load_yaml_string($meta); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
elsif ($filename =~ /\.json$/) { |
24
|
39
|
|
|
|
|
177
|
return $class->load_json_string($meta); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
else { |
27
|
7
|
|
|
|
|
38
|
$class->load_string($meta); # try to detect yaml/json |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub load_string { |
32
|
8
|
|
|
8
|
1
|
11
|
my ($class, $string) = @_; |
33
|
8
|
100
|
|
|
|
60
|
if ( $string =~ /^---/ ) { # looks like YAML |
|
|
100
|
|
|
|
|
|
34
|
1
|
|
|
|
|
3
|
return $class->load_yaml_string($string); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
elsif ( $string =~ /^\s*\{/ ) { # looks like JSON |
37
|
6
|
|
|
|
|
14
|
return $class->load_json_string($string); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
else { # maybe doc-marker-free YAML |
40
|
1
|
|
|
|
|
3
|
return $class->load_yaml_string($string); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub load_yaml_string { |
45
|
121
|
|
|
121
|
1
|
2177
|
my ($class, $string) = @_; |
46
|
121
|
|
|
|
|
228
|
my $backend = $class->yaml_backend(); |
47
|
17
|
|
|
17
|
|
62
|
my $data = eval { no strict 'refs'; &{"$backend\::Load"}($string) }; |
|
17
|
|
|
|
|
12
|
|
|
17
|
|
|
|
|
10770
|
|
|
121
|
|
|
|
|
153
|
|
|
121
|
|
|
|
|
116
|
|
|
121
|
|
|
|
|
534
|
|
48
|
121
|
100
|
|
|
|
180158
|
croak $@ if $@; |
49
|
120
|
|
50
|
|
|
451
|
return $data || {}; # in case document was valid but empty |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub load_json_string { |
53
|
54
|
|
|
54
|
1
|
4819
|
my ($class, $string) = @_; |
54
|
54
|
|
|
|
|
200
|
require Encode; |
55
|
|
|
|
|
|
|
# load_json_string takes characters, decode_json expects bytes |
56
|
54
|
|
|
|
|
169
|
my $encoded = Encode::encode('UTF-8', $string, Encode::PERLQQ()); |
57
|
54
|
|
|
|
|
2952
|
my $data = eval { $class->json_decoder()->can('decode_json')->($encoded) }; |
|
54
|
|
|
|
|
128
|
|
58
|
54
|
50
|
|
|
|
409239
|
croak $@ if $@; |
59
|
54
|
|
50
|
|
|
212
|
return $data || {}; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub yaml_backend { |
63
|
129
|
50
|
33
|
129
|
1
|
6673
|
if ($ENV{PERL_CORE} or not defined $ENV{PERL_YAML_BACKEND} ) { |
64
|
129
|
50
|
|
|
|
222
|
_can_load( 'CPAN::Meta::YAML', 0.011 ) |
65
|
|
|
|
|
|
|
or croak "CPAN::Meta::YAML 0.011 is not available\n"; |
66
|
129
|
|
|
|
|
229
|
return "CPAN::Meta::YAML"; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
else { |
69
|
0
|
|
|
|
|
0
|
my $backend = $ENV{PERL_YAML_BACKEND}; |
70
|
0
|
0
|
|
|
|
0
|
_can_load( $backend ) |
71
|
|
|
|
|
|
|
or croak "Could not load PERL_YAML_BACKEND '$backend'\n"; |
72
|
0
|
0
|
|
|
|
0
|
$backend->can("Load") |
73
|
|
|
|
|
|
|
or croak "PERL_YAML_BACKEND '$backend' does not implement Load()\n"; |
74
|
0
|
|
|
|
|
0
|
return $backend; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub json_decoder { |
79
|
55
|
50
|
|
55
|
1
|
1469
|
if ($ENV{PERL_CORE}) { |
80
|
0
|
0
|
|
|
|
0
|
_can_load( 'JSON::PP' => 2.27300 ) |
81
|
|
|
|
|
|
|
or croak "JSON::PP 2.27300 is not available\n"; |
82
|
0
|
|
|
|
|
0
|
return 'JSON::PP'; |
83
|
|
|
|
|
|
|
} |
84
|
55
|
100
|
|
|
|
115
|
if (my $decoder = $ENV{CPAN_META_JSON_DECODER}) { |
85
|
2
|
50
|
|
|
|
8
|
_can_load( $decoder ) |
86
|
|
|
|
|
|
|
or croak "Could not load CPAN_META_JSON_DECODER '$decoder'\n"; |
87
|
2
|
50
|
|
|
|
15
|
$decoder->can('decode_json') |
88
|
|
|
|
|
|
|
or croak "No decode_json sub provided by CPAN_META_JSON_DECODER '$decoder'\n"; |
89
|
2
|
|
|
|
|
8
|
return $decoder; |
90
|
|
|
|
|
|
|
} |
91
|
53
|
|
|
|
|
116
|
return $_[0]->json_backend; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub json_backend { |
95
|
61
|
50
|
|
61
|
1
|
6761
|
if ($ENV{PERL_CORE}) { |
96
|
0
|
0
|
|
|
|
0
|
_can_load( 'JSON::PP' => 2.27300 ) |
97
|
|
|
|
|
|
|
or croak "JSON::PP 2.27300 is not available\n"; |
98
|
0
|
|
|
|
|
0
|
return 'JSON::PP'; |
99
|
|
|
|
|
|
|
} |
100
|
61
|
50
|
|
|
|
121
|
if (my $backend = $ENV{CPAN_META_JSON_BACKEND}) { |
101
|
0
|
0
|
|
|
|
0
|
_can_load( $backend ) |
102
|
|
|
|
|
|
|
or croak "Could not load CPAN_META_JSON_BACKEND '$backend'\n"; |
103
|
0
|
0
|
|
|
|
0
|
$backend->can('new') |
104
|
|
|
|
|
|
|
or croak "No constructor provided by CPAN_META_JSON_BACKEND '$backend'\n"; |
105
|
0
|
|
|
|
|
0
|
return $backend; |
106
|
|
|
|
|
|
|
} |
107
|
61
|
50
|
66
|
|
|
163
|
if (! $ENV{PERL_JSON_BACKEND} or $ENV{PERL_JSON_BACKEND} eq 'JSON::PP') { |
108
|
61
|
50
|
|
|
|
97
|
_can_load( 'JSON::PP' => 2.27300 ) |
109
|
|
|
|
|
|
|
or croak "JSON::PP 2.27300 is not available\n"; |
110
|
61
|
|
|
|
|
733
|
return 'JSON::PP'; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
else { |
113
|
0
|
0
|
|
|
|
0
|
_can_load( 'JSON' => 2.5 ) |
114
|
|
|
|
|
|
|
or croak "JSON 2.5 is required for " . |
115
|
|
|
|
|
|
|
"\$ENV{PERL_JSON_BACKEND} = '$ENV{PERL_JSON_BACKEND}'\n"; |
116
|
0
|
|
|
|
|
0
|
return "JSON"; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub _slurp { |
121
|
150
|
|
|
150
|
|
4270
|
require Encode; |
122
|
150
|
50
|
|
|
|
52455
|
open my $fh, "<:raw", "$_[0]" ## no critic |
123
|
|
|
|
|
|
|
or die "can't open $_[0] for reading: $!"; |
124
|
150
|
|
|
|
|
187
|
my $content = do { local $/; <$fh> }; |
|
150
|
|
|
|
|
416
|
|
|
150
|
|
|
|
|
3180
|
|
125
|
150
|
|
|
|
|
738
|
$content = Encode::decode('UTF-8', $content, Encode::PERLQQ()); |
126
|
150
|
|
|
|
|
12440
|
return $content; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub _can_load { |
130
|
192
|
|
|
192
|
|
242
|
my ($module, $version) = @_; |
131
|
192
|
|
|
|
|
642
|
(my $file = $module) =~ s{::}{/}g; |
132
|
192
|
|
|
|
|
252
|
$file .= ".pm"; |
133
|
192
|
100
|
|
|
|
648
|
return 1 if $INC{$file}; |
134
|
9
|
50
|
|
|
|
29
|
return 0 if exists $INC{$file}; # prior load failed |
135
|
9
|
50
|
|
|
|
15
|
eval { require $file; 1 } |
|
9
|
|
|
|
|
4610
|
|
|
9
|
|
|
|
|
36113
|
|
136
|
|
|
|
|
|
|
or return 0; |
137
|
9
|
50
|
|
|
|
33
|
if ( defined $version ) { |
138
|
9
|
50
|
|
|
|
15
|
eval { $module->VERSION($version); 1 } |
|
9
|
|
|
|
|
162
|
|
|
9
|
|
|
|
|
41
|
|
139
|
|
|
|
|
|
|
or return 0; |
140
|
|
|
|
|
|
|
} |
141
|
9
|
|
|
|
|
30
|
return 1; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# Kept for backwards compatibility only |
145
|
|
|
|
|
|
|
# Create an object from a file |
146
|
|
|
|
|
|
|
sub LoadFile ($) { ## no critic |
147
|
1
|
|
|
1
|
1
|
2
|
return Load(_slurp(shift)); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
# Parse a document from a string. |
151
|
|
|
|
|
|
|
sub Load ($) { ## no critic |
152
|
3
|
|
|
3
|
1
|
606
|
require CPAN::Meta::YAML; |
153
|
3
|
|
|
|
|
3842
|
my $object = eval { CPAN::Meta::YAML::Load(shift) }; |
|
3
|
|
|
|
|
8
|
|
154
|
3
|
50
|
|
|
|
969
|
croak $@ if $@; |
155
|
3
|
|
|
|
|
7
|
return $object; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
1; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
__END__ |