7 Params::Util - Simple, compact and correct param-checking functions
11 # Import some functions
12 use Params::Util qw{_SCALAR _HASH _INSTANCE};
14 # If you are lazy, or need a lot of them...
15 use Params::Util ':ALL';
18 my $object = _INSTANCE(shift, 'Foo') or return undef;
19 my $image = _SCALAR(shift) or return undef;
20 my $options = _HASH(shift) or return undef;
26 C<Params::Util> provides a basic set of importable functions that makes
27 checking parameters a hell of a lot easier
29 While they can be (and are) used in other contexts, the main point
30 behind this module is that the functions B<both> Do What You Mean,
31 and Do The Right Thing, so they are most useful when you are getting
32 params passed into your code from someone and/or somewhere else
33 and you can't really trust the quality.
35 Thus, C<Params::Util> is of most use at the edges of your API, where
36 params and data are coming in from outside your code.
38 The functions provided by C<Params::Util> check in the most strictly
39 correct manner known, are documented as thoroughly as possible so their
40 exact behaviour is clear, and heavily tested so make sure they are not
41 fooled by weird data and Really Bad Things.
43 To use, simply load the module providing the functions you want to use
44 as arguments (as shown in the SYNOPSIS).
46 To aid in maintainability, C<Params::Util> will B<never> export by
49 You must explicitly name the functions you want to export, or use the
50 C<:ALL> param to just have it export everything (although this is not
51 recommended if you have any _FOO functions yourself with which future
52 additions to C<Params::Util> may clash)
65 use vars qw{$VERSION @ISA @EXPORT_OK %EXPORT_TAGS};
74 _CLASS _CLASSISA _SUBCLASS _DRIVER
75 _NUMBER _POSINT _NONNEGINT
77 _ARRAY _ARRAY0 _ARRAYLIKE
78 _HASH _HASH0 _HASHLIKE
80 _INVOCANT _REGEX _INSTANCE
84 %EXPORT_TAGS = ( ALL => \@EXPORT_OK );
87 local $ENV{PERL_DL_NONLAZY} = 0 if $ENV{PERL_DL_NONLAZY};
88 bootstrap Params::Util $VERSION;
90 } unless $ENV{PERL_PARAMS_UTIL_PP};
96 #####################################################################
97 # Param Checking Functions
101 =head2 _STRING $string
103 The C<_STRING> function is intended to be imported into your
104 package, and provides a convenient way to test to see if a value is
105 a normal non-false string of non-zero length.
107 Note that this will NOT do anything magic to deal with the special
108 C<'0'> false negative case, but will return it.
110 # '0' not considered valid data
111 my $name = _STRING(shift) or die "Bad name";
113 # '0' is considered valid data
114 my $string = _STRING($_[0]) ? shift : die "Bad string";
116 Please also note that this function expects a normal string. It does
117 not support overloading or other magic techniques to get a string.
119 Returns the string as a conveince if it is a valid string, or
124 eval <<'END_PERL' unless defined &_STRING;
126 (defined $_[0] and ! ref $_[0] and length($_[0])) ? $_[0] : undef;
132 =head2 _IDENTIFIER $string
134 The C<_IDENTIFIER> function is intended to be imported into your
135 package, and provides a convenient way to test to see if a value is
136 a string that is a valid Perl identifier.
138 Returns the string as a convenience if it is a valid identifier, or
143 eval <<'END_PERL' unless defined &_IDENTIFIER;
144 sub _IDENTIFIER ($) {
145 (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^[^\W\d]\w*\z/s) ? $_[0] : undef;
151 =head2 _CLASS $string
153 The C<_CLASS> function is intended to be imported into your
154 package, and provides a convenient way to test to see if a value is
155 a string that is a valid Perl class.
157 This function only checks that the format is valid, not that the
158 class is actually loaded. It also assumes "normalised" form, and does
159 not accept class names such as C<::Foo> or C<D'Oh>.
161 Returns the string as a convenience if it is a valid class name, or
166 eval <<'END_PERL' unless defined &_CLASS;
168 (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^[^\W\d]\w*(?:::\w+)*\z/s) ? $_[0] : undef;
174 =head2 _CLASSISA $string, $class
176 The C<_CLASSISA> function is intended to be imported into your
177 package, and provides a convenient way to test to see if a value is
178 a string that is a particularly class, or a subclass of it.
180 This function checks that the format is valid and calls the -E<gt>isa
181 method on the class name. It does not check that the class is actually
184 It also assumes "normalised" form, and does
185 not accept class names such as C<::Foo> or C<D'Oh>.
187 Returns the string as a convenience if it is a valid class name, or
192 eval <<'END_PERL' unless defined &_CLASSISA;
194 (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^[^\W\d]\w*(?:::\w+)*\z/s and $_[0]->isa($_[1])) ? $_[0] : undef;
200 =head2 _SUBCLASS $string, $class
202 The C<_SUBCLASS> function is intended to be imported into your
203 package, and provides a convenient way to test to see if a value is
204 a string that is a subclass of a specified class.
206 This function checks that the format is valid and calls the -E<gt>isa
207 method on the class name. It does not check that the class is actually
210 It also assumes "normalised" form, and does
211 not accept class names such as C<::Foo> or C<D'Oh>.
213 Returns the string as a convenience if it is a valid class name, or
218 eval <<'END_PERL' unless defined &_SUBCLASS;
220 (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^[^\W\d]\w*(?:::\w+)*\z/s and $_[0] ne $_[1] and $_[0]->isa($_[1])) ? $_[0] : undef;
226 =head2 _NUMBER $scalar
228 The C<_NUMBER> function is intended to be imported into your
229 package, and provides a convenient way to test to see if a value is
230 a number. That is, it is defined and perl thinks it's a number.
232 This function is basically a Params::Util-style wrapper around the
233 L<Scalar::Util> C<looks_like_number> function.
235 Returns the value as a convience, or C<undef> if the value is not a
240 eval <<'END_PERL' unless defined &_NUMBER;
242 ( defined $_[0] and ! ref $_[0] and Scalar::Util::looks_like_number($_[0]) )
250 =head2 _POSINT $integer
252 The C<_POSINT> function is intended to be imported into your
253 package, and provides a convenient way to test to see if a value is
254 a positive integer (of any length).
256 Returns the value as a convience, or C<undef> if the value is not a
259 The name itself is derived from the XML schema constraint of the same
264 eval <<'END_PERL' unless defined &_POSINT;
266 (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^[1-9]\d*$/) ? $_[0] : undef;
272 =head2 _NONNEGINT $integer
274 The C<_NONNEGINT> function is intended to be imported into your
275 package, and provides a convenient way to test to see if a value is
276 a non-negative integer (of any length). That is, a positive integer,
279 Returns the value as a convience, or C<undef> if the value is not a
280 non-negative integer.
282 As with other tests that may return false values, care should be taken
283 to test via "defined" in boolean validy contexts.
285 unless ( defined _NONNEGINT($value) ) {
289 The name itself is derived from the XML schema constraint of the same
294 eval <<'END_PERL' unless defined &_NONNEGINT;
296 (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^(?:0|[1-9]\d*)$/) ? $_[0] : undef;
302 =head2 _SCALAR \$scalar
304 The C<_SCALAR> function is intended to be imported into your package,
305 and provides a convenient way to test for a raw and unblessed
306 C<SCALAR> reference, with content of non-zero length.
308 For a version that allows zero length C<SCALAR> references, see
309 the C<_SCALAR0> function.
311 Returns the C<SCALAR> reference itself as a convenience, or C<undef>
312 if the value provided is not a C<SCALAR> reference.
316 eval <<'END_PERL' unless defined &_SCALAR;
318 (ref $_[0] eq 'SCALAR' and defined ${$_[0]} and ${$_[0]} ne '') ? $_[0] : undef;
324 =head2 _SCALAR0 \$scalar
326 The C<_SCALAR0> function is intended to be imported into your package,
327 and provides a convenient way to test for a raw and unblessed
328 C<SCALAR0> reference, allowing content of zero-length.
330 For a simpler "give me some content" version that requires non-zero
331 length, C<_SCALAR> function.
333 Returns the C<SCALAR> reference itself as a convenience, or C<undef>
334 if the value provided is not a C<SCALAR> reference.
338 eval <<'END_PERL' unless defined &_SCALAR0;
340 ref $_[0] eq 'SCALAR' ? $_[0] : undef;
348 The C<_ARRAY> function is intended to be imported into your package,
349 and provides a convenient way to test for a raw and unblessed
350 C<ARRAY> reference containing B<at least> one element of any kind.
352 For a more basic form that allows zero length ARRAY references, see
353 the C<_ARRAY0> function.
355 Returns the C<ARRAY> reference itself as a convenience, or C<undef>
356 if the value provided is not an C<ARRAY> reference.
360 eval <<'END_PERL' unless defined &_ARRAY;
362 (ref $_[0] eq 'ARRAY' and @{$_[0]}) ? $_[0] : undef;
368 =head2 _ARRAY0 $value
370 The C<_ARRAY0> function is intended to be imported into your package,
371 and provides a convenient way to test for a raw and unblessed
372 C<ARRAY> reference, allowing C<ARRAY> references that contain no
375 For a more basic "An array of something" form that also requires at
376 least one element, see the C<_ARRAY> function.
378 Returns the C<ARRAY> reference itself as a convenience, or C<undef>
379 if the value provided is not an C<ARRAY> reference.
383 eval <<'END_PERL' unless defined &_ARRAY0;
385 ref $_[0] eq 'ARRAY' ? $_[0] : undef;
391 =head2 _ARRAYLIKE $value
393 The C<_ARRAYLIKE> function tests whether a given scalar value can respond to
394 array dereferencing. If it can, the value is returned. If it cannot,
395 C<_ARRAYLIKE> returns C<undef>.
399 eval <<'END_PERL' unless defined &_ARRAYLIKE;
401 (defined $_[0] and ref $_[0] and (
402 (Scalar::Util::reftype($_[0]) eq 'ARRAY')
404 overload::Method($_[0], '@{}')
413 The C<_HASH> function is intended to be imported into your package,
414 and provides a convenient way to test for a raw and unblessed
415 C<HASH> reference with at least one entry.
417 For a version of this function that allows the C<HASH> to be empty,
418 see the C<_HASH0> function.
420 Returns the C<HASH> reference itself as a convenience, or C<undef>
421 if the value provided is not an C<HASH> reference.
425 eval <<'END_PERL' unless defined &_HASH;
427 (ref $_[0] eq 'HASH' and scalar %{$_[0]}) ? $_[0] : undef;
435 The C<_HASH0> function is intended to be imported into your package,
436 and provides a convenient way to test for a raw and unblessed
437 C<HASH> reference, regardless of the C<HASH> content.
439 For a simpler "A hash of something" version that requires at least one
440 element, see the C<_HASH> function.
442 Returns the C<HASH> reference itself as a convenience, or C<undef>
443 if the value provided is not an C<HASH> reference.
447 eval <<'END_PERL' unless defined &_HASH0;
449 ref $_[0] eq 'HASH' ? $_[0] : undef;
455 =head2 _HASHLIKE $value
457 The C<_HASHLIKE> function tests whether a given scalar value can respond to
458 hash dereferencing. If it can, the value is returned. If it cannot,
459 C<_HASHLIKE> returns C<undef>.
463 eval <<'END_PERL' unless defined &_HASHLIKE;
465 (defined $_[0] and ref $_[0] and (
466 (Scalar::Util::reftype($_[0]) eq 'HASH')
468 overload::Method($_[0], '%{}')
477 The C<_CODE> function is intended to be imported into your package,
478 and provides a convenient way to test for a raw and unblessed
481 Returns the C<CODE> reference itself as a convenience, or C<undef>
482 if the value provided is not an C<CODE> reference.
486 eval <<'END_PERL' unless defined &_CODE;
488 ref $_[0] eq 'CODE' ? $_[0] : undef;
494 =head2 _CODELIKE $value
496 The C<_CODELIKE> is the more generic version of C<_CODE>. Unlike C<_CODE>,
497 which checks for an explicit C<CODE> reference, the C<_CODELIKE> function
498 also includes things that act like them, such as blessed objects that
501 Please note that in the case of objects overloaded with '&{}', you will
502 almost always end up also testing it in 'bool' context at some stage.
507 my $code1 = _CODELIKE(shift) or die "No code param provided";
508 my $code2 = _CODELIKE(shift);
510 print "Got optional second code param";
514 As such, you will most likely always want to make sure your class has
515 at least the following to allow it to evaluate to true in boolean
518 # Always evaluate to true in boolean context
519 use overload 'bool' => sub () { 1 };
521 Returns the callable value as a convenience, or C<undef> if the
522 value provided is not callable.
524 Note - This function was formerly known as _CALLABLE but has been renamed
525 for greater symmetry with the other _XXXXLIKE functions.
527 The use of _CALLABLE has been deprecated. It will continue to work, but
528 with a warning, until end-2006, then will be removed.
530 I apologise for any inconvenience caused.
534 eval <<'END_PERL' unless defined &_CODELIKE;
537 (Scalar::Util::reftype($_[0])||'') eq 'CODE'
539 Scalar::Util::blessed($_[0]) and overload::Method($_[0],'&{}')
547 =head2 _INVOCANT $value
549 This routine tests whether the given value is a valid method invocant.
550 This can be either an instance of an object, or a class name.
552 If so, the value itself is returned. Otherwise, C<_INVOCANT>
557 eval <<'END_PERL' unless defined &_INVOCANT;
560 (defined Scalar::Util::blessed($_[0])
562 # We used to check for stash definedness, but any class-like name is a
563 # valid invocant for UNIVERSAL methods, so we stopped. -- rjbs, 2006-07-02
564 Params::Util::_CLASS($_[0]))
571 =head2 _INSTANCE $object, $class
573 The C<_INSTANCE> function is intended to be imported into your package,
574 and provides a convenient way to test for an object of a particular class
575 in a strictly correct manner.
577 Returns the object itself as a convenience, or C<undef> if the value
578 provided is not an object of that type.
582 eval <<'END_PERL' unless defined &_INSTANCE;
584 (Scalar::Util::blessed($_[0]) and $_[0]->isa($_[1])) ? $_[0] : undef;
592 The C<_REGEX> function is intended to be imported into your package,
593 and provides a convenient way to test for a regular expression.
595 Returns the value itself as a convenience, or C<undef> if the value
596 provided is not a regular expression.
600 eval <<'END_PERL' unless defined &_REGEX;
602 (defined $_[0] and 'Regexp' eq ref($_[0])) ? $_[0] : undef;
608 =head2 _SET \@array, $class
610 The C<_SET> function is intended to be imported into your package,
611 and provides a convenient way to test for set of at least one object of
612 a particular class in a strictly correct manner.
614 The set is provided as a reference to an C<ARRAY> of objects of the
617 For an alternative function that allows zero-length sets, see the
620 Returns the C<ARRAY> reference itself as a convenience, or C<undef> if
621 the value provided is not a set of that class.
625 eval <<'END_PERL' unless defined &_SET;
628 _ARRAY($set) or return undef;
629 foreach my $item ( @$set ) {
630 _INSTANCE($item,$_[0]) or return undef;
638 =head2 _SET0 \@array, $class
640 The C<_SET0> function is intended to be imported into your package,
641 and provides a convenient way to test for a set of objects of a
642 particular class in a strictly correct manner, allowing for zero objects.
644 The set is provided as a reference to an C<ARRAY> of objects of the
647 For an alternative function that requires at least one object, see the
650 Returns the C<ARRAY> reference itself as a convenience, or C<undef> if
651 the value provided is not a set of that class.
655 eval <<'END_PERL' unless defined &_SET0;
658 _ARRAY0($set) or return undef;
659 foreach my $item ( @$set ) {
660 _INSTANCE($item,$_[0]) or return undef;
670 The C<_HANDLE> function is intended to be imported into your package,
671 and provides a convenient way to test whether or not a single scalar
672 value is a file handle.
674 Unfortunately, in Perl the definition of a file handle can be a little
675 bit fuzzy, so this function is likely to be somewhat imperfect (at first
678 That said, it is implement as well or better than the other file handle
679 detectors in existance (and we stole from the best of them).
683 # We're doing this longhand for now. Once everything is perfect,
684 # we'll compress this into something that compiles more efficiently.
685 # Further, testing file handles is not something that is generally
686 # done millions of times, so doing it slowly is not a big speed hit.
687 eval <<'END_PERL' unless defined &_HANDLE;
691 # It has to be defined, of course
692 unless ( defined $it ) {
696 # Normal globs are considered to be file handles
697 if ( ref $it eq 'GLOB' ) {
701 # Check for a normal tied filehandle
702 # Side Note: 5.5.4's tied() and can() doesn't like getting undef
703 if ( tied($it) and tied($it)->can('TIEHANDLE') ) {
707 # There are no other non-object handles that we support
708 unless ( Scalar::Util::blessed($it) ) {
712 # Check for a common base classes for conventional IO::Handle object
713 if ( $it->isa('IO::Handle') ) {
718 # Check for tied file handles using Tie::Handle
719 if ( $it->isa('Tie::Handle') ) {
723 # IO::Scalar is not a proper seekable, but it is valid is a
724 # regular file handle
725 if ( $it->isa('IO::Scalar') ) {
729 # Yet another special case for IO::String, which refuses (for now
730 # anyway) to become a subclass of IO::Handle.
731 if ( $it->isa('IO::String') ) {
735 # This is not any sort of object we know about
742 =head2 _DRIVER $string
745 my $class = _DRIVER(shift, 'My::Driver::Base') or die "Bad driver";
749 The C<_DRIVER> function is intended to be imported into your
750 package, and provides a convenient way to load and validate
753 The most common pattern when taking a driver class as a parameter
754 is to check that the name is a class (i.e. check against _CLASS)
755 and then to load the class (if it exists) and then ensure that
756 the class returns true for the isa method on some base driver name.
758 Return the value as a convenience, or C<undef> if the value is not
759 a class name, the module does not exist, the module does not load,
760 or the class fails the isa test.
764 eval <<'END_PERL' unless defined &_DRIVER;
766 (defined _CLASS($_[0]) and eval "require $_[0];" and ! $@ and $_[0]->isa($_[1]) and $_[0] ne $_[1]) ? $_[0] : undef;
776 - Add _CAN to help resolve the UNIVERSAL::can debacle
778 - Would be even nicer if someone would demonstrate how the hell to
779 build a Module::Install dist of the ::Util dual Perl/XS type. :/
781 - Implement an assertion-like version of this module, that dies on
784 - Implement a Test:: version of this module, for use in testing
788 Bugs should be reported via the CPAN bug tracker at
790 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Params-Util>
792 For other issues, contact the author.
796 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
804 Copyright 2005 - 2009 Adam Kennedy.
806 This program is free software; you can redistribute
807 it and/or modify it under the same terms as Perl itself.
809 The full text of the license can be found in the
810 LICENSE file included with this module.