Blame README

Packit 37d9e7
NAME
Packit 37d9e7
    Params::Util - Simple, compact and correct param-checking functions
Packit 37d9e7
Packit 37d9e7
SYNOPSIS
Packit 37d9e7
      # Import some functions
Packit 37d9e7
      use Params::Util qw{_SCALAR _HASH _INSTANCE};
Packit 37d9e7
      
Packit 37d9e7
  # If you are lazy, or need a lot of them...
Packit 37d9e7
      use Params::Util ':ALL';
Packit 37d9e7
      
Packit 37d9e7
  sub foo {
Packit 37d9e7
          my $object  = _INSTANCE(shift, 'Foo') or return undef;
Packit 37d9e7
          my $image   = _SCALAR(shift)          or return undef;
Packit 37d9e7
          my $options = _HASH(shift)            or return undef;
Packit 37d9e7
          # etc...
Packit 37d9e7
      }
Packit 37d9e7
Packit 37d9e7
DESCRIPTION
Packit 37d9e7
    "Params::Util" provides a basic set of importable functions that makes
Packit 37d9e7
    checking parameters a hell of a lot easier
Packit 37d9e7
Packit 37d9e7
    While they can be (and are) used in other contexts, the main point
Packit 37d9e7
    behind this module is that the functions both Do What You Mean, and Do
Packit 37d9e7
    The Right Thing, so they are most useful when you are getting params
Packit 37d9e7
    passed into your code from someone and/or somewhere else and you can't
Packit 37d9e7
    really trust the quality.
Packit 37d9e7
Packit 37d9e7
    Thus, "Params::Util" is of most use at the edges of your API, where
Packit 37d9e7
    params and data are coming in from outside your code.
Packit 37d9e7
Packit 37d9e7
    The functions provided by "Params::Util" check in the most strictly
Packit 37d9e7
    correct manner known, are documented as thoroughly as possible so their
Packit 37d9e7
    exact behaviour is clear, and heavily tested so make sure they are not
Packit 37d9e7
    fooled by weird data and Really Bad Things.
Packit 37d9e7
Packit 37d9e7
    To use, simply load the module providing the functions you want to use
Packit 37d9e7
    as arguments (as shown in the SYNOPSIS).
Packit 37d9e7
Packit 37d9e7
    To aid in maintainability, "Params::Util" will never export by default.
Packit 37d9e7
Packit 37d9e7
    You must explicitly name the functions you want to export, or use the
Packit 37d9e7
    ":ALL" param to just have it export everything (although this is not
Packit 37d9e7
    recommended if you have any _FOO functions yourself with which future
Packit 37d9e7
    additions to "Params::Util" may clash)
Packit 37d9e7
Packit 37d9e7
FUNCTIONS
Packit 37d9e7
  _STRING $string
Packit 37d9e7
    The "_STRING" function is intended to be imported into your package, and
Packit 37d9e7
    provides a convenient way to test to see if a value is a normal
Packit 37d9e7
    non-false string of non-zero length.
Packit 37d9e7
Packit 37d9e7
    Note that this will NOT do anything magic to deal with the special '0'
Packit 37d9e7
    false negative case, but will return it.
Packit 37d9e7
Packit 37d9e7
      # '0' not considered valid data
Packit 37d9e7
      my $name = _STRING(shift) or die "Bad name";
Packit 37d9e7
      
Packit 37d9e7
  # '0' is considered valid data
Packit 37d9e7
      my $string = _STRING($_[0]) ? shift : die "Bad string";
Packit 37d9e7
Packit 37d9e7
    Please also note that this function expects a normal string. It does not
Packit 37d9e7
    support overloading or other magic techniques to get a string.
Packit 37d9e7
Packit 37d9e7
    Returns the string as a conveince if it is a valid string, or "undef" if
Packit 37d9e7
    not.
Packit 37d9e7
Packit 37d9e7
  _IDENTIFIER $string
Packit 37d9e7
    The "_IDENTIFIER" function is intended to be imported into your package,
Packit 37d9e7
    and provides a convenient way to test to see if a value is a string that
Packit 37d9e7
    is a valid Perl identifier.
Packit 37d9e7
Packit 37d9e7
    Returns the string as a convenience if it is a valid identifier, or
Packit 37d9e7
    "undef" if not.
Packit 37d9e7
Packit 37d9e7
  _CLASS $string
Packit 37d9e7
    The "_CLASS" function is intended to be imported into your package, and
Packit 37d9e7
    provides a convenient way to test to see if a value is a string that is
Packit 37d9e7
    a valid Perl class.
Packit 37d9e7
Packit 37d9e7
    This function only checks that the format is valid, not that the class
