<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">use strict;
use warnings;
package CPAN::Meta::Requirements;
our $VERSION = '2.125'; # VERSION
# ABSTRACT: a set of version requirements for a CPAN dist


use Carp ();
use Scalar::Util ();
use version 0.77 (); # the -&gt;parse method


my @valid_options = qw( bad_version_hook );

sub new {
  my ($class, $options) = @_;
  $options ||= {};
  Carp::croak "Argument to $class\-&gt;new() must be a hash reference"
    unless ref $options eq 'HASH';
  my %self = map {; $_ =&gt; $options-&gt;{$_}} @valid_options;

  return bless \%self =&gt; $class;
}

sub _version_object {
  my ($self, $version) = @_;

  my $vobj;

  eval {
    $vobj  = (! defined $version)                ? version-&gt;parse(0)
           : (! Scalar::Util::blessed($version)) ? version-&gt;parse($version)
           :                                       $version;
  };

  if ( my $err = $@ ) {
    my $hook = $self-&gt;{bad_version_hook};
    $vobj = eval { $hook-&gt;($version) }
      if ref $hook eq 'CODE';
    unless (Scalar::Util::blessed($vobj) &amp;&amp; $vobj-&gt;isa("version")) {
      $err =~ s{ at .* line \d+.*$}{};
      die "Can't convert '$version': $err";
    }
  }

  # ensure no leading '.'
  if ( $vobj =~ m{\A\.} ) {
    $vobj = version-&gt;parse("0$vobj");
  }

  # ensure normal v-string form
  if ( $vobj-&gt;is_qv ) {
    $vobj = version-&gt;parse($vobj-&gt;normal);
  }

  return $vobj;
}


BEGIN {
  for my $type (qw(minimum maximum exclusion exact_version)) {
    my $method = "with_$type";
    my $to_add = $type eq 'exact_version' ? $type : "add_$type";

    my $code = sub {
      my ($self, $name, $version) = @_;

      $version = $self-&gt;_version_object( $version );

      $self-&gt;__modify_entry_for($name, $method, $version);

      return $self;
    };
    
    no strict 'refs';
    *$to_add = $code;
  }
}


sub add_requirements {
  my ($self, $req) = @_;

  for my $module ($req-&gt;required_modules) {
    my $modifiers = $req-&gt;__entry_for($module)-&gt;as_modifiers;
    for my $modifier (@$modifiers) {
      my ($method, @args) = @$modifier;
      $self-&gt;$method($module =&gt; @args);
    };
  }

  return $self;
}


sub accepts_module {
  my ($self, $module, $version) = @_;

  $version = $self-&gt;_version_object( $version );

  return 1 unless my $range = $self-&gt;__entry_for($module);
  return $range-&gt;_accepts($version);
}


sub clear_requirement {
  my ($self, $module) = @_;

  return $self unless $self-&gt;__entry_for($module);

  Carp::confess("can't clear requirements on finalized requirements")
    if $self-&gt;is_finalized;

  delete $self-&gt;{requirements}{ $module };

  return $self;
}


sub requirements_for_module {
  my ($self, $module) = @_;
  my $entry = $self-&gt;__entry_for($module);
  return unless $entry;
  return $entry-&gt;as_string;
}


sub required_modules { keys %{ $_[0]{requirements} } }


sub clone {
  my ($self) = @_;
  my $new = (ref $self)-&gt;new;

  return $new-&gt;add_requirements($self);
}

sub __entry_for     { $_[0]{requirements}{ $_[1] } }

sub __modify_entry_for {
  my ($self, $name, $method, $version) = @_;

  my $fin = $self-&gt;is_finalized;
  my $old = $self-&gt;__entry_for($name);

  Carp::confess("can't add new requirements to finalized requirements")
    if $fin and not $old;

  my $new = ($old || 'CPAN::Meta::Requirements::_Range::Range')
          -&gt;$method($version);

  Carp::confess("can't modify finalized requirements")
    if $fin and $old-&gt;as_string ne $new-&gt;as_string;

  $self-&gt;{requirements}{ $name } = $new;
}


