<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">use 5.006;
use strict;
use warnings;
package CPAN::Meta;
our $VERSION = '2.120921'; # VERSION


use Carp qw(carp croak);
use CPAN::Meta::Feature;
use CPAN::Meta::Prereqs;
use CPAN::Meta::Converter;
use CPAN::Meta::Validator;
use Parse::CPAN::Meta 1.4403 ();

BEGIN { *_dclone = \&amp;CPAN::Meta::Converter::_dclone }


BEGIN {
  my @STRING_READERS = qw(
    abstract
    description
    dynamic_config
    generated_by
    name
    release_status
    version
  );

  no strict 'refs';
  for my $attr (@STRING_READERS) {
    *$attr = sub { $_[0]{ $attr } };
  }
}


BEGIN {
  my @LIST_READERS = qw(
    author
    keywords
    license
  );

  no strict 'refs';
  for my $attr (@LIST_READERS) {
    *$attr = sub {
      my $value = $_[0]{ $attr };
      croak "$attr must be called in list context"
        unless wantarray;
      return @{ _dclone($value) } if ref $value;
      return $value;
    };
  }
}

sub authors  { $_[0]-&gt;author }
sub licenses { $_[0]-&gt;license }


BEGIN {
  my @MAP_READERS = qw(
    meta-spec
    resources
    provides
    no_index

    prereqs
    optional_features
  );

  no strict 'refs';
  for my $attr (@MAP_READERS) {
    (my $subname = $attr) =~ s/-/_/;
    *$subname = sub {
      my $value = $_[0]{ $attr };
      return _dclone($value) if $value;
      return {};
    };
  }
}


sub custom_keys {
  return grep { /^x_/i } keys %{$_[0]};
}

sub custom {
  my ($self, $attr) = @_;
  my $value = $self-&gt;{$attr};
  return _dclone($value) if ref $value;
  return $value;
}


sub _new {
  my ($class, $struct, $options) = @_;
  my $self;

  if ( $options-&gt;{lazy_validation} ) {
    # try to convert to a valid structure; if succeeds, then return it
    my $cmc = CPAN::Meta::Converter-&gt;new( $struct );
    $self = $cmc-&gt;convert( version =&gt; 2 ); # valid or dies
    return bless $self, $class;
  }
  else {
    # validate original struct
    my $cmv = CPAN::Meta::Validator-&gt;new( $struct );
    unless ( $cmv-&gt;is_valid) {
      die "Invalid metadata structure. Errors: "
        . join(", ", $cmv-&gt;errors) . "\n";
    }
  }

  # up-convert older spec versions
  my $version = $struct-&gt;{'meta-spec'}{version} || '1.0';
  if ( $version == 2 ) {
    $self = $struct;
  }
  else {
    my $cmc = CPAN::Meta::Converter-&gt;new( $struct );
    $self = $cmc-&gt;convert( version =&gt; 2 );
  }

  return bless $self, $class;
}

sub new {
  my ($class, $struct, $options) = @_;
  my $self = eval { $class-&gt;_new($struct, $options) };
  croak($@) if $@;
  return $self;
}


sub create {
  my ($class, $struct, $options) = @_;
  my $version = __PACKAGE__-&gt;VERSION || 2;
  $struct-&gt;{generated_by} ||= __PACKAGE__ . " version $version" ;
  $struct-&gt;{'meta-spec'}{version} ||= int($version);
  my $self = eval { $class-&gt;_new($struct, $options) };
  croak ($@) if $@;
  return $self;
}


sub load_file {
  my ($class, $file, $options) = @_;
  $options-&gt;{lazy_validation} = 1 unless exists $options-&gt;{lazy_validation};

  croak "load_file() requires a valid, readable filename"
    unless -r $file;

  my $self;
  eval {
    my $struct = Parse::CPAN::Meta-&gt;load_file( $file );
    $self = $class-&gt;_new($struct, $options);
  };
  croak($@) if $@;
  return $self;
}


sub load_yaml_string {
  my ($class, $yaml, $options) = @_;
  $options-&gt;{lazy_validation} = 1 unless exists $options-&gt;{lazy_validation};

  my $self;
  eval {
    my ($struct) = Parse::CPAN::Meta-&gt;load_yaml_string( $yaml );
    $self = $class-&gt;_new($struct, $options);
  };
  croak($@) if $@;
  return $self;
}


sub load_json_string {
  my ($class, $json, $options) = @_;
  $options-&gt;{lazy_validation} = 1 unless exists $options-&gt;{lazy_validation};

  my $self;
  eval {
    my $struct = Parse::CPAN::Meta-&gt;load_json_string( $json );
    $self = $class-&gt;_new($struct, $options);
  };
  croak($@) if $@;
  return $self;
}