Packit 37d9e7
    is actually loaded. It also assumes "normalised" form, and does not
Packit 37d9e7
    accept class names such as "::Foo" or "D'Oh".
Packit 37d9e7
Packit 37d9e7
    Returns the string as a convenience if it is a valid class name, or
Packit 37d9e7
    "undef" if not.
Packit 37d9e7
Packit 37d9e7
  _CLASSISA $string, $class
Packit 37d9e7
    The "_CLASSISA" function is intended to be imported into your package,
Packit 37d9e7
    and provides a convenient way to test to see if a value is a string that
Packit 37d9e7
    is a particularly class, or a subclass of it.
Packit 37d9e7
Packit 37d9e7
    This function checks that the format is valid and calls the ->isa method
Packit 37d9e7
    on the class name. It does not check that the class is actually loaded.
Packit 37d9e7
Packit 37d9e7
    It also assumes "normalised" form, and does not accept class names such
Packit 37d9e7
    as "::Foo" or "D'Oh".
Packit 37d9e7
Packit 37d9e7
    Returns the string as a convenience if it is a valid class name, or
Packit 37d9e7
    "undef" if not.
Packit 37d9e7
Packit 37d9e7
  _CLASSDOES $string, $role
Packit 37d9e7
    This routine behaves exactly like "_CLASSISA", but checks with "->DOES"
Packit 37d9e7
    rather than "->isa". This is probably only a good idea to use on Perl
Packit 37d9e7
    5.10 or later, when UNIVERSAL::DOES has been implemented.
Packit 37d9e7
Packit 37d9e7
  _SUBCLASS $string, $class
Packit 37d9e7
    The "_SUBCLASS" function is intended to be imported into your package,
Packit 37d9e7
    and provides a convenient way to test to see if a value is a string that
Packit 37d9e7
    is a subclass of a specified class.
Packit 37d9e7
Packit 37d9e7
    This function checks that the format is valid and calls the ->isa method
Packit 37d9e7
    on the class name. It does not check that the class is actually loaded.
Packit 37d9e7
Packit 37d9e7
    It also assumes "normalised" form, and does not accept class names such
Packit 37d9e7
    as "::Foo" or "D'Oh".
Packit 37d9e7
Packit 37d9e7
    Returns the string as a convenience if it is a valid class name, or
Packit 37d9e7
    "undef" if not.
Packit 37d9e7
Packit 37d9e7
  _NUMBER $scalar
Packit 37d9e7
    The "_NUMBER" function is intended to be imported into your package, and
Packit 37d9e7
    provides a convenient way to test to see if a value is a number. That
Packit 37d9e7
    is, it is defined and perl thinks it's a number.
Packit 37d9e7
Packit 37d9e7
    This function is basically a Params::Util-style wrapper around the
Packit 37d9e7
    Scalar::Util "looks_like_number" function.
Packit 37d9e7
Packit 37d9e7
    Returns the value as a convience, or "undef" if the value is not a
Packit 37d9e7
    number.
Packit 37d9e7
Packit 37d9e7
  _POSINT $integer
Packit 37d9e7
    The "_POSINT" function is intended to be imported into your package, and
Packit 37d9e7
    provides a convenient way to test to see if a value is a positive
Packit 37d9e7
    integer (of any length).
Packit 37d9e7
Packit 37d9e7
    Returns the value as a convience, or "undef" if the value is not a
Packit 37d9e7
    positive integer.
Packit 37d9e7
Packit 37d9e7
    The name itself is derived from the XML schema constraint of the same
Packit 37d9e7
    name.
Packit 37d9e7
Packit 37d9e7
  _NONNEGINT $integer
Packit 37d9e7
    The "_NONNEGINT" function is intended to be imported into your package,
Packit 37d9e7
    and provides a convenient way to test to see if a value is a
Packit 37d9e7
    non-negative integer (of any length). That is, a positive integer, or
Packit 37d9e7
    zero.
Packit 37d9e7
Packit 37d9e7
    Returns the value as a convience, or "undef" if the value is not a
Packit 37d9e7
    non-negative integer.
Packit 37d9e7
Packit 37d9e7
    As with other tests that may return false values, care should be taken
Packit 37d9e7
    to test via "defined" in boolean validy contexts.
Packit 37d9e7
Packit 37d9e7
      unless ( defined _NONNEGINT($value) ) {
Packit 37d9e7
         die "Invalid value";
Packit 37d9e7
      }
Packit 37d9e7
Packit 37d9e7
    The name itself is derived from the XML schema constraint of the same
Packit 37d9e7
    name.
Packit 37d9e7
Packit 37d9e7
  _SCALAR \$scalar
Packit 37d9e7
    The "_SCALAR" function is intended to be imported into your package, and
Packit 37d9e7
    provides a convenient way to test for a raw and unblessed "SCALAR"
Packit 37d9e7
    reference, with content of non-zero length.
Packit 37d9e7
Packit 37d9e7
    For a version that allows zero length "SCALAR" references, see the
Packit 37d9e7
    "_SCALAR0" function.
Packit 37d9e7
Packit 37d9e7
    Returns the "SCALAR" reference itself as a convenience, or "undef" if
Packit 37d9e7
    the value provided is not a "SCALAR" reference.
Packit 37d9e7
Packit 37d9e7
  _SCALAR0 \$scalar
Packit 37d9e7
    The "_SCALAR0" function is intended to be imported into your package,
Packit 37d9e7
    and provides a convenient way to test for a raw and unblessed "SCALAR0"
Packit 37d9e7
    reference, allowing content of zero-length.
Packit 37d9e7
Packit 37d9e7
    For a simpler "give me some content" version that requires non-zero
Packit 37d9e7
    length, "_SCALAR" function.
Packit 37d9e7
Packit 37d9e7
    Returns the "SCALAR" reference itself as a convenience, or "undef" if
Packit 37d9e7
    the value provided is not a "SCALAR" reference.
Packit 37d9e7
Packit 37d9e7
  _ARRAY $value
Packit 37d9e7
    The "_ARRAY" function is intended to be imported into your package, and
Packit 37d9e7
    provides a convenient way to test for a raw and unblessed "ARRAY"
Packit 37d9e7
    reference containing at least one element of any kind.
Packit 37d9e7
Packit 37d9e7
    For a more basic form that allows zero length ARRAY references, see the
Packit 37d9e7
    "_ARRAY0" function.
Packit 37d9e7
Packit 37d9e7
    Returns the "ARRAY" reference itself as a convenience, or "undef" if the
Packit 37d9e7
    value provided is not an "ARRAY" reference.
Packit 37d9e7
Packit 37d9e7
  _ARRAY0 $value
Packit 37d9e7
    The "_ARRAY0" function is intended to be imported into your package, and
Packit 37d9e7
    provides a convenient way to test for a raw and unblessed "ARRAY"
Packit 37d9e7
    reference, allowing "ARRAY" references that contain no elements.
Packit 37d9e7
Packit 37d9e7
    For a more basic "An array of something" form that also requires at
Packit 37d9e7
    least one element, see the "_ARRAY" function.
Packit 37d9e7
Packit 37d9e7
    Returns the "ARRAY" reference itself as a convenience, or "undef" if the
Packit 37d9e7
    value provided is not an "ARRAY" reference.
Packit 37d9e7
Packit 37d9e7
  _ARRAYLIKE $value
Packit 37d9e7
    The "_ARRAYLIKE" function tests whether a given scalar value can respond
Packit 37d9e7
    to array dereferencing. If it can, the value is returned. If it cannot,
Packit 37d9e7
    "_ARRAYLIKE" returns "undef".
Packit 37d9e7
Packit 37d9e7
  _HASH $value
Packit 37d9e7
    The "_HASH" function is intended to be imported into your package, and
Packit 37d9e7
    provides a convenient way to test for a raw and unblessed "HASH"
Packit 37d9e7
    reference with at least one entry.
Packit 37d9e7
Packit 37d9e7
    For a version of this function that allows the "HASH" to be empty, see
Packit 37d9e7
    the "_HASH0" function.
Packit 37d9e7
Packit 37d9e7
    Returns the "HASH" reference itself as a convenience, or "undef" if the
Packit 37d9e7
    value provided is not an "HASH" reference.
Packit 37d9e7
Packit 37d9e7
  _HASH0 $value
Packit 37d9e7
    The "_HASH0" function is intended to be imported into your package, and
Packit 37d9e7
    provides a convenient way to test for a raw and unblessed "HASH"
Packit 37d9e7
    reference, regardless of the "HASH" content.
Packit 37d9e7
Packit 37d9e7
    For a simpler "A hash of something" version that requires at least one
Packit 37d9e7
    element, see the "_HASH" function.
Packit 37d9e7
Packit 37d9e7
    Returns the "HASH" reference itself as a convenience, or "undef" if the
Packit 37d9e7
    value provided is not an "HASH" reference.
Packit 37d9e7
Packit 37d9e7
  _HASHLIKE $value
Packit 37d9e7
    The "_HASHLIKE" function tests whether a given scalar value can respond
Packit 37d9e7
    to hash dereferencing. If it can, the value is returned. If it cannot,