sub is_simple {
  my ($self) = @_;
  for my $module ($self-&gt;required_modules) {
    # XXX: This is a complete hack, but also entirely correct.
    return if $self-&gt;__entry_for($module)-&gt;as_string =~ /\s/;
  }

  return 1;
}


sub is_finalized { $_[0]{finalized} }


sub finalize { $_[0]{finalized} = 1 }


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

  my %hash = map {; $_ =&gt; $self-&gt;{requirements}{$_}-&gt;as_string }
             $self-&gt;required_modules;

  return \%hash;
}


my %methods_for_op = (
  '==' =&gt; [ qw(exact_version) ],
  '!=' =&gt; [ qw(add_exclusion) ],
  '&gt;=' =&gt; [ qw(add_minimum)   ],
  '&lt;=' =&gt; [ qw(add_maximum)   ],
  '&gt;'  =&gt; [ qw(add_minimum add_exclusion) ],
  '&lt;'  =&gt; [ qw(add_maximum add_exclusion) ],
);

sub add_string_requirement {
  my ($self, $module, $req) = @_;

  Carp::confess("No requirement string provided for $module")
    unless defined $req &amp;&amp; length $req;

  my @parts = split qr{\s*,\s*}, $req;


  for my $part (@parts) {
    my ($op, $ver) = $part =~ m{\A\s*(==|&gt;=|&gt;|&lt;=|&lt;|!=)\s*(.*)\z};

    if (! defined $op) {
      $self-&gt;add_minimum($module =&gt; $part);
    } else {
      Carp::confess("illegal requirement string: $req")
        unless my $methods = $methods_for_op{ $op };

      $self-&gt;$_($module =&gt; $ver) for @$methods;
    }
  }
}


sub from_string_hash {
  my ($class, $hash) = @_;

  my $self = $class-&gt;new;

  for my $module (keys %$hash) {
    my $req = $hash-&gt;{$module};
    unless ( defined $req &amp;&amp; length $req ) {
      $req = 0;
      Carp::carp("Undefined requirement for $module treated as '0'");
    }
    $self-&gt;add_string_requirement($module, $req);
  }

  return $self;
}

##############################################################

{
  package
    CPAN::Meta::Requirements::_Range::Exact;
  sub _new     { bless { version =&gt; $_[1] } =&gt; $_[0] }

  sub _accepts { return $_[0]{version} == $_[1] }

  sub as_string { return "== $_[0]{version}" }

  sub as_modifiers { return [ [ exact_version =&gt; $_[0]{version} ] ] }

  sub _clone {
    (ref $_[0])-&gt;_new( version-&gt;new( $_[0]{version} ) )
  }

  sub with_exact_version {
    my ($self, $version) = @_;

    return $self-&gt;_clone if $self-&gt;_accepts($version);

    Carp::confess("illegal requirements: unequal exact version specified");
  }

  sub with_minimum {
    my ($self, $minimum) = @_;
    return $self-&gt;_clone if $self-&gt;{version} &gt;= $minimum;
    Carp::confess("illegal requirements: minimum above exact specification");
  }

  sub with_maximum {
    my ($self, $maximum) = @_;
    return $self-&gt;_clone if $self-&gt;{version} &lt;= $maximum;
    Carp::confess("illegal requirements: maximum below exact specification");
  }

  sub with_exclusion {
    my ($self, $exclusion) = @_;
    return $self-&gt;_clone unless $exclusion == $self-&gt;{version};
    Carp::confess("illegal requirements: excluded exact specification");
  }
}

##############################################################

