1 # Original package Params::Util downloaded from CPAN on 6/15/2012. This module
2 # has been modified by Andreas Schuh on 6/15/2012 to make it part of BASIS.
4 package BASIS::Params::Util;
10 BASIS::Params::Util - Simple, compact and correct param-checking functions
14 # Import some functions
15 use BASIS::Params::Util qw{_SCALAR _HASH _INSTANCE};
17 # If you are lazy, or need a lot of them...
18 use BASIS::Params::Util ':ALL';
21 my $object = _INSTANCE(shift, 'Foo') or return undef;
22 my $image = _SCALAR(shift) or return undef;
23 my $options = _HASH(shift) or return undef;
29 C<BASIS::Params::Util> provides a basic set of importable functions that makes
30 checking parameters a hell of a lot easier
32 While they can be (and are) used in other contexts, the main point
33 behind this module is that the functions B<both> Do What You Mean,
34 and Do The Right Thing, so they are most useful when you are getting
35 params passed into your code from someone and/or somewhere else
36 and you can't really trust the quality.
38 Thus, C<BASIS::Params::Util> is of most use at the edges of your API, where
39 params and data are coming in from outside your code.
41 The functions provided by C<BASIS::Params::Util> check in the most strictly
42 correct manner known, are documented as thoroughly as possible so their
43 exact behaviour is clear, and heavily tested so make sure they are not
44 fooled by weird data and Really Bad Things.
46 To use, simply load the module providing the functions you want to use
47 as arguments (as shown in the SYNOPSIS).
49 To aid in maintainability, C<BASIS::Params::Util> will B<never> export by
52 You must explicitly name the functions you want to export, or use the
53 C<:ALL> param to just have it export everything (although this is not
54 recommended if you have any _FOO functions yourself with which future
55 additions to C<BASIS::Params::Util> may clash)
68 use vars qw{$VERSION @ISA @EXPORT_OK %EXPORT_TAGS};
77 _CLASS _CLASSISA _SUBCLASS _DRIVER _CLASSDOES
78 _NUMBER _POSINT _NONNEGINT
80 _ARRAY _ARRAY0 _ARRAYLIKE
81 _HASH _HASH0 _HASHLIKE
83 _INVOCANT _REGEX _INSTANCE _INSTANCEDOES
87 %EXPORT_TAGS = ( ALL => \@EXPORT_OK );
90 local $ENV{PERL_DL_NONLAZY} = 0 if $ENV{PERL_DL_NONLAZY};
91 bootstrap BASIS::Params::Util $VERSION;
93 } unless $ENV{PERL_PARAMS_UTIL_PP};
95 # Use a private pure-perl copy of looks_like_number if the version of
96 # Scalar::Util is old (for whatever reason).
97 my $SU = eval "$Scalar::Util::VERSION" || 0;
99 Scalar::Util->import('looks_like_number');
102 sub looks_like_number {
105 # checks from perlfaq4
106 return 0 if !defined($_);
108 return overload::Overloaded($_) ? defined(0 + $_) : 0;
110 return 1 if (/^[+-]?[0-9]+$/); # is a +/- integer
111 return 1 if (/^([+-]?)(?=[0-9]|\.[0-9])[0-9]*(\.[0-9]*)?([Ee]([+-]?[0-9]+))?$/); # a C float
112 return 1 if ($] >= 5.008 and /^(Inf(inity)?|NaN)$/i) or ($] >= 5.006001 and /^Inf$/i);
123 #####################################################################
124 # Param Checking Functions
128 =head2 _STRING $string
130 The C<_STRING> function is intended to be imported into your
131 package, and provides a convenient way to test to see if a value is
132 a normal non-false string of non-zero length.
134 Note that this will NOT do anything magic to deal with the special
135 C<'0'> false negative case, but will return it.
137 # '0' not considered valid data
138 my $name = _STRING(shift) or die "Bad name";
140 # '0' is considered valid data
141 my $string = _STRING($_[0]) ? shift : die "Bad string";
143 Please also note that this function expects a normal string. It does
144 not support overloading or other magic techniques to get a string.
146 Returns the string as a conveince if it is a valid string, or
151 eval <<'END_PERL' unless defined &_STRING;
153 (defined $_[0] and ! ref $_[0] and length($_[0])) ? $_[0] : undef;
159 =head2 _IDENTIFIER $string
161 The C<_IDENTIFIER> function is intended to be imported into your
162 package, and provides a convenient way to test to see if a value is
163 a string that is a valid Perl identifier.
165 Returns the string as a convenience if it is a valid identifier, or
170 eval <<'END_PERL' unless defined &_IDENTIFIER;
171 sub _IDENTIFIER ($) {
172 (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^[^\W\d]\w*\z/s) ? $_[0] : undef;
178 =head2 _CLASS $string
180 The C<_CLASS> function is intended to be imported into your
181 package, and provides a convenient way to test to see if a value is
182 a string that is a valid Perl class.
184 This function only checks that the format is valid, not that the
185 class is actually loaded. It also assumes "normalised" form, and does
186 not accept class names such as C<::Foo> or C<D'Oh>.
188 Returns the string as a convenience if it is a valid class name, or
193 eval <<'END_PERL' unless defined &_CLASS;
195 (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^[^\W\d]\w*(?:::\w+)*\z/s) ? $_[0] : undef;
201 =head2 _CLASSISA $string, $class
203 The C<_CLASSISA> function is intended to be imported into your
204 package, and provides a convenient way to test to see if a value is
205 a string that is a particularly class, or a subclass of it.
207 This function checks that the format is valid and calls the -E<gt>isa
208 method on the class name. It does not check that the class is actually
211 It also assumes "normalised" form, and does
212 not accept class names such as C<::Foo> or C<D'Oh>.
214 Returns the string as a convenience if it is a valid class name, or
219 eval <<'END_PERL' unless defined &_CLASSISA;
221 (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^[^\W\d]\w*(?:::\w+)*\z/s and $_[0]->isa($_[1])) ? $_[0] : undef;
225 =head2 _CLASSDOES $string, $role
227 This routine behaves exactly like C<L</_CLASSISA>>, but checks with C<< ->DOES
228 >> rather than C<< ->isa >>. This is probably only a good idea to use on Perl
229 5.10 or later, when L<UNIVERSAL::DOES|UNIVERSAL::DOES/DOES> has been
234 eval <<'END_PERL' unless defined &_CLASSDOES;
235 sub _CLASSDOES ($$) {
236 (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^[^\W\d]\w*(?:::\w+)*\z/s and $_[0]->DOES($_[1])) ? $_[0] : undef;
242 =head2 _SUBCLASS $string, $class
244 The C<_SUBCLASS> function is intended to be imported into your
245 package, and provides a convenient way to test to see if a value is
246 a string that is a subclass of a specified class.
248 This function checks that the format is valid and calls the -E<gt>isa
249 method on the class name. It does not check that the class is actually
252 It also assumes "normalised" form, and does
253 not accept class names such as C<::Foo> or C<D'Oh>.
255 Returns the string as a convenience if it is a valid class name, or
260 eval <<'END_PERL' unless defined &_SUBCLASS;
262 (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^[^\W\d]\w*(?:::\w+)*\z/s and $_[0] ne $_[1] and $_[0]->isa($_[1])) ? $_[0] : undef;
268 =head2 _NUMBER $scalar
270 The C<_NUMBER> function is intended to be imported into your
271 package, and provides a convenient way to test to see if a value is
272 a number. That is, it is defined and perl thinks it's a number.
274 This function is basically a BASIS::Params::Util-style wrapper around the
275 L<Scalar::Util> C<looks_like_number> function.
277 Returns the value as a convience, or C<undef> if the value is not a
282 eval <<'END_PERL' unless defined &_NUMBER;
284 ( defined $_[0] and ! ref $_[0] and looks_like_number($_[0]) )
292 =head2 _POSINT $integer
294 The C<_POSINT> function is intended to be imported into your
295 package, and provides a convenient way to test to see if a value is
296 a positive integer (of any length).
298 Returns the value as a convience, or C<undef> if the value is not a
301 The name itself is derived from the XML schema constraint of the same
306 eval <<'END_PERL' unless defined &_POSINT;
308 (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^[1-9]\d*$/) ? $_[0] : undef;
314 =head2 _NONNEGINT $integer
316 The C<_NONNEGINT> function is intended to be imported into your
317 package, and provides a convenient way to test to see if a value is
318 a non-negative integer (of any length). That is, a positive integer,
321 Returns the value as a convience, or C<undef> if the value is not a
322 non-negative integer.
324 As with other tests that may return false values, care should be taken
325 to test via "defined" in boolean validy contexts.
327 unless ( defined _NONNEGINT($value) ) {
331 The name itself is derived from the XML schema constraint of the same
336 eval <<'END_PERL' unless defined &_NONNEGINT;
338 (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^(?:0|[1-9]\d*)$/) ? $_[0] : undef;
344 =head2 _SCALAR \$scalar
346 The C<_SCALAR> function is intended to be imported into your package,
347 and provides a convenient way to test for a raw and unblessed
348 C<SCALAR> reference, with content of non-zero length.
350 For a version that allows zero length C<SCALAR> references, see
351 the C<_SCALAR0> function.
353 Returns the C<SCALAR> reference itself as a convenience, or C<undef>
354 if the value provided is not a C<SCALAR> reference.
358 eval <<'END_PERL' unless defined &_SCALAR;
360 (ref $_[0] eq 'SCALAR' and defined ${$_[0]} and ${$_[0]} ne '') ? $_[0] : undef;
366 =head2 _SCALAR0 \$scalar
368 The C<_SCALAR0> function is intended to be imported into your package,
369 and provides a convenient way to test for a raw and unblessed
370 C<SCALAR0> reference, allowing content of zero-length.
372 For a simpler "give me some content" version that requires non-zero
373 length, C<_SCALAR> function.
375 Returns the C<SCALAR> reference itself as a convenience, or C<undef>
376 if the value provided is not a C<SCALAR> reference.
380 eval <<'END_PERL' unless defined &_SCALAR0;
382 ref $_[0] eq 'SCALAR' ? $_[0] : undef;
390 The C<_ARRAY> function is intended to be imported into your package,
391 and provides a convenient way to test for a raw and unblessed
392 C<ARRAY> reference containing B<at least> one element of any kind.
394 For a more basic form that allows zero length ARRAY references, see
395 the C<_ARRAY0> function.
397 Returns the C<ARRAY> reference itself as a convenience, or C<undef>
398 if the value provided is not an C<ARRAY> reference.
402 eval <<'END_PERL' unless defined &_ARRAY;
404 (ref $_[0] eq 'ARRAY' and @{$_[0]}) ? $_[0] : undef;
410 =head2 _ARRAY0 $value
412 The C<_ARRAY0> function is intended to be imported into your package,
413 and provides a convenient way to test for a raw and unblessed
414 C<ARRAY> reference, allowing C<ARRAY> references that contain no
417 For a more basic "An array of something" form that also requires at
418 least one element, see the C<_ARRAY> function.
420 Returns the C<ARRAY> reference itself as a convenience, or C<undef>
421 if the value provided is not an C<ARRAY> reference.
425 eval <<'END_PERL' unless defined &_ARRAY0;
427 ref $_[0] eq 'ARRAY' ? $_[0] : undef;
433 =head2 _ARRAYLIKE $value
435 The C<_ARRAYLIKE> function tests whether a given scalar value can respond to
436 array dereferencing. If it can, the value is returned. If it cannot,
437 C<_ARRAYLIKE> returns C<undef>.
441 eval <<'END_PERL' unless defined &_ARRAYLIKE;
443 (defined $_[0] and ref $_[0] and (
444 (Scalar::Util::reftype($_[0]) eq 'ARRAY')
446 overload::Method($_[0], '@{}')
455 The C<_HASH> function is intended to be imported into your package,
456 and provides a convenient way to test for a raw and unblessed
457 C<HASH> reference with at least one entry.
459 For a version of this function that allows the C<HASH> to be empty,
460 see the C<_HASH0> function.
462 Returns the C<HASH> reference itself as a convenience, or C<undef>
463 if the value provided is not an C<HASH> reference.
467 eval <<'END_PERL' unless defined &_HASH;
469 (ref $_[0] eq 'HASH' and scalar %{$_[0]}) ? $_[0] : undef;
477 The C<_HASH0> function is intended to be imported into your package,
478 and provides a convenient way to test for a raw and unblessed
479 C<HASH> reference, regardless of the C<HASH> content.
481 For a simpler "A hash of something" version that requires at least one
482 element, see the C<_HASH> function.
484 Returns the C<HASH> reference itself as a convenience, or C<undef>
485 if the value provided is not an C<HASH> reference.
489 eval <<'END_PERL' unless defined &_HASH0;
491 ref $_[0] eq 'HASH' ? $_[0] : undef;
497 =head2 _HASHLIKE $value
499 The C<_HASHLIKE> function tests whether a given scalar value can respond to
500 hash dereferencing. If it can, the value is returned. If it cannot,
501 C<_HASHLIKE> returns C<undef>.
505 eval <<'END_PERL' unless defined &_HASHLIKE;
507 (defined $_[0] and ref $_[0] and (
508 (Scalar::Util::reftype($_[0]) eq 'HASH')
510 overload::Method($_[0], '%{}')
519 The C<_CODE> function is intended to be imported into your package,
520 and provides a convenient way to test for a raw and unblessed
523 Returns the C<CODE> reference itself as a convenience, or C<undef>
524 if the value provided is not an C<CODE> reference.
528 eval <<'END_PERL' unless defined &_CODE;
530 ref $_[0] eq 'CODE' ? $_[0] : undef;
536 =head2 _CODELIKE $value
538 The C<_CODELIKE> is the more generic version of C<_CODE>. Unlike C<_CODE>,
539 which checks for an explicit C<CODE> reference, the C<_CODELIKE> function
540 also includes things that act like them, such as blessed objects that
543 Please note that in the case of objects overloaded with '&{}', you will
544 almost always end up also testing it in 'bool' context at some stage.
549 my $code1 = _CODELIKE(shift) or die "No code param provided";
550 my $code2 = _CODELIKE(shift);
552 print "Got optional second code param";
556 As such, you will most likely always want to make sure your class has
557 at least the following to allow it to evaluate to true in boolean
560 # Always evaluate to true in boolean context
561 use overload 'bool' => sub () { 1 };
563 Returns the callable value as a convenience, or C<undef> if the
564 value provided is not callable.
566 Note - This function was formerly known as _CALLABLE but has been renamed
567 for greater symmetry with the other _XXXXLIKE functions.
569 The use of _CALLABLE has been deprecated. It will continue to work, but
570 with a warning, until end-2006, then will be removed.
572 I apologise for any inconvenience caused.
576 eval <<'END_PERL' unless defined &_CODELIKE;
579 (Scalar::Util::reftype($_[0])||'') eq 'CODE'
581 Scalar::Util::blessed($_[0]) and overload::Method($_[0],'&{}')
589 =head2 _INVOCANT $value
591 This routine tests whether the given value is a valid method invocant.
592 This can be either an instance of an object, or a class name.
594 If so, the value itself is returned. Otherwise, C<_INVOCANT>
599 eval <<'END_PERL' unless defined &_INVOCANT;
602 (defined Scalar::Util::blessed($_[0])
604 # We used to check for stash definedness, but any class-like name is a
605 # valid invocant for UNIVERSAL methods, so we stopped. -- rjbs, 2006-07-02
606 BASIS::Params::Util::_CLASS($_[0]))
613 =head2 _INSTANCE $object, $class
615 The C<_INSTANCE> function is intended to be imported into your package,
616 and provides a convenient way to test for an object of a particular class
617 in a strictly correct manner.
619 Returns the object itself as a convenience, or C<undef> if the value
620 provided is not an object of that type.
624 eval <<'END_PERL' unless defined &_INSTANCE;
626 (Scalar::Util::blessed($_[0]) and $_[0]->isa($_[1])) ? $_[0] : undef;
630 =head2 _INSTANCEDOES $object, $role
632 This routine behaves exactly like C<L</_INSTANCE>>, but checks with C<< ->DOES
633 >> rather than C<< ->isa >>. This is probably only a good idea to use on Perl
634 5.10 or later, when L<UNIVERSAL::DOES|UNIVERSAL::DOES/DOES> has been
639 eval <<'END_PERL' unless defined &_INSTANCEDOES;
640 sub _INSTANCEDOES ($$) {
641 (Scalar::Util::blessed($_[0]) and $_[0]->DOES($_[1])) ? $_[0] : undef;
649 The C<_REGEX> function is intended to be imported into your package,
650 and provides a convenient way to test for a regular expression.
652 Returns the value itself as a convenience, or C<undef> if the value
653 provided is not a regular expression.
657 eval <<'END_PERL' unless defined &_REGEX;
659 (defined $_[0] and 'Regexp' eq ref($_[0])) ? $_[0] : undef;
665 =head2 _SET \@array, $class
667 The C<_SET> function is intended to be imported into your package,
668 and provides a convenient way to test for set of at least one object of
669 a particular class in a strictly correct manner.
671 The set is provided as a reference to an C<ARRAY> of objects of the
674 For an alternative function that allows zero-length sets, see the
677 Returns the C<ARRAY> reference itself as a convenience, or C<undef> if
678 the value provided is not a set of that class.
682 eval <<'END_PERL' unless defined &_SET;
685 _ARRAY($set) or return undef;
686 foreach my $item ( @$set ) {
687 _INSTANCE($item,$_[0]) or return undef;
695 =head2 _SET0 \@array, $class
697 The C<_SET0> function is intended to be imported into your package,
698 and provides a convenient way to test for a set of objects of a
699 particular class in a strictly correct manner, allowing for zero objects.
701 The set is provided as a reference to an C<ARRAY> of objects of the
704 For an alternative function that requires at least one object, see the
707 Returns the C<ARRAY> reference itself as a convenience, or C<undef> if
708 the value provided is not a set of that class.
712 eval <<'END_PERL' unless defined &_SET0;
715 _ARRAY0($set) or return undef;
716 foreach my $item ( @$set ) {
717 _INSTANCE($item,$_[0]) or return undef;
727 The C<_HANDLE> function is intended to be imported into your package,
728 and provides a convenient way to test whether or not a single scalar
729 value is a file handle.
731 Unfortunately, in Perl the definition of a file handle can be a little
732 bit fuzzy, so this function is likely to be somewhat imperfect (at first
735 That said, it is implement as well or better than the other file handle
736 detectors in existance (and we stole from the best of them).
740 # We're doing this longhand for now. Once everything is perfect,
741 # we'll compress this into something that compiles more efficiently.
742 # Further, testing file handles is not something that is generally
743 # done millions of times, so doing it slowly is not a big speed hit.
744 eval <<'END_PERL' unless defined &_HANDLE;
748 # It has to be defined, of course
749 unless ( defined $it ) {
753 # Normal globs are considered to be file handles
754 if ( ref $it eq 'GLOB' ) {
758 # Check for a normal tied filehandle
759 # Side Note: 5.5.4's tied() and can() doesn't like getting undef
760 if ( tied($it) and tied($it)->can('TIEHANDLE') ) {
764 # There are no other non-object handles that we support
765 unless ( Scalar::Util::blessed($it) ) {
769 # Check for a common base classes for conventional IO::Handle object
770 if ( $it->isa('IO::Handle') ) {
775 # Check for tied file handles using Tie::Handle
776 if ( $it->isa('Tie::Handle') ) {
780 # IO::Scalar is not a proper seekable, but it is valid is a
781 # regular file handle
782 if ( $it->isa('IO::Scalar') ) {
786 # Yet another special case for IO::String, which refuses (for now
787 # anyway) to become a subclass of IO::Handle.
788 if ( $it->isa('IO::String') ) {
792 # This is not any sort of object we know about
799 =head2 _DRIVER $string
802 my $class = _DRIVER(shift, 'My::Driver::Base') or die "Bad driver";
806 The C<_DRIVER> function is intended to be imported into your
807 package, and provides a convenient way to load and validate
810 The most common pattern when taking a driver class as a parameter
811 is to check that the name is a class (i.e. check against _CLASS)
812 and then to load the class (if it exists) and then ensure that
813 the class returns true for the isa method on some base driver name.
815 Return the value as a convenience, or C<undef> if the value is not
816 a class name, the module does not exist, the module does not load,
817 or the class fails the isa test.
821 eval <<'END_PERL' unless defined &_DRIVER;
823 (defined _CLASS($_[0]) and eval "require $_[0];" and ! $@ and $_[0]->isa($_[1]) and $_[0] ne $_[1]) ? $_[0] : undef;
833 - Add _CAN to help resolve the UNIVERSAL::can debacle
835 - Would be even nicer if someone would demonstrate how the hell to
836 build a Module::Install dist of the ::Util dual Perl/XS type. :/
838 - Implement an assertion-like version of this module, that dies on
841 - Implement a Test:: version of this module, for use in testing
845 Bugs should be reported via the CPAN bug tracker at
847 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Params-Util>
849 For other issues, contact the author.
853 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
855 Modified by Andreas Schuh on 6/15/2012 in order to make it a subpackage
856 of the SBIA namespace for inclusion with the BASIS package.
864 Copyright 2005 - 2012 Adam Kennedy.
866 This program is free software; you can redistribute
867 it and/or modify it under the same terms as Perl itself.
869 The full text of the license can be found in the
870 LICENSE file included with this module.