Packit 37d9e7
    "_HASHLIKE" returns "undef".
Packit 37d9e7
Packit 37d9e7
  _CODE $value
Packit 37d9e7
    The "_CODE" function is intended to be imported into your package, and
Packit 37d9e7
    provides a convenient way to test for a raw and unblessed "CODE"
Packit 37d9e7
    reference.
Packit 37d9e7
Packit 37d9e7
    Returns the "CODE" reference itself as a convenience, or "undef" if the
Packit 37d9e7
    value provided is not an "CODE" reference.
Packit 37d9e7
Packit 37d9e7
  _CODELIKE $value
Packit 37d9e7
    The "_CODELIKE" is the more generic version of "_CODE". Unlike "_CODE",
Packit 37d9e7
    which checks for an explicit "CODE" reference, the "_CODELIKE" function
Packit 37d9e7
    also includes things that act like them, such as blessed objects that
Packit 37d9e7
    overload '&{}'.
Packit 37d9e7
Packit 37d9e7
    Please note that in the case of objects overloaded with '&{}', you will
Packit 37d9e7
    almost always end up also testing it in 'bool' context at some stage.
Packit 37d9e7
Packit 37d9e7
    For example:
Packit 37d9e7
Packit 37d9e7
      sub foo {
Packit 37d9e7
          my $code1 = _CODELIKE(shift) or die "No code param provided";
Packit 37d9e7
          my $code2 = _CODELIKE(shift);
Packit 37d9e7
          if ( $code2 ) {
Packit 37d9e7
               print "Got optional second code param";
Packit 37d9e7
          }
Packit 37d9e7
      }
Packit 37d9e7
Packit 37d9e7
    As such, you will most likely always want to make sure your class has at
Packit 37d9e7
    least the following to allow it to evaluate to true in boolean context.
Packit 37d9e7
Packit 37d9e7
      # Always evaluate to true in boolean context
Packit 37d9e7
      use overload 'bool' => sub () { 1 };
Packit 37d9e7
Packit 37d9e7
    Returns the callable value as a convenience, or "undef" if the value
Packit 37d9e7
    provided is not callable.
Packit 37d9e7
Packit 37d9e7
    Note - This function was formerly known as _CALLABLE but has been
Packit 37d9e7
    renamed for greater symmetry with the other _XXXXLIKE functions.
Packit 37d9e7
Packit 37d9e7
    The use of _CALLABLE has been deprecated. It will continue to work, but
Packit 37d9e7
    with a warning, until end-2006, then will be removed.
Packit 37d9e7
Packit 37d9e7
    I apologise for any inconvenience caused.
Packit 37d9e7
Packit 37d9e7
  _INVOCANT $value
Packit 37d9e7
    This routine tests whether the given value is a valid method invocant.
Packit 37d9e7
    This can be either an instance of an object, or a class name.
Packit 37d9e7
Packit 37d9e7
    If so, the value itself is returned. Otherwise, "_INVOCANT" returns
Packit 37d9e7
    "undef".
Packit 37d9e7
Packit 37d9e7
  _INSTANCE $object, $class
Packit 37d9e7
    The "_INSTANCE" function is intended to be imported into your package,
Packit 37d9e7
    and provides a convenient way to test for an object of a particular
Packit 37d9e7
    class in a strictly correct manner.
Packit 37d9e7
Packit 37d9e7
    Returns the object itself as a convenience, or "undef" if the value
Packit 37d9e7
    provided is not an object of that type.
Packit 37d9e7
Packit 37d9e7
  _INSTANCEDOES $object, $role
Packit 37d9e7
    This routine behaves exactly like "_INSTANCE", but checks with "->DOES"
Packit 37d9e7
    rather than "->isa". This is probably only a good idea to use on Perl
Packit 37d9e7
    5.10 or later, when UNIVERSAL::DOES has been implemented.
Packit 37d9e7
Packit 37d9e7
  _REGEX $value
Packit 37d9e7
    The "_REGEX" function is intended to be imported into your package, and
Packit 37d9e7
    provides a convenient way to test for a regular expression.
Packit 37d9e7
Packit 37d9e7
    Returns the value itself as a convenience, or "undef" if the value
Packit 37d9e7
    provided is not a regular expression.
Packit 37d9e7
Packit 37d9e7
  _SET \@array, $class
Packit 37d9e7
    The "_SET" function is intended to be imported into your package, and
Packit 37d9e7
    provides a convenient way to test for set of at least one object of a
Packit 37d9e7
    particular class in a strictly correct manner.
Packit 37d9e7
Packit 37d9e7
    The set is provided as a reference to an "ARRAY" of objects of the class