{
  package
    CPAN::Meta::Requirements::_Range::Range;

  sub _self { ref($_[0]) ? $_[0] : (bless { } =&gt; $_[0]) }

  sub _clone {
    return (bless { } =&gt; $_[0]) unless ref $_[0];

    my ($s) = @_;
    my %guts = (
      (exists $s-&gt;{minimum} ? (minimum =&gt; version-&gt;new($s-&gt;{minimum})) : ()),
      (exists $s-&gt;{maximum} ? (maximum =&gt; version-&gt;new($s-&gt;{maximum})) : ()),

      (exists $s-&gt;{exclusions}
        ? (exclusions =&gt; [ map { version-&gt;new($_) } @{ $s-&gt;{exclusions} } ])
        : ()),
    );

    bless \%guts =&gt; ref($s);
  }

  sub as_modifiers {
    my ($self) = @_;
    my @mods;
    push @mods, [ add_minimum =&gt; $self-&gt;{minimum} ] if exists $self-&gt;{minimum};
    push @mods, [ add_maximum =&gt; $self-&gt;{maximum} ] if exists $self-&gt;{maximum};
    push @mods, map {; [ add_exclusion =&gt; $_ ] } @{$self-&gt;{exclusions} || []};
    return \@mods;
  }

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

    return 0 if ! keys %$self;

    return "$self-&gt;{minimum}" if (keys %$self) == 1 and exists $self-&gt;{minimum};

    my @exclusions = @{ $self-&gt;{exclusions} || [] };

    my @parts;

    for my $pair (
      [ qw( &gt;= &gt; minimum ) ],
      [ qw( &lt;= &lt; maximum ) ],
    ) {
      my ($op, $e_op, $k) = @$pair;
      if (exists $self-&gt;{$k}) {
        my @new_exclusions = grep { $_ != $self-&gt;{ $k } } @exclusions;
        if (@new_exclusions == @exclusions) {
          push @parts, "$op $self-&gt;{ $k }";
        } else {
          push @parts, "$e_op $self-&gt;{ $k }";
          @exclusions = @new_exclusions;
        }
      }
    }

    push @parts, map {; "!= $_" } @exclusions;

    return join q{, }, @parts;
  }

  sub with_exact_version {
    my ($self, $version) = @_;
    $self = $self-&gt;_clone;

    Carp::confess("illegal requirements: exact specification outside of range")
      unless $self-&gt;_accepts($version);

    return CPAN::Meta::Requirements::_Range::Exact-&gt;_new($version);
  }

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

    if (defined $self-&gt;{minimum} and defined $self-&gt;{maximum}) {
      if ($self-&gt;{minimum} == $self-&gt;{maximum}) {
        Carp::confess("illegal requirements: excluded all values")
          if grep { $_ == $self-&gt;{minimum} } @{ $self-&gt;{exclusions} || [] };

        return CPAN::Meta::Requirements::_Range::Exact-&gt;_new($self-&gt;{minimum})
      }

      Carp::confess("illegal requirements: minimum exceeds maximum")
        if $self-&gt;{minimum} &gt; $self-&gt;{maximum};
    }

    # eliminate irrelevant exclusions
    if ($self-&gt;{exclusions}) {
      my %seen;
      @{ $self-&gt;{exclusions} } = grep {
        (! defined $self-&gt;{minimum} or $_ &gt;= $self-&gt;{minimum})
        and
        (! defined $self-&gt;{maximum} or $_ &lt;= $self-&gt;{maximum})
        and
        ! $seen{$_}++
      } @{ $self-&gt;{exclusions} };
    }

    return $self;
  }

  sub with_minimum {
    my ($self, $minimum) = @_;
    $self = $self-&gt;_clone;

    if (defined (my $old_min = $self-&gt;{minimum})) {
      $self-&gt;{minimum} = (sort { $b cmp $a } ($minimum, $old_min))[0];
    } else {
      $self-&gt;{minimum} = $minimum;
    }

    return $self-&gt;_simplify;
  }

  sub with_maximum {
    my ($self, $maximum) = @_;
    $self = $self-&gt;_clone;

    if (defined (my $old_max = $self-&gt;{maximum})) {
      $self-&gt;{maximum} = (sort { $a cmp $b } ($maximum, $old_max))[0];
    } else {
      $self-&gt;{maximum} = $maximum;
    }

    return $self-&gt;_simplify;
  }

  sub with_exclusion {
    my ($self, $exclusion) = @_;
    $self = $self-&gt;_clone;

    push @{ $self-&gt;{exclusions} ||= [] }, $exclusion;

    return $self-&gt;_simplify;
  }

  sub _accepts {
    my ($self, $version) = @_;

    return if defined $self-&gt;{minimum} and $version &lt; $self-&gt;{minimum};
    return if defined $self-&gt;{maximum} and $version &gt; $self-&gt;{maximum};
    return if defined $self-&gt;{exclusions}
          and grep { $version == $_ } @{ $self-&gt;{exclusions} };

    return 1;
  }
}

