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

our $VERSION = '2.143';

use Carp ();

#pod =head1 SYNOPSIS
#pod
#pod   use CPAN::Meta::Requirements::Range;
#pod
#pod   my $range = CPAN::Meta::Requirements::Range-&gt;with_minimum(1);
#pod
#pod   $range = $range-&gt;with_maximum('v2.2');
#pod
#pod   my $stringified = $range-&gt;as_string;
#pod
#pod =head1 DESCRIPTION
#pod
#pod A CPAN::Meta::Requirements::Range object models a set of version constraints like
#pod those specified in the F&lt;META.yml&gt; or F&lt;META.json&gt; files in CPAN distributions,
#pod and as defined by L&lt;CPAN::Meta::Spec&gt;;
#pod It can be built up by adding more and more constraints, and it will reduce them
#pod to the simplest representation.
#pod
#pod Logically impossible constraints will be identified immediately by thrown
#pod exceptions.
#pod
#pod =cut

use Carp ();

package
  CPAN::Meta::Requirements::Range::_Base;

# To help ExtUtils::MakeMaker bootstrap CPAN::Meta::Requirements on perls
# before 5.10, we fall back to the EUMM bundled compatibility version module if
# that's the only thing available.  This shouldn't ever happen in a normal CPAN
# install of CPAN::Meta::Requirements, as version.pm will be picked up from
# prereqs and be available at runtime.

BEGIN {
  eval "use version ()"; ## no critic
  if ( my $err = $@ ) {
    eval "use ExtUtils::MakeMaker::version" or die $err; ## no critic
  }
}

# from version::vpp
sub _find_magic_vstring {
  my $value = shift;
  my $tvalue = '';
  require B;
  my $sv = B::svref_2object(\$value);
  my $magic = ref($sv) eq 'B::PVMG' ? $sv-&gt;MAGIC : undef;
  while ( $magic ) {
    if ( $magic-&gt;TYPE eq 'V' ) {
      $tvalue = $magic-&gt;PTR;
      $tvalue =~ s/^v?(.+)$/v$1/;
      last;
    }
    else {
      $magic = $magic-&gt;MOREMAGIC;
    }
  }
  return $tvalue;
}

# Perl 5.10.0 didn't have "is_qv" in version.pm
*_is_qv = version-&gt;can('is_qv') ? sub { $_[0]-&gt;is_qv } : sub { exists $_[0]-&gt;{qv} };

# construct once, reuse many times
my $V0 = version-&gt;new(0);