sub save {
  my ($self, $file, $options) = @_;

  my $version = $options-&gt;{version} || '2';
  my $layer = $] ge '5.008001' ? ':utf8' : '';

  if ( $version ge '2' ) {
    carp "'$file' should end in '.json'"
      unless $file =~ m{\.json$};
  }
  else {
    carp "'$file' should end in '.yml'"
      unless $file =~ m{\.yml$};
  }

  my $data = $self-&gt;as_string( $options );
  open my $fh, "&gt;$layer", $file
    or die "Error opening '$file' for writing: $!\n";

  print {$fh} $data;
  close $fh
    or die "Error closing '$file': $!\n";

  return 1;
}


sub meta_spec_version {
  my ($self) = @_;
  return $self-&gt;meta_spec-&gt;{version};
}


sub effective_prereqs {
  my ($self, $features) = @_;
  $features ||= [];

  my $prereq = CPAN::Meta::Prereqs-&gt;new($self-&gt;prereqs);

  return $prereq unless @$features;

  my @other = map {; $self-&gt;feature($_)-&gt;prereqs } @$features;

  return $prereq-&gt;with_merged_prereqs(\@other);
}


sub should_index_file {
  my ($self, $filename) = @_;

  for my $no_index_file (@{ $self-&gt;no_index-&gt;{file} || [] }) {
    return if $filename eq $no_index_file;
  }

  for my $no_index_dir (@{ $self-&gt;no_index-&gt;{directory} }) {
    $no_index_dir =~ s{$}{/} unless $no_index_dir =~ m{/\z};
    return if index($filename, $no_index_dir) == 0;
  }

  return 1;
}


sub should_index_package {
  my ($self, $package) = @_;

  for my $no_index_pkg (@{ $self-&gt;no_index-&gt;{package} || [] }) {
    return if $package eq $no_index_pkg;
  }

  for my $no_index_ns (@{ $self-&gt;no_index-&gt;{namespace} }) {
    return if index($package, "${no_index_ns}::") == 0;
  }

  return 1;
}


sub features {
  my ($self) = @_;

  my $opt_f = $self-&gt;optional_features;
  my @features = map {; CPAN::Meta::Feature-&gt;new($_ =&gt; $opt_f-&gt;{ $_ }) }
                 keys %$opt_f;

  return @features;
}


sub feature {
  my ($self, $ident) = @_;

  croak "no feature named $ident"
    unless my $f = $self-&gt;optional_features-&gt;{ $ident };

  return CPAN::Meta::Feature-&gt;new($ident, $f);
}


sub as_struct {
  my ($self, $options) = @_;
  my $struct = _dclone($self);
  if ( $options-&gt;{version} ) {
    my $cmc = CPAN::Meta::Converter-&gt;new( $struct );
    $struct = $cmc-&gt;convert( version =&gt; $options-&gt;{version} );
  }
  return $struct;
}


sub as_string {
  my ($self, $options) = @_;

  my $version = $options-&gt;{version} || '2';

  my $struct;
  if ( $self-&gt;meta_spec_version ne $version ) {
    my $cmc = CPAN::Meta::Converter-&gt;new( $self-&gt;as_struct );
    $struct = $cmc-&gt;convert( version =&gt; $version );
  }
  else {
    $struct = $self-&gt;as_struct;
  }

  my ($data, $backend);
  if ( $version ge '2' ) {
    $backend = Parse::CPAN::Meta-&gt;json_backend();
    $data = $backend-&gt;new-&gt;pretty-&gt;canonical-&gt;encode($struct);
  }
  else {
    $backend = Parse::CPAN::Meta-&gt;yaml_backend();
    $data = eval { no strict 'refs'; &amp;{"$backend\::Dump"}($struct) };
    if ( $@ ) {
      croak $backend-&gt;can('errstr') ? $backend-&gt;errstr : $@
    }
  }

  return $data;
}

# Used by JSON::PP, etc. for "convert_blessed"
sub TO_JSON {
  return { %{ $_[0] } };
}

1;

# ABSTRACT: the distribution metadata for a CPAN dist



=pod

=head1 NAME

CPAN::Meta - the distribution metadata for a CPAN dist

=head1 VERSION

version 2.120921

=head1 SYNOPSIS

  my $meta = CPAN::Meta-&gt;load_file('META.json');

  printf "testing requirements for %s version %s\n",
    $meta-&gt;name,
    $meta-&gt;version;

  my $prereqs = $meta-&gt;requirements_for('configure');

  for my $module ($prereqs-&gt;required_modules) {
    my $version = get_local_version($module);

    die "missing required module $module" unless defined $version;
    die "version for $module not in range"
      unless $prereqs-&gt;accepts_module($module, $version);
  }