1;
# vim: ts=2 sts=2 sw=2 et:

__END__

=pod

=encoding utf-8

=head1 NAME

CPAN::Meta::Requirements - a set of version requirements for a CPAN dist

=head1 VERSION

version 2.125

=head1 SYNOPSIS

  use CPAN::Meta::Requirements;

  my $build_requires = CPAN::Meta::Requirements-&gt;new;

  $build_requires-&gt;add_minimum('Library::Foo' =&gt; 1.208);

  $build_requires-&gt;add_minimum('Library::Foo' =&gt; 2.602);

  $build_requires-&gt;add_minimum('Module::Bar'  =&gt; 'v1.2.3');

  $METAyml-&gt;{build_requires} = $build_requires-&gt;as_string_hash;

=head1 DESCRIPTION

A CPAN::Meta::Requirements object models a set of version constraints like
those specified in the F&lt;META.yml&gt; or F&lt;META.json&gt; files in CPAN distributions.
It can be built up by adding more and more constraints, and it will reduce them
to the simplest representation.

Logically impossible constraints will be identified immediately by thrown
exceptions.

=head1 METHODS

=head2 new

  my $req = CPAN::Meta::Requirements-&gt;new;

This returns a new CPAN::Meta::Requirements object.  It takes an optional
hash reference argument.  The following keys are supported:

=over 4

=item *

&lt;bad_version_hook&gt; -- if provided, when a version cannot be parsed into

a version object, this code reference will be called with the invalid version
string as an argument.  It must return a valid version object.

=back

All other keys are ignored.

=head2 add_minimum

  $req-&gt;add_minimum( $module =&gt; $version );

This adds a new minimum version requirement.  If the new requirement is
redundant to the existing specification, this has no effect.

Minimum requirements are inclusive.  C&lt;$version&gt; is required, along with any
greater version number.

This method returns the requirements object.

=head2 add_maximum

  $req-&gt;add_maximum( $module =&gt; $version );

This adds a new maximum version requirement.  If the new requirement is
redundant to the existing specification, this has no effect.

Maximum requirements are inclusive.  No version strictly greater than the given
version is allowed.

This method returns the requirements object.

=head2 add_exclusion

  $req-&gt;add_exclusion( $module =&gt; $version );

This adds a new excluded version.  For example, you might use these three
method calls:

  $req-&gt;add_minimum( $module =&gt; '1.00' );
  $req-&gt;add_maximum( $module =&gt; '1.82' );

  $req-&gt;add_exclusion( $module =&gt; '1.75' );

Any version between 1.00 and 1.82 inclusive would be acceptable, except for
1.75.

This method returns the requirements object.

=head2 exact_version

  $req-&gt;exact_version( $module =&gt; $version );

This sets the version required for the given module to I&lt;exactly&gt; the given
version.  No other version would be considered acceptable.

This method returns the requirements object.

=head2 add_requirements

  $req-&gt;add_requirements( $another_req_object );

This method adds all the requirements in the given CPAN::Meta::Requirements object
to the requirements object on which it was called.  If there are any conflicts,
an exception is thrown.

This method returns the requirements object.

=head2 accepts_module

  my $bool = $req-&gt;accepts_modules($module =&gt; $version);