# safe if given an unblessed reference
sub _isa_version {
  UNIVERSAL::isa( $_[0], 'UNIVERSAL' ) &amp;&amp; $_[0]-&gt;isa('version')
}

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

  my ($vobj, $err);

  if (not defined $version or (!ref($version) &amp;&amp; $version eq '0')) {
    return $V0;
  }
  elsif ( ref($version) eq 'version' || ( ref($version) &amp;&amp; _isa_version($version) ) ) {
    $vobj = $version;
  }
  else {
    # hack around version::vpp not handling &lt;3 character vstring literals
    if ( $INC{'version/vpp.pm'} || $INC{'ExtUtils/MakeMaker/version/vpp.pm'} ) {
      my $magic = _find_magic_vstring( $version );
      $version = $magic if length $magic;
    }
    # pad to 3 characters if before 5.8.1 and appears to be a v-string
    if ( $] &lt; 5.008001 &amp;&amp; $version !~ /\A[0-9]/ &amp;&amp; substr($version,0,1) ne 'v' &amp;&amp; length($version) &lt; 3 ) {
      $version .= "\0" x (3 - length($version));
    }
    eval {
      local $SIG{__WARN__} = sub { die "Invalid version: $_[0]" };
      # avoid specific segfault on some older version.pm versions
      die "Invalid version: $version" if $version eq 'version';
      $vobj = version-&gt;new($version);
    };
    if ( my $err = $@ ) {
      $vobj = eval { $bad_version_hook-&gt;($version, $module) }
        if ref $bad_version_hook eq 'CODE';
      unless (eval { $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;new("0$vobj");
  }

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

  return $vobj;
}

#pod =method with_string_requirement
#pod
#pod   $req-&gt;with_string_requirement('&gt;= 1.208, &lt;= 2.206');
#pod   $req-&gt;with_string_requirement(v1.208);
#pod
#pod This method parses the passed in string and adds the appropriate requirement.
#pod A version can be a Perl "v-string".  It understands version ranges as described
#pod in the L&lt;CPAN::Meta::Spec/Version Ranges&gt;. For example:
#pod
#pod =over 4
#pod
#pod =item 1.3
#pod
#pod =item &gt;= 1.3
#pod
#pod =item &lt;= 1.3
#pod
#pod =item == 1.3
#pod
#pod =item != 1.3
#pod
#pod =item &gt; 1.3
#pod
#pod =item &lt; 1.3
#pod
#pod =item &gt;= 1.3, != 1.5, &lt;= 2.0
#pod
#pod A version number without an operator is equivalent to specifying a minimum
#pod (C&lt;E&lt;gt&gt;=&gt;).  Extra whitespace is allowed.
#pod
#pod =back
#pod
#pod =cut

my %methods_for_op = (
  '==' =&gt; [ qw(with_exact_version) ],
  '!=' =&gt; [ qw(with_exclusion) ],
  '&gt;=' =&gt; [ qw(with_minimum)   ],
  '&lt;=' =&gt; [ qw(with_maximum)   ],
  '&gt;'  =&gt; [ qw(with_minimum with_exclusion) ],
  '&lt;'  =&gt; [ qw(with_maximum with_exclusion) ],
);

sub with_string_requirement {
  my ($self, $req, $module, $bad_version_hook) = @_;
  $module //= 'module';

  unless ( defined $req &amp;&amp; length $req ) {
    $req = 0;
    Carp::carp("Undefined requirement for $module treated as '0'");
  }

  my $magic = _find_magic_vstring( $req );
  if (length $magic) {
    return $self-&gt;with_minimum($magic, $module, $bad_version_hook);
  }

  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 = $self-&gt;with_minimum($part, $module, $bad_version_hook);
    } else {
      Carp::croak("illegal requirement string: $req")
        unless my $methods = $methods_for_op{ $op };

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

  return $self;
}

#pod =method with_range
#pod
#pod  $range-&gt;with_range($other_range)
#pod
#pod This creates a new range object that is a merge two others.
#pod
#pod =cut

sub with_range {
  my ($self, $other, $module, $bad_version_hook) = @_;
  for my $modifier($other-&gt;_as_modifiers) {
    my ($method, $arg) = @$modifier;
    $self = $self-&gt;$method($arg, $module, $bad_version_hook);
  }
  return $self;
}

package CPAN::Meta::Requirements::Range;

our @ISA = 'CPAN::Meta::Requirements::Range::_Base';

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);
}

#pod =method with_exact_version
#pod
#pod   $range-&gt;with_exact_version( $version );
#pod
#pod This sets the version required to I&lt;exactly&gt; the given
#pod version.  No other version would be considered acceptable.
#pod
#pod This method returns the version range object.
#pod
#pod =cut

sub with_exact_version {
  my ($self, $version, $module, $bad_version_hook) = @_;
  $module //= 'module';
  $self = $self-&gt;_clone;
  $version = $self-&gt;_version_object($version, $module, $bad_version_hook);

  unless ($self-&gt;accepts($version)) {
    $self-&gt;_reject_requirements(
      $module,
      "exact specification $version outside of range " . $self-&gt;as_string
    );
  }

  return CPAN::Meta::Requirements::Range::_Exact-&gt;_new($version);
}

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

  if (defined $self-&gt;{minimum} and defined $self-&gt;{maximum}) {
    if ($self-&gt;{minimum} == $self-&gt;{maximum}) {
      if (grep { $_ == $self-&gt;{minimum} } @{ $self-&gt;{exclusions} || [] }) {
        $self-&gt;_reject_requirements(
          $module,
          "minimum and maximum are both $self-&gt;{minimum}, which is excluded",
        );
      }

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

    if ($self-&gt;{minimum} &gt; $self-&gt;{maximum}) {
      $self-&gt;_reject_requirements(
        $module,
        "minimum $self-&gt;{minimum} exceeds maximum $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;
}

#pod =method with_minimum
#pod
#pod   $range-&gt;with_minimum( $version );
#pod
#pod This adds a new minimum version requirement.  If the new requirement is
#pod redundant to the existing specification, this has no effect.
#pod
#pod Minimum requirements are inclusive.  C&lt;$version&gt; is required, along with any
#pod greater version number.
#pod
#pod This method returns the version range object.
#pod
#pod =cut

sub with_minimum {
  my ($self, $minimum, $module, $bad_version_hook) = @_;
  $module //= 'module';
  $self = $self-&gt;_clone;
  $minimum = $self-&gt;_version_object( $minimum, $module, $bad_version_hook );

  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($module);
}

#pod =method with_maximum
#pod
#pod   $range-&gt;with_maximum( $version );
#pod
#pod This adds a new maximum version requirement.  If the new requirement is
#pod redundant to the existing specification, this has no effect.
#pod
#pod Maximum requirements are inclusive.  No version strictly greater than the given
#pod version is allowed.
#pod
#pod This method returns the version range object.
#pod
#pod =cut

sub with_maximum {
  my ($self, $maximum, $module, $bad_version_hook) = @_;
  $module //= 'module';
  $self = $self-&gt;_clone;
  $maximum = $self-&gt;_version_object( $maximum, $module, $bad_version_hook );

  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($module);
}

#pod =method with_exclusion
#pod
#pod   $range-&gt;with_exclusion( $version );
#pod
#pod This adds a new excluded version.  For example, you might use these three
#pod method calls:
#pod
#pod   $range-&gt;with_minimum( '1.00' );
#pod   $range-&gt;with_maximum( '1.82' );
#pod
#pod   $range-&gt;with_exclusion( '1.75' );
#pod
#pod Any version between 1.00 and 1.82 inclusive would be acceptable, except for
#pod 1.75.
#pod
#pod This method returns the requirements object.
#pod
#pod =cut

sub with_exclusion {
  my ($self, $exclusion, $module, $bad_version_hook) = @_;
  $module //= 'module';
  $self = $self-&gt;_clone;
  $exclusion = $self-&gt;_version_object( $exclusion, $module, $bad_version_hook );

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

  return $self-&gt;_simplify($module);
}

sub _as_modifiers {
  my ($self) = @_;
  my @mods;
  push @mods, [ with_minimum =&gt; $self-&gt;{minimum} ] if exists $self-&gt;{minimum};
  push @mods, [ with_maximum =&gt; $self-&gt;{maximum} ] if exists $self-&gt;{maximum};
  push @mods, map {; [ with_exclusion =&gt; $_ ] } @{$self-&gt;{exclusions} || []};
  return @mods;
}

#pod =method as_struct
#pod
#pod   $range-&gt;as_struct( $module );
#pod
#pod This returns a data structure containing the version requirements. This should
#pod not be used for version checks (see L&lt;/accepts_module&gt; instead).
#pod
#pod =cut

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

  return 0 if ! keys %$self;

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

  my @parts;

  for my $tuple (
    [ qw( &gt;= &gt; minimum ) ],
    [ qw( &lt;= &lt; maximum ) ],
  ) {
    my ($op, $e_op, $k) = @$tuple;
    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 \@parts;
}

#pod =method as_string
#pod
#pod   $range-&gt;as_string;
#pod
#pod This returns a string containing the version requirements in the format
#pod described in L&lt;CPAN::Meta::Spec&gt;. This should only be used for informational
#pod purposes such as error messages and should not be interpreted or used for
#pod comparison (see L&lt;/accepts&gt; instead).
#pod
#pod =cut

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

  my @parts = @{ $self-&gt;as_struct };

  return $parts[0][1] if @parts == 1 and $parts[0][0] eq '&gt;=';

  return join q{, }, map {; join q{ }, @$_ } @parts;
}

sub _reject_requirements {
  my ($self, $module, $error) = @_;
  Carp::croak("illegal requirements for $module: $error")
}

#pod =method accepts
#pod
#pod   my $bool = $range-&gt;accepts($version);
#pod
#pod Given a version, this method returns true if the version specification
#pod accepts the provided version.  In other words, given:
#pod
#pod   '&gt;= 1.00, &lt; 2.00'
#pod
#pod We will accept 1.00 and 1.75 but not 0.50 or 2.00.
#pod
#pod =cut

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;
}

#pod =method is_simple
#pod
#pod This method returns true if and only if the range is an inclusive minimum
#pod -- that is, if their string expression is just the version number.
#pod
#pod =cut

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

  return 1;
}

package
  CPAN::Meta::Requirements::Range::_Exact;

our @ISA = 'CPAN::Meta::Requirements::Range::_Base';

our $VERSION = '2.141';

BEGIN {
  eval "use version ()"; ## no critic
  if ( my $err = $@ ) {
    eval "use ExtUtils::MakeMaker::version" or die $err; ## no critic
  }
}

sub _new      { bless { version =&gt; $_[1] } =&gt; $_[0] }

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

sub _reject_requirements {
  my ($self, $module, $error) = @_;
  Carp::croak("illegal requirements for $module: $error")
}

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

sub with_exact_version {
  my ($self, $version, $module, $bad_version_hook) = @_;
  $module //= 'module';
  $version = $self-&gt;_version_object($version, $module, $bad_version_hook);

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

  $self-&gt;_reject_requirements(
    $module,
    "can't be exactly $version when exact requirement is already $self-&gt;{version}",
  );
}

sub with_minimum {
  my ($self, $minimum, $module, $bad_version_hook) = @_;
  $module //= 'module';
  $minimum = $self-&gt;_version_object( $minimum, $module, $bad_version_hook );

  return $self-&gt;_clone if $self-&gt;{version} &gt;= $minimum;
  $self-&gt;_reject_requirements(
    $module,
    "minimum $minimum exceeds exact specification $self-&gt;{version}",
  );
}

sub with_maximum {
  my ($self, $maximum, $module, $bad_version_hook) = @_;
  $module //= 'module';
  $maximum = $self-&gt;_version_object( $maximum, $module, $bad_version_hook );

  return $self-&gt;_clone if $self-&gt;{version} &lt;= $maximum;
  $self-&gt;_reject_requirements(
    $module,
    "maximum $maximum below exact specification $self-&gt;{version}",
  );
}

sub with_exclusion {
  my ($self, $exclusion, $module, $bad_version_hook) = @_;
  $module //= 'module';
  $exclusion = $self-&gt;_version_object( $exclusion, $module, $bad_version_hook );

  return $self-&gt;_clone unless $exclusion == $self-&gt;{version};
  $self-&gt;_reject_requirements(
    $module,
    "tried to exclude $exclusion, which is already exactly specified",
  );
}

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

sub as_struct { return [ [ '==', "$_[0]{version}" ] ] }

sub _as_modifiers { return [ with_exact_version =&gt; $_[0]{version} ] }


1;

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

__END__

=pod

=encoding UTF-8

=head1 NAME

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

=head1 VERSION

version 2.143

=head1 SYNOPSIS

  use CPAN::Meta::Requirements::Range;

  my $range = CPAN::Meta::Requirements::Range-&gt;with_minimum(1);

  $range = $range-&gt;with_maximum('v2.2');

  my $stringified = $range-&gt;as_string;

=head1 DESCRIPTION

A CPAN::Meta::Requirements::Range 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,
and as defined by L&lt;CPAN::Meta::Spec&gt;;
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 with_string_requirement

  $req-&gt;with_string_requirement('&gt;= 1.208, &lt;= 2.206');
  $req-&gt;with_string_requirement(v1.208);

This method parses the passed in string and adds the appropriate requirement.
A version can be a Perl "v-string".  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 with_range

 $range-&gt;with_range($other_range)

This creates a new range object that is a merge two others.

=head2 with_exact_version

  $range-&gt;with_exact_version( $version );

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

This method returns the version range object.

=head2 with_minimum

  $range-&gt;with_minimum( $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 version range object.

=head2 with_maximum

  $range-&gt;with_maximum( $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 version range object.

=head2 with_exclusion

  $range-&gt;with_exclusion( $version );

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

  $range-&gt;with_minimum( '1.00' );
  $range-&gt;with_maximum( '1.82' );

  $range-&gt;with_exclusion( '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 as_struct

  $range-&gt;as_struct( $module );

This returns a data structure containing the version requirements. This should
not be used for version checks (see L&lt;/accepts_module&gt; instead).

=head2 as_string

  $range-&gt;as_string;

This returns a string containing the version requirements in the format
described in L&lt;CPAN::Meta::Spec&gt;. 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&gt; instead).

=head2 accepts

  my $bool = $range-&gt;accepts($version);

Given a version, this method returns true if the version specification
accepts the provided version.  In other words, given:

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

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

=head2 is_simple

This method returns true if and only if the range is an inclusive minimum
-- that is, if their string expression is just the version number.

=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>