=head1 DESCRIPTION

Software distributions released to the CPAN include a F&lt;META.json&gt; or, for
older distributions, F&lt;META.yml&gt;, which describes the distribution, its
contents, and the requirements for building and installing the distribution.
The data structure stored in the F&lt;META.json&gt; file is described in
L&lt;CPAN::Meta::Spec&gt;.

CPAN::Meta provides a simple class to represent this distribution metadata (or
I&lt;distmeta&gt;), along with some helpful methods for interrogating that data.

The documentation below is only for the methods of the CPAN::Meta object.  For
information on the meaning of individual fields, consult the spec.

=head1 METHODS

=head2 new

  my $meta = CPAN::Meta-&gt;new($distmeta_struct, \%options);

Returns a valid CPAN::Meta object or dies if the supplied metadata hash
reference fails to validate.  Older-format metadata will be up-converted to
version 2 if they validate against the original stated specification.

It takes an optional hashref of options. Valid options include:

=over

=item *

lazy_validation -- if true, new will attempt to convert the given metadata
to version 2 before attempting to validate it.  This means than any
fixable errors will be handled by CPAN::Meta::Converter before validation.
(Note that this might result in invalid optional data being silently
dropped.)  The default is false.

=back

=head2 create

  my $meta = CPAN::Meta-&gt;create($distmeta_struct, \%options);

This is same as C&lt;new()&gt;, except that C&lt;generated_by&gt; and C&lt;meta-spec&gt; fields
will be generated if not provided.  This means the metadata structure is
assumed to otherwise follow the latest L&lt;CPAN::Meta::Spec&gt;.

=head2 load_file

  my $meta = CPAN::Meta-&gt;load_file($distmeta_file, \%options);

Given a pathname to a file containing metadata, this deserializes the file
according to its file suffix and constructs a new C&lt;CPAN::Meta&gt; object, just
like C&lt;new()&gt;.  It will die if the deserialized version fails to validate
against its stated specification version.

It takes the same options as C&lt;new()&gt; but C&lt;lazy_validation&gt; defaults to
true.

=head2 load_yaml_string

  my $meta = CPAN::Meta-&gt;load_yaml_string($yaml, \%options);

This method returns a new CPAN::Meta object using the first document in the
given YAML string.  In other respects it is identical to C&lt;load_file()&gt;.

=head2 load_json_string

  my $meta = CPAN::Meta-&gt;load_json_string($json, \%options);

This method returns a new CPAN::Meta object using the structure represented by
the given JSON string.  In other respects it is identical to C&lt;load_file()&gt;.

=head2 save

  $meta-&gt;save($distmeta_file, \%options);

Serializes the object as JSON and writes it to the given file.  The only valid
option is C&lt;version&gt;, which defaults to '2'. On Perl 5.8.1 or later, the file
is saved with UTF-8 encoding.

For C&lt;version&gt; 2 (or higher), the filename should end in '.json'.  L&lt;JSON::PP&gt;
is the default JSON backend. Using another JSON backend requires L&lt;JSON&gt; 2.5 or
later and you must set the C&lt;$ENV{PERL_JSON_BACKEND}&gt; to a supported alternate
backend like L&lt;JSON::XS&gt;.

For C&lt;version&gt; less than 2, the filename should end in '.yml'.
L&lt;CPAN::Meta::Converter&gt; is used to generate an older metadata structure, which
is serialized to YAML.  CPAN::Meta::YAML is the default YAML backend.  You may
set the C&lt;$ENV{PERL_YAML_BACKEND}&gt; to a supported alternative backend, though
this is not recommended due to subtle incompatibilities between YAML parsers on
CPAN.

=head2 meta_spec_version

This method returns the version part of the C&lt;meta_spec&gt; entry in the distmeta
structure.  It is equivalent to:

  $meta-&gt;meta_spec-&gt;{version};

=head2 effective_prereqs

  my $prereqs = $meta-&gt;effective_prereqs;

  my $prereqs = $meta-&gt;effective_prereqs( \@feature_identifiers );

This method returns a L&lt;CPAN::Meta::Prereqs&gt; object describing all the
prereqs for the distribution.  If an arrayref of feature identifiers is given,
the prereqs for the identified features are merged together with the
distribution's core prereqs before the CPAN::Meta::Prereqs object is returned.

=head2 should_index_file

  ... if $meta-&gt;should_index_file( $filename );

This method returns true if the given file should be indexed.  It decides this
by checking the C&lt;file&gt; and C&lt;directory&gt; keys in the C&lt;no_index&gt; property of
the distmeta structure.

