# perldoc > CPAN::Meta::Requirements

---
type: CommandReference
command: CPAN::Meta::Requirements
mode: perldoc
section: 3
source: perldoc
---

## Quick Reference

- `CPAN::Meta::Requirements->new` — create a new requirements object
- `$req->add_minimum($module => $version)` — add an inclusive minimum version requirement
- `$req->add_maximum($module => $version)` — add an inclusive maximum version requirement
- `$req->add_exclusion($module => $version)` — add an excluded version
- `$req->exact_version($module => $version)` — set an exact version requirement
- `$req->accepts_module($module => $version)` — check if a version satisfies the requirements
- `$req->as_string_hash` — return requirements as a hash of version strings
- `$req->from_string_hash(\%hash)` — construct object from a hash of version strings

## Name

a set of version requirements for a CPAN dist

## Synopsis

perl
use CPAN::Meta::Requirements;

my $build_requires = CPAN::Meta::Requirements->new;

$build_requires->add_minimum('Library::Foo' => 1.208);
$build_requires->add_minimum('Library::Foo' => 2.602);
$build_requires->add_minimum('Module::Bar'  => 'v1.2.3');

$METAyml->{build_requires} = $build_requires->as_string_hash;
## Description

A `CPAN::Meta::Requirements` object models a set of version constraints like those specified in META.yml or META.json files in CPAN distributions, as defined by [CPAN::Meta::Spec](https://metacpan.org/pod/CPAN::Meta::Spec). It can be built up by adding more and more constraints, reducing them to the simplest representation. Logically impossible constraints are identified immediately by thrown exceptions.

## Methods

- `new` — `my $req = CPAN::Meta::Requirements->new`  
  Returns a new requirements object. Optional hash reference argument: `bad_version_hook` — a code reference called when a version cannot be parsed (receives invalid version string and module name, must return a valid version object). All other keys are ignored.

- `add_minimum` — `$req->add_minimum($module => $version)`  
  Adds an inclusive minimum version requirement. Ignores redundant constraints. Returns the requirements object.

- `add_maximum` — `$req->add_maximum($module => $version)`  
  Adds an inclusive maximum version requirement (no version strictly greater than the given version is allowed). Ignores redundant constraints. Returns the requirements object.

- `add_exclusion` — `$req->add_exclusion($module => $version)`  
  Adds an excluded version. For example: require >= 1.00, <= 1.82, except 1.75. Returns the requirements object.

- `exact_version` — `$req->exact_version($module => $version)`  
  Sets the version required for the given module to exactly the given version. No other version is acceptable. Returns the requirements object.

- `add_requirements` — `$req->add_requirements($another_req_object)`  
  Merges all requirements from another `CPAN::Meta::Requirements` object. Throws an exception on conflicts. Returns the requirements object.

- `accepts_module` — `my $bool = $req->accepts_module($module => $version)`  
  Returns true if the version specification for the module accepts the provided version. For modules not in the requirements, returns true.

- `clear_requirement` — `$req->clear_requirement($module)`  
  Removes the requirement for a given module. Returns the requirements object.

- `requirements_for_module` — `$req->requirements_for_module($module)`  
  Returns a string containing the version requirements in [CPAN::Meta::Spec](https://metacpan.org/pod/CPAN::Meta::Spec) format, or undef if none. For informational purposes only.

- `structured_requirements_for_module` — `$req->structured_requirements_for_module($module)`  
  Returns a data structure (or undef) of version requirements. Added in version 2.134. Not for version checks.

- `required_modules` — `my @modules = $req->required_modules`  
  Returns a list of all modules for which requirements have been specified.

- `clone` — `$req->clone`  
  Returns a clone of the invocant. The clone and original can be changed independently.

- `is_simple` — `my $bool = $req->is_simple`  
  Returns true if all requirements are inclusive minimums (string expression is just the version number).

- `is_finalized` — `my $bool = $req->is_finalized`  
  Returns true if the requirements have been finalized via `finalize`.

- `finalize` — `$req->finalize`  
  Marks the requirements finalized. Subsequent changes that would alter the requirements are fatal. Cloned requirements are not finalized.

- `as_string_hash` — `my $hashref = $req->as_string_hash`  
  Returns a hash reference of module names to version strings in [CPAN::Meta::Spec](https://metacpan.org/pod/CPAN::Meta::Spec) format. Example output:
  ```perl
  {
    'CPAN::Meta::Requirements' => '0.102',
    'Library::Foo' => '>= 1.208, <= 2.206',
    'Module::Bar'  => '>= v1.2.3, != v1.2.8',
    'Xyzzy'        => '== 6.01',
  }
  
- `add_string_requirement` — `$req->add_string_requirement($module => $string)`  
  Parses a version string (or Perl v-string) and adds the appropriate requirement. Understands version ranges as described in [CPAN::Meta::Spec](https://metacpan.org/pod/CPAN::Meta::Spec). Examples: `1.3`, `>= 1.3`, `<= 1.3`, `== 1.3`, `!= 1.3`, `> 1.3`, `< 1.3`, `>= 1.3, != 1.5, <= 2.0`. A version number without an operator is equivalent to minimum (`>=`).

- `from_string_hash` — `my $req = CPAN::Meta::Requirements->from_string_hash(\%hash, \%opts)`  
  Alternate constructor. Takes a hash of module names and version requirement strings (or v-strings). Optionally accepts a hash-reference of options (same as `new`).

## See Also

- [CPAN::Meta::Spec](https://metacpan.org/pod/CPAN::Meta::Spec) — version specification format
- [CPAN::Meta](https://metacpan.org/pod/CPAN::Meta) — CPAN distribution metadata