Packit 37d9e7
    provided.
Packit 37d9e7
Packit 37d9e7
    For an alternative function that allows zero-length sets, see the
Packit 37d9e7
    "_SET0" function.
Packit 37d9e7
Packit 37d9e7
    Returns the "ARRAY" reference itself as a convenience, or "undef" if the
Packit 37d9e7
    value provided is not a set of that class.
Packit 37d9e7
Packit 37d9e7
  _SET0 \@array, $class
Packit 37d9e7
    The "_SET0" function is intended to be imported into your package, and
Packit 37d9e7
    provides a convenient way to test for a set of objects of a particular
Packit 37d9e7
    class in a strictly correct manner, allowing for zero objects.
Packit 37d9e7
Packit 37d9e7
    The set is provided as a reference to an "ARRAY" of objects of the class
Packit 37d9e7
    provided.
Packit 37d9e7
Packit 37d9e7
    For an alternative function that requires at least one object, see the
Packit 37d9e7
    "_SET" function.
Packit 37d9e7
Packit 37d9e7
    Returns the "ARRAY" reference itself as a convenience, or "undef" if the
Packit 37d9e7
    value provided is not a set of that class.
Packit 37d9e7
Packit 37d9e7
  _HANDLE
Packit 37d9e7
    The "_HANDLE" function is intended to be imported into your package, and
Packit 37d9e7
    provides a convenient way to test whether or not a single scalar value
Packit 37d9e7
    is a file handle.
Packit 37d9e7
Packit 37d9e7
    Unfortunately, in Perl the definition of a file handle can be a little
Packit 37d9e7
    bit fuzzy, so this function is likely to be somewhat imperfect (at first
Packit 37d9e7
    anyway).
Packit 37d9e7
Packit 37d9e7
    That said, it is implement as well or better than the other file handle
Packit 37d9e7
    detectors in existance (and we stole from the best of them).
Packit 37d9e7
Packit 37d9e7
  _DRIVER $string
Packit 37d9e7
      sub foo {
Packit 37d9e7
        my $class = _DRIVER(shift, 'My::Driver::Base') or die "Bad driver";
Packit 37d9e7
        ...
Packit 37d9e7
      }
Packit 37d9e7
Packit 37d9e7
    The "_DRIVER" function is intended to be imported into your package, and
Packit 37d9e7
    provides a convenient way to load and validate a driver class.
Packit 37d9e7
Packit 37d9e7
    The most common pattern when taking a driver class as a parameter is to
Packit 37d9e7
    check that the name is a class (i.e. check against _CLASS) and then to
Packit 37d9e7
    load the class (if it exists) and then ensure that the class returns
Packit 37d9e7
    true for the isa method on some base driver name.
Packit 37d9e7
Packit 37d9e7
    Return the value as a convenience, or "undef" if the value is not a
Packit 37d9e7
    class name, the module does not exist, the module does not load, or the
Packit 37d9e7
    class fails the isa test.
Packit 37d9e7
Packit 37d9e7
TO DO
Packit 37d9e7
    - Add _CAN to help resolve the UNIVERSAL::can debacle
Packit 37d9e7
Packit 37d9e7
    - Would be even nicer if someone would demonstrate how the hell to build
Packit 37d9e7
    a Module::Install dist of the ::Util dual Perl/XS type. :/
Packit 37d9e7
Packit 37d9e7
    - Implement an assertion-like version of this module, that dies on
Packit 37d9e7
    error.
Packit 37d9e7
Packit 37d9e7
    - Implement a Test:: version of this module, for use in testing
Packit 37d9e7
Packit 37d9e7
SUPPORT
Packit 37d9e7
    Bugs should be reported via the CPAN bug tracker at
Packit 37d9e7
Packit 37d9e7
    <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Params-Util>
Packit 37d9e7
Packit 37d9e7
    For other issues, contact the author.
Packit 37d9e7
Packit 37d9e7
AUTHOR
Packit 37d9e7
    Adam Kennedy <adamk@cpan.org>
Packit 37d9e7
Packit 37d9e7
SEE ALSO
Packit 37d9e7
    Params::Validate
Packit 37d9e7
Packit 37d9e7
COPYRIGHT
Packit 37d9e7
    Copyright 2005 - 2012 Adam Kennedy.
Packit 37d9e7
Packit 37d9e7
    This program is free software; you can redistribute it and/or modify it
Packit 37d9e7
    under the same terms as Perl itself.
Packit 37d9e7
Packit 37d9e7
    The full text of the license can be found in the LICENSE file included
Packit 37d9e7
    with this module.
Packit 37d9e7