C&lt;$filename&gt; should be given in unix format.

=head2 should_index_package

  ... if $meta-&gt;should_index_package( $package );

This method returns true if the given package should be indexed.  It decides
this by checking the C&lt;package&gt; and C&lt;namespace&gt; keys in the C&lt;no_index&gt;
property of the distmeta structure.

=head2 features

  my @feature_objects = $meta-&gt;features;

This method returns a list of L&lt;CPAN::Meta::Feature&gt; objects, one for each
optional feature described by the distribution's metadata.

=head2 feature

  my $feature_object = $meta-&gt;feature( $identifier );

This method returns a L&lt;CPAN::Meta::Feature&gt; object for the optional feature
with the given identifier.  If no feature with that identifier exists, an
exception will be raised.

=head2 as_struct

  my $copy = $meta-&gt;as_struct( \%options );

This method returns a deep copy of the object's metadata as an unblessed has
reference.  It takes an optional hashref of options.  If the hashref contains
a C&lt;version&gt; argument, the copied metadata will be converted to the version
of the specification and returned.  For example:

  my $old_spec = $meta-&gt;as_struct( {version =&gt; "1.4"} );

=head2 as_string

  my $string = $meta-&gt;as_string( \%options );

This method returns a serialized copy of the object's metadata as a character
string.  (The strings are B&lt;not&gt; UTF-8 encoded.)  It takes an optional hashref
of options.  If the hashref contains a C&lt;version&gt; argument, the copied metadata
will be converted to the version of the specification and returned.  For
example:

  my $string = $meta-&gt;as_struct( {version =&gt; "1.4"} );

For C&lt;version&gt; greater than or equal to 2, the string will be serialized as
JSON.  For C&lt;version&gt; less than 2, the string will be serialized as YAML.  In
both cases, the same rules are followed as in the C&lt;save()&gt; method for choosing
a serialization backend.

=head1 STRING DATA

The following methods return a single value, which is the value for the
corresponding entry in the distmeta structure.  Values should be either undef
or strings.

=over 4

=item *

abstract

=item *

description

=item *

dynamic_config

=item *

generated_by

=item *

name

=item *

release_status

=item *

version

=back

=head1 LIST DATA

These methods return lists of string values, which might be represented in the
distmeta structure as arrayrefs or scalars:

=over 4

=item *

authors

=item *

keywords

=item *

licenses

=back

The C&lt;authors&gt; and C&lt;licenses&gt; methods may also be called as C&lt;author&gt; and
C&lt;license&gt;, respectively, to match the field name in the distmeta structure.

=head1 MAP DATA

These readers return hashrefs of arbitrary unblessed data structures, each
described more fully in the specification:

=over 4

=item *

meta_spec

=item *

resources

=item *

provides

=item *

no_index

=item *

prereqs

=item *

optional_features

=back

=head1 CUSTOM DATA

A list of custom keys are available from the C&lt;custom_keys&gt; method and
particular keys may be retrieved with the C&lt;custom&gt; method.

  say $meta-&gt;custom($_) for $meta-&gt;custom_keys;

If a custom key refers to a data structure, a deep clone is returned.

=for Pod::Coverage TO_JSON abstract author authors custom custom_keys description dynamic_config
generated_by keywords license licenses meta_spec name no_index
optional_features prereqs provides release_status resources version

=head1 BUGS

Please report any bugs or feature using the CPAN Request Tracker.
Bugs can be submitted through the web interface at
L&lt;http://rt.cpan.org/Dist/Display.html?Queue=CPAN-Meta&gt;

When submitting a bug or request, please include a test-file or a patch to an
existing test-file that illustrates the bug or desired feature.

=head1 SEE ALSO

=over 4

=item *

L&lt;CPAN::Meta::Converter&gt;

=item *

L&lt;CPAN::Meta::Validator&gt;

=back

=for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan

=head1 SUPPORT

=head2 Bugs / Feature Requests

Please report any bugs or feature requests through the issue tracker
at L&lt;http://rt.cpan.org/Public/Dist/Display.html?Name=CPAN-Meta&gt;.
You will be notified automatically of any progress on your issue.

=head2 Source Code

This is open source software.  The code repository is available for
public review and contribution under the terms of the license.

L&lt;http://github.com/dagolden/cpan-meta&gt;

  git clone git://github.com/dagolden/cpan-meta.git

=head1 AUTHORS

=over 4

=item *

David Golden &lt;dagolden@cpan.org&gt;

=item *

Ricardo Signes &lt;rjbs@cpan.org&gt;

=back

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2010 by David Golden and Ricardo Signes.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut


__END__


</pre></body></html>