Given an module and version, this method returns true if the version
specification for the module accepts the provided version.  In other words,
given:

  Module =&gt; '&gt;= 1.00, &lt; 2.00'

We will accept 1.00 and 1.75 but not 0.50 or 2.00.

For modules that do not appear in the requirements, this method will return
true.

=head2 clear_requirement

  $req-&gt;clear_requirement( $module );

This removes the requirement for a given module from the object.

This method returns the requirements object.

=head2 requirements_for_module

  $req-&gt;requirements_for_module( $module );

This returns a string containing the version requirements for a given module in
the format described in L&lt;CPAN::Meta::Spec&gt; or undef if the given module has no
requirements. This should only be used for informational purposes such as error
messages and should not be interpreted or used for comparison (see
L&lt;/accepts_module&gt; instead.)

=head2 required_modules

This method returns a list of all the modules for which requirements have been
specified.

=head2 clone

  $req-&gt;clone;

This method returns a clone of the invocant.  The clone and the original object
can then be changed independent of one another.

=head2 is_simple

This method returns true if and only if all requirements are inclusive minimums
-- that is, if their string expression is just the version number.

=head2 is_finalized

This method returns true if the requirements have been finalized by having the
C&lt;finalize&gt; method called on them.

=head2 finalize

This method marks the requirements finalized.  Subsequent attempts to change
the requirements will be fatal, I&lt;if&gt; they would result in a change.  If they
would not alter the requirements, they have no effect.

If a finalized set of requirements is cloned, the cloned requirements are not
also finalized.

=head2 as_string_hash

This returns a reference to a hash describing the requirements using the
strings in the F&lt;META.yml&gt; specification.

For example after the following program:

  my $req = CPAN::Meta::Requirements-&gt;new;

  $req-&gt;add_minimum('CPAN::Meta::Requirements' =&gt; 0.102);

  $req-&gt;add_minimum('Library::Foo' =&gt; 1.208);

  $req-&gt;add_maximum('Library::Foo' =&gt; 2.602);

  $req-&gt;add_minimum('Module::Bar'  =&gt; 'v1.2.3');

  $req-&gt;add_exclusion('Module::Bar'  =&gt; 'v1.2.8');

  $req-&gt;exact_version('Xyzzy'  =&gt; '6.01');

  my $hashref = $req-&gt;as_string_hash;

C&lt;$hashref&gt; would contain:

  {
    'CPAN::Meta::Requirements' =&gt; '0.102',
    'Library::Foo' =&gt; '&gt;= 1.208, &lt;= 2.206',
    'Module::Bar'  =&gt; '&gt;= v1.2.3, != v1.2.8',
    'Xyzzy'        =&gt; '== 6.01',
  }

=head2 add_string_requirement

  $req-&gt;add_string_requirement('Library::Foo' =&gt; '&gt;= 1.208, &lt;= 2.206');

This method parses the passed in string and adds the appropriate requirement
for the given module.  It understands version ranges as described in the
L&lt;CPAN::Meta::Spec/Version Ranges&gt;. For example:

=over 4

=item 1.3

=item &gt;= 1.3

=item &lt;= 1.3

=item == 1.3

=item != 1.3

=item &gt; 1.3

=item &lt; 1.3

=item &gt;= 1.3, != 1.5, &lt;= 2.0

A version number without an operator is equivalent to specifying a minimum
(C&lt;E&lt;gt&gt;=&gt;).  Extra whitespace is allowed.

=back

=head2 from_string_hash

  my $req = CPAN::Meta::Requirements-&gt;from_string_hash( \%hash );

This is an alternate constructor for a CPAN::Meta::Requirements object.  It takes
a hash of module names and version requirement strings and returns a new
CPAN::Meta::Requirements object.

=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;https://github.com/dagolden/CPAN-Meta-Requirements/issues&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;https://github.com/dagolden/CPAN-Meta-Requirements&gt;

  git clone https://github.com/dagolden/CPAN-Meta-Requirements.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
</pre></body></html>