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