| 1 | 22 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 22 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | package Parse::CPAN::Meta; | ||||||||||||||||||||||||||||
| 5 | # ABSTRACT: Parse META.yml and META.json CPAN metadata files | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 22 | our $VERSION = '2.150015'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 22 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 22 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 22 | our @ISA = qw/Exporter/; | |||||||||||||||||||||||||||
| 13 | 22 | our @EXPORT_OK = qw/Load LoadFile/; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 149 | ▶ | sub load_file { | ||||||||||||||||||||||||||
|
Subroutine
load_file: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 149 | my ($class, $filename) = @_; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 149 | my $meta = _slurp($filename); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 149 | ▶ | if ($filename =~ /\.ya?ml$/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 21 | 103 | return $class->load_yaml_string($meta); | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | elsif ($filename =~ /\.json$/) { | ||||||||||||||||||||||||||||
| 24 | 39 | return $class->load_json_string($meta); | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | else { | ||||||||||||||||||||||||||||
| 27 | 7 | $class->load_string($meta); # try to detect yaml/json | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 8 | ▶ | sub load_string { | ||||||||||||||||||||||||||
|
Subroutine
load_string: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 8 | my ($class, $string) = @_; | |||||||||||||||||||||||||||
| 33 | 8 | ▶ | if ( $string =~ /^---/ ) { # looks like YAML | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 34 | 1 | return $class->load_yaml_string($string); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | elsif ( $string =~ /^\s*\{/ ) { # looks like JSON | ||||||||||||||||||||||||||||
| 37 | 6 | return $class->load_json_string($string); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | else { # maybe doc-marker-free YAML | ||||||||||||||||||||||||||||
| 40 | 1 | return $class->load_yaml_string($string); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 122 | ▶ | sub load_yaml_string { | ||||||||||||||||||||||||||
|
Subroutine
load_yaml_string: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 122 | my ($class, $string) = @_; | |||||||||||||||||||||||||||
| 46 | 122 | my $backend = $class->yaml_backend(); | |||||||||||||||||||||||||||
| 47 | 22 | ▶ | my $data = eval { no strict 'refs'; &{"$backend\::Load"}($string) }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | 122 | ▶ | croak $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 121 | ▶ | return $data || {}; # in case document was valid but empty | ||||||||||||||||||||||||||
|
Condition50%1/2
$data || {}
MC/DC0%0/1
$data || {}
$data
Truth table50%1/2
A
A= $data
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 76 | ▶ | sub load_json_string { | ||||||||||||||||||||||||||
|
Subroutine
load_json_string: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 76 | my ($class, $string) = @_; | |||||||||||||||||||||||||||
| 54 | 76 | require Encode; | |||||||||||||||||||||||||||
| 55 | # load_json_string takes characters, decode_json expects bytes | ||||||||||||||||||||||||||||
| 56 | 76 | my $encoded = Encode::encode('UTF-8', $string, Encode::PERLQQ()); | |||||||||||||||||||||||||||
| 57 | 76 | my $data = eval { $class->json_decoder()->can('decode_json')->($encoded) }; | |||||||||||||||||||||||||||
| 58 | 76 | ▶ | croak $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 76 | ▶ | return $data || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$data || {}
MC/DC0%0/1
$data || {}
$data
Truth table50%1/2
A
A= $data
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 131 | ▶ | sub yaml_backend { | ||||||||||||||||||||||||||
|
Subroutine
yaml_backend: called CC=6 SCAR=18.6
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 131 | ▶ | if ($ENV{PERL_CORE} or not defined $ENV{PERL_YAML_BACKEND} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ENV{"PERL_CORE"} or not defined $ENV{"PERL_YAML_BACKEND"}
MC/DC50%1/2
$ENV{"PERL_CORE"} or not defined $ENV{"PERL_YAML_BACKEND"}
$ENV{"PERL_CORE"}
not defined $ENV{"PERL_YAML_BACKEND"}
Truth table66%2/3
A or not(B)
A= $ENV{"PERL_CORE"}B= not defined $ENV{"PERL_YAML_BACKEND"}
| |||||||||||||||||||||||||||||
| 64 | 129 | ▶ | _can_load( 'CPAN::Meta::YAML', 0.011 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | or croak "CPAN::Meta::YAML 0.011 is not available\n"; | ||||||||||||||||||||||||||||
| 66 | 129 | return "CPAN::Meta::YAML"; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | else { | ||||||||||||||||||||||||||||
| 69 | 2 | my $backend = $ENV{PERL_YAML_BACKEND}; | |||||||||||||||||||||||||||
| 70 | 2 | ▶ | _can_load( $backend ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | or croak "Could not load PERL_YAML_BACKEND '$backend'\n"; | ||||||||||||||||||||||||||||
| 72 | 2 | ▶ | $backend->can("Load") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | or croak "PERL_YAML_BACKEND '$backend' does not implement Load()\n"; | ||||||||||||||||||||||||||||
| 74 | 2 | return $backend; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 77 | ▶ | sub json_decoder { | ||||||||||||||||||||||||||
|
Subroutine
json_decoder: called CC=6 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 77 | ▶ | if ($ENV{PERL_CORE}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | _can_load( 'JSON::PP' => 2.27300 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | or croak "JSON::PP 2.27300 is not available\n"; | ||||||||||||||||||||||||||||
| 82 | 0 | return 'JSON::PP'; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 77 | ▶ | if (my $decoder = $ENV{CPAN_META_JSON_DECODER}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 2 | ▶ | _can_load( $decoder ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | or croak "Could not load CPAN_META_JSON_DECODER '$decoder'\n"; | ||||||||||||||||||||||||||||
| 87 | 2 | ▶ | $decoder->can('decode_json') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | or croak "No decode_json sub provided by CPAN_META_JSON_DECODER '$decoder'\n"; | ||||||||||||||||||||||||||||
| 89 | 2 | return $decoder; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 75 | return $_[0]->json_backend; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 83 | ▶ | sub json_backend { | ||||||||||||||||||||||||||
|
Subroutine
json_backend: called CC=10 SCAR=36.1
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 83 | ▶ | if ($ENV{PERL_CORE}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | _can_load( 'JSON::PP' => 2.27300 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | or croak "JSON::PP 2.27300 is not available\n"; | ||||||||||||||||||||||||||||
| 98 | 0 | return 'JSON::PP'; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 83 | ▶ | if (my $backend = $ENV{CPAN_META_JSON_BACKEND}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | _can_load( $backend ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | or croak "Could not load CPAN_META_JSON_BACKEND '$backend'\n"; | ||||||||||||||||||||||||||||
| 103 | 0 | ▶ | $backend->can('new') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | or croak "No constructor provided by CPAN_META_JSON_BACKEND '$backend'\n"; | ||||||||||||||||||||||||||||
| 105 | 0 | return $backend; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 83 | ▶ | if (! $ENV{PERL_JSON_BACKEND} or $ENV{PERL_JSON_BACKEND} eq 'JSON::PP') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $ENV{"PERL_JSON_BACKEND"} or $ENV{"PERL_JSON_BACKEND"} eq "JSON::PP"
MC/DC0%0/2
not $ENV{"PERL_JSON_BACKEND"} or $ENV{"PERL_JSON_BACKEND"} eq "JSON::PP"
not $ENV{"PERL_JSON_BACKEND"}
$ENV{"PERL_JSON_BACKEND"} eq "JSON::PP"
Truth table66%2/3
not(A) or B
A= not $ENV{"PERL_JSON_BACKEND"}B= $ENV{"PERL_JSON_BACKEND"} eq "JSON::PP"
| |||||||||||||||||||||||||||||
| 108 | 83 | ▶ | _can_load( 'JSON::PP' => 2.27300 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | or croak "JSON::PP 2.27300 is not available\n"; | ||||||||||||||||||||||||||||
| 110 | 83 | return 'JSON::PP'; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | else { | ||||||||||||||||||||||||||||
| 113 | 0 | ▶ | _can_load( 'JSON' => 2.5 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | or croak "JSON 2.5 is required for " . | ||||||||||||||||||||||||||||
| 115 | "\$ENV{PERL_JSON_BACKEND} = '$ENV{PERL_JSON_BACKEND}'\n"; | ||||||||||||||||||||||||||||
| 116 | 0 | return "JSON"; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 150 | ▶ | sub _slurp { | ||||||||||||||||||||||||||
|
Subroutine
_slurp: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 121 | 150 | require Encode; | |||||||||||||||||||||||||||
| 122 | 150 | ▶ | open my $fh, "<:raw", "$_[0]" ## no critic | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | or die "can't open $_[0] for reading: $!"; | ||||||||||||||||||||||||||||
| 124 | 150 | my $content = do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 125 | 150 | $content = Encode::decode('UTF-8', $content, Encode::PERLQQ()); | |||||||||||||||||||||||||||
| 126 | 150 | return $content; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 216 | ▶ | sub _can_load { | ||||||||||||||||||||||||||
|
Subroutine
_can_load: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||
| 130 | 216 | my ($module, $version) = @_; | |||||||||||||||||||||||||||
| 131 | 216 | (my $file = $module) =~ s{::}{/}g; | |||||||||||||||||||||||||||
| 132 | 216 | $file .= ".pm"; | |||||||||||||||||||||||||||
| 133 | 216 | ▶ | return 1 if $INC{$file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 31 | ▶ | return 0 if exists $INC{$file}; # prior load failed | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 31 | ▶ | eval { require $file; 1 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | or return 0; | ||||||||||||||||||||||||||||
| 137 | 31 | ▶ | if ( defined $version ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 31 | ▶ | eval { $module->VERSION($version); 1 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | or return 0; | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 31 | return 1; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # Kept for backwards compatibility only | ||||||||||||||||||||||||||||
| 145 | # Create an object from a file | ||||||||||||||||||||||||||||
| 146 | 1 | ▶ | sub LoadFile ($) { ## no critic | ||||||||||||||||||||||||||
|
Subroutine
LoadFile: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | 1 | return Load(_slurp(shift)); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # Parse a document from a string. | ||||||||||||||||||||||||||||
| 151 | 3 | ▶ | sub Load ($) { ## no critic | ||||||||||||||||||||||||||
|
Subroutine
Load: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 152 | 3 | require CPAN::Meta::YAML; | |||||||||||||||||||||||||||
| 153 | 3 | my $object = eval { CPAN::Meta::YAML::Load(shift) }; | |||||||||||||||||||||||||||
| 154 | 3 | ▶ | croak $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 3 | return $object; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 22 | 1; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | __END__ | ||||||||||||||||||||||||||||