Params-Validate-Dependencies

 view release on metacpan or  search on metacpan

lib/Params/Validate/Dependencies.pm  view on Meta::CPAN

package Params::Validate::Dependencies;

use strict;
use warnings;

use Clone qw(clone);
use Params::Validate (); # don't import yet
use Params::Validate::Dependencies::Documenter;
use Scalar::Util qw(blessed);
use PadWalker qw(closed_over);

use base qw(Exporter);

use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS $DOC);

$VERSION = '2.00';
$DOC = 0;

# copy and update P::V's EXPORT* constants
my @_of = qw(any_of all_of none_of one_of);
@EXPORT = (@Params::Validate::EXPORT, @_of);
@EXPORT_OK = (@Params::Validate::EXPORT_OK, @_of, 'exclusively');
%EXPORT_TAGS = (%{clone(\%Params::Validate::EXPORT_TAGS)}, _of => \@_of);
push @{$EXPORT_TAGS{all}}, (@_of, 'exclusively');

# because repeating the call to _validate_factory_args everywhere is BAD
foreach my $sub (@_of, 'exclusively') {
  no strict 'refs';
  no warnings 'redefine';
  my $orig = \&{$sub};
  *{$sub} = sub {
    local *__ANON__ = $sub;
    _validate_factory_args(@_);
    $orig->(@_);
  };
}

sub import {
  # import all of P::V except validate() and dvalidate_with()
  Params::Validate->import(grep { ! /^validate(_with)?$/ } @Params::Validate::EXPORT_OK);
  # now export all that P::V would have exported, plus *_of
  __PACKAGE__->export_to_level(1, @_);
}

=head1 NAME

Params::Validate::Dependencies - check that the right combination of arguments is passed to a function

=head1 DESCRIPTION

Extends Params::Validate to make it easy to validate
that you have been passed the correct combinations of parameters.

=head1 SYNOPSIS

This example validates that sub 'foo's arguments are of the right types,
and that either we have at least one of alpha, beta and gamma, or
we have both of bar amd baz:

  use Params::Validate::Dependencies qw(:all);

  sub foo {
    validate(@_,
      {
        alpha => { type => ARRAYREF, optional => 1 },
        beta  => { type => ARRAYREF, optional => 1 },
        gamma => { type => ARRAYREF, optional => 1 },
        bar   => { type => SCALAR, optional => 1 },
        baz   => { type => SCALAR, optional => 1 },
      },



( run in 1.453 second using v1.01-cache-2.11-cpan-e1769b4cff6 )