Params::Util(3)
π¦ Params::Util(3) β Simple, compact and correct param-checking functions π¦
okay bestie, this is the Params::Util(3) page from the User Contributed Perl Documentation, and it's literally about making param-checking not a whole stressful situation. π
πΈ NAME πΈ
Params::Util β Simple, compact and correct param-checking functions
β¨ SYNOPSIS β¨
# Import some functions
use Params::Util qw{_SCALAR _HASH _INSTANCE};
# If you are lazy, or need a lot of them...
use Params::Util ':ALL';
sub foo {
my $object = _INSTANCE(shift, 'Foo') or return undef;
my $image = _SCALAR(shift) or return undef;
my $options = _HASH(shift) or return undef;
# etc...
}
π DESCRIPTION π
so Params::Util gives you a basic lil set of importable functions that makes checking parameters a hell of a lot easier. periodt. π
they can be used in other contexts too (and they are), but the main character energy here is that these functions both Do What You Mean and Do The Right Thing. that makes them most useful when you're getting params passed into your code from someone and/or somewhere else and you can't really trust the quality. π
so basically, Params::Util is most useful at the edges of your API, where params and data are coming in from outside your code. π¦
the functions provided by Params::Util check in the most strictly correct manner known, are documented as thoroughly as possible so their exact behaviour is clear, and heavily tested to make sure they are not fooled by weird data and Really Bad Things. ππ₯
to use it, just load the module providing the functions you want to use as arguments (as shown in the SYNOPSIS). π«Ά
to aid in maintainability, Params::Util will never export by default. no surprise exports, no chaos, no cap.
you must explicitly name the functions you want to export, or use the :ALL param to just have it export everything β although this is not recommended if you have any _FOO functions yourself with which future additions to Params::Util may clash. iykyk. π
π FUNCTIONS π
π _STRING $string π
the _STRING function is meant to be imported into your package, and it gives you a convenient way to test whether a value is a normal non-false string of non-zero length. β¨
heads up, bestie: this will NOT do anything magic to deal with the special '0' false negative case, but will return it. π«Ά
# '0' not considered valid data
my $name = _STRING(shift) or die "Bad name";
# '0' is considered valid data
my $string = _STRING($_[0]) ? shift : die "Bad string";
also please note that this function expects a normal string. it does not support overloading or other magic techniques to get a string. nope, not today. π ββοΈ
returns the string as a convenience if it is a valid string, or undef if not. π
π¦ _IDENTIFIER $string π¦
the _IDENTIFIER function is meant to be imported into your package, and it gives you a convenient way to test whether a value is a string that is a valid Perl identifier. π
returns the string as a convenience if it is a valid identifier, or undef if not.
π _CLASS $string π
the _CLASS function is meant to be imported into your package, and it gives you a convenient way to test whether a value is a string that is a valid Perl class. πΈ
this function only checks that the format is valid β not that the class is actually loaded. it also assumes "normalized" form, and does not accept class names such as ::Foo or D'Oh. π
returns the string as a convenience if it is a valid class name, or undef if not.
β¨ _CLASSISA $string, $class β¨
the _CLASSISA function is meant to be imported into your package, and it gives you a convenient way to test whether a value is a string that is a particular class, or a subclass of it. π
this function checks that the format is valid and calls the ->isa method on the class name. it does not check that the class is actually loaded.
it also assumes "normalized" form, and does not accept class names such as ::Foo or D'Oh. π
returns the string as a convenience if it is a valid class name, or undef if not.
π _CLASSDOES $string, $role π
this routine behaves exactly like _CLASSISA, but checks with ->DOES rather than ->isa. π«Ά
this is probably only a good idea to use on Perl 5.10 or later, when UNIVERSAL::DOES has been implemented. β
π _SUBCLASS $string, $class π
the _SUBCLASS function is meant to be imported into your package, and it gives you a convenient way to test whether a value is a string that is a subclass of a specified class. π
this function checks that the format is valid and calls the ->isa method on the class name. it does not check that the class is actually loaded.
it also assumes "normalized" form, and does not accept class names such as ::Foo or D'Oh. π
returns the string as a convenience if it is a valid class name, or undef if not.
π₯ _NUMBER $scalar π₯
the _NUMBER function is meant to be imported into your package, and it gives you a convenient way to test whether a value is a number. that is, it is defined and perl thinks it's a number. π
this function is basically a Params::Util-style wrapper around the Scalar::Util looks_like_number function. π
returns the value as a convenience, or undef if the value is not a number.
π _POSINT $integer π
the _POSINT function is meant to be imported into your package, and it gives you a convenient way to test whether a value is a positive integer (of any length). β¨
returns the value as a convenience, or undef if the value is not a positive integer.
the name itself is derived from the XML schema constraint of the same name. π«Ά
π§ _NONNEGINT $integer π§
the _NONNEGINT function is meant to be imported into your package, and it gives you a convenient way to test whether a value is a non-negative integer (of any length). that is, a positive integer, or zero. πΈ
returns the value as a convenience, or undef if the value is not a non-negative integer.
as with other tests that may return false values, care should be taken to test via defined in boolean validly contexts. π
unless ( defined _NONNEGINT($value) ) {
die "Invalid value";
}
the name itself is derived from the XML schema constraint of the same name. π
π _SCALAR \$scalar π
the _SCALAR function is meant to be imported into your package, and it gives you a convenient way to test for a raw and unblessed SCALAR reference, with content of non-zero length. β¨
for a version that allows zero length SCALAR references, see the _SCALAR0 function. π«Ά
returns the SCALAR reference itself as a convenience, or undef if the value provided is not a SCALAR reference.
π¦ _SCALAR0 \$scalar π¦
the _SCALAR0 function is meant to be imported into your package, and it gives you a convenient way to test for a raw and unblessed "SCALAR0" reference, allowing content of zero-length. π
for a simpler "give me some content" version that requires non-zero length, _SCALAR function. π
returns the SCALAR reference itself as a convenience, or undef if the value provided is not a SCALAR reference.
π _ARRAY $value π
the _ARRAY function is meant to be imported into your package, and it gives you a convenient way to test for a raw and unblessed ARRAY reference containing at least one element of any kind. β¨
for a more basic form that allows zero length ARRAY references, see the _ARRAY0 function. π«Ά
returns the ARRAY reference itself as a convenience, or undef if the value provided is not an ARRAY reference.
πΈ _ARRAY0 $value πΈ
the _ARRAY0 function is meant to be imported into your package, and it gives you a convenient way to test for a raw and unblessed ARRAY reference, allowing ARRAY references that contain no elements. π
for a more basic "An array of something" form that also requires at least one element, see the _ARRAY function. π
returns the ARRAY reference itself as a convenience, or undef if the value provided is not an ARRAY reference.
β _ARRAYLIKE $value β
the _ARRAYLIKE function tests whether a given scalar value can respond to array dereferencing. π₯
if it can, the value is returned. if it cannot, _ARRAYLIKE returns undef. π
π _HASH $value π
the _HASH function is meant to be imported into your package, and it gives you a convenient way to test for a raw and unblessed HASH reference with at least one entry. β¨
for a version of this function that allows the HASH to be empty, see the _HASH0 function. π«Ά
returns the HASH reference itself as a convenience, or undef if the value provided is not an HASH reference.
π _HASH0 $value π
the _HASH0 function is meant to be imported into your package, and it gives you a convenient way to test for a raw and unblessed HASH reference, regardless of the HASH content. πΈ
for a simpler "A hash of something" version that requires at least one element, see the _HASH function. π
returns the HASH reference itself as a convenience, or undef if the value provided is not an HASH reference.
π¦ _HASHLIKE $value π¦
the _HASHLIKE function tests whether a given scalar value can respond to hash dereferencing. π
if it can, the value is returned. if it cannot, _HASHLIKE returns undef. β¨
π _CODE $value π
the _CODE function is meant to be imported into your package, and it gives you a convenient way to test for a raw and unblessed CODE reference. π₯
returns the CODE reference itself as a convenience, or undef if the value provided is not an CODE reference. π
π
_CODELIKE $value π
the _CODELIKE is the more generic version of _CODE. unlike _CODE, which checks for an explicit CODE reference, the _CODELIKE function also includes things that act like them, such as blessed objects that overload '&{}'. π
please note that in the case of objects overloaded with '&{}', you will almost always end up also testing it in 'bool' context at some stage. π«Ά
for example:
sub foo {
my $code1 = _CODELIKE(shift) or die "No code param provided";
my $code2 = _CODELIKE(shift);
if ( $code2 ) {
print "Got optional second code param";
}
}
as such, you will most likely always want to make sure your class has at least the following to allow it to evaluate to true in boolean context. β
# Always evaluate to true in boolean context
use overload 'bool' => sub () { 1 };
returns the callable value as a convenience, or undef if the value provided is not callable. π
note β this function was formerly known as_CALLABLEbut has been renamed for greater symmetry with the other_XXXXLIKEfunctions. π¦ the use of_CALLABLEhas been deprecated. it will continue to work, but with a warning, until end-2006, then will be removed. π i apologize for any inconvenience caused. π
π _INVOCANT $value π
this routine tests whether the given value is a valid method invocant. this can be either an instance of an object, or a class name. π
if so, the value itself is returned. otherwise, _INVOCANT returns undef. β¨
_INSTANCE $object, $class π
Okay so _INSTANCE is meant to be imported into your package, and it's the handy way to check βis this an object of that class?β in a strictly correct way. It returns the object itself as a convenience (so you can chain stuff), or undef if the value you passed isn't an object of that type. Periodt. β¨
_INSTANCEDOES $object, $role π¦
This one behaves exactly like _INSTANCE, but it checks with ->DOES instead of ->isa. Lowkey probably only a good idea on Perl 5.10 or later, when UNIVERSAL::DOES has been implemented. iykyk π
_REGEX $value π
_REGEX is meant to be imported into your package, and it's the convenient way to test for a regular expression. Returns the value itself as a convenience, or undef if the value you gave it isn't a regular expression. Slay. πΈ
_SET \@array, $class π
_SET is meant to be imported into your package, and it's the convenient way to test for a set of at least one object of a particular class, in a strictly correct manner. The set is provided as a reference to an ARRAY of objects of the class you provided. If you want an alternative function that allows zero-length sets, see the _SET0 function. It returns the ARRAY reference itself as a convenience, or undef if the value provided isn't a set of that class. Obsessed. π
_SET0 \@array, $class π§
_SET0 is meant to be imported into your package, and it's the convenient way to test for a set of objects of a particular class, in a strictly correct manner, allowing for zero objects. The set is provided as a reference to an ARRAY of objects of the class you provided. If you want an alternative function that requires at least one object, see the _SET function. It returns the ARRAY reference itself as a convenience, or undef if the value provided isn't a set of that class. No cap. π«Ά
_HANDLE π
_HANDLE is meant to be imported into your package, and it's the convenient way to test whether a single scalar value is a file handle. Unfortunately, in Perl the definition of a file handle can be a little bit fuzzy, so this function is likely to be somewhat imperfect (at first anyway). That said, it's implemented as well asβor better thanβthe other file handle detectors in existence (and yeah, we stole from the best of them). It's giving βtrust me bestie.β π
_DRIVER $string π₯
sub foo {
my $class = _DRIVER(shift, 'My::Driver::Base') or die "Bad driver";
...
}
_DRIVER is meant to be imported into your package, and it's the convenient way to load and validate a driver class. The most common pattern when you're taking a driver class as a parameter is: check that the name is a class (i.e. check against _CLASS), then load the class (if it exists), and then make sure the class returns true for the isa method on some base driver name. It returns the value as a convenience, or undef if the value isn't a class name, the module doesn't exist, the module doesn't load, or the class fails the isa test. Main character energy. π
π TO DO π
- Add
_CANto help resolve theUNIVERSAL::candebacle. (we know, we know) - Implement an assertion-like version of this module, that dies on error.
- Implement a
Test::version of this module, for use in testing.
π SUPPORT π
Bugs should be reported via the CPAN bug tracker at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Params-Util>. Do it for the plot. π«Ά
π AUTHOR π
Adam Kennedy <adamk AT cpan.org>
Jens Rehsack <rehsack AT cpan.org>
π¦ SEE ALSO π¦
Params::Validate
π COPYRIGHT π
Copyright 2005 - 2012 Adam Kennedy.
Copyright 2020 - 2020 Jens Rehsack.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.
That's a wrap on Params::Util(3) bestie! This page is from Perl v5.42.0, dated 2020-11-04 β go forth and validate those params. You ate. π
β¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Params-Util 1.102-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Params-Util 1.102-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Params-Util 1.102-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Params-Util 1.102-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms