File Coverage

blib/lib/Params/Util.pm
Criterion Covered Total %
statement 20 20 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 27 27 100.0


line stmt bran cond sub pod time code
1             package Params::Util;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Params::Util - Simple, compact and correct param-checking functions
8              
9             =head1 SYNOPSIS
10              
11             # Import some functions
12             use Params::Util qw{_SCALAR _HASH _INSTANCE};
13            
14             # If you are lazy, or need a lot of them...
15             use Params::Util ':ALL';
16            
17             sub foo {
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;
21             # etc...
22             }
23              
24             =head1 DESCRIPTION
25              
26             C provides a basic set of importable functions that makes
27             checking parameters a hell of a lot easier
28              
29             While they can be (and are) used in other contexts, the main point
30             behind this module is that the functions B 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.
34              
35             Thus, C is of most use at the edges of your API, where
36             params and data are coming in from outside your code.
37              
38             The functions provided by C 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.
42              
43             To use, simply load the module providing the functions you want to use
44             as arguments (as shown in the SYNOPSIS).
45              
46             To aid in maintainability, C will B export by
47             default.
48              
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 may clash)
53              
54             =head1 FUNCTIONS
55              
56             =cut
57              
58 18     18   2934729 use 5.00503;
  18         70  
59 18     18   112 use strict;
  18         97  
  18         625  
60 18     18   113 use warnings;
  18         31  
  18         1226  
61 18     18   6606 use parent qw{Exporter XSLoader};
  18         4435  
  18         129  
62              
63 18     18   10321 use Params::Util::PP qw();
  18         60  
  18         2725  
64              
65             our $VERSION = '1.103_01';
66             $VERSION =~ tr/_//d;
67              
68             local $ENV{PERL_DL_NONLAZY} = 0 if $ENV{PERL_DL_NONLAZY};
69             XSLoader::load("Params::Util", $VERSION) unless $ENV{PERL_PARAMS_UTIL_PP};
70              
71             our @EXPORT_OK = qw{
72             _STRING _IDENTIFIER
73             _CLASS _CLASSISA _SUBCLASS _DRIVER _CLASSDOES
74             _NUMBER _POSINT _NONNEGINT
75             _SCALAR _SCALAR0
76             _ARRAY _ARRAY0 _ARRAYLIKE
77             _HASH _HASH0 _HASHLIKE
78             _CODE _CODELIKE
79             _INVOCANT _REGEX _INSTANCE _INSTANCEDOES
80             _SET _SET0
81             _HANDLE
82             };
83             our %EXPORT_TAGS = (ALL => \@EXPORT_OK);
84              
85             ## no critic (TestingAndDebugging::ProhibitNoStrict)
86 18     18   144 no strict "refs";
  18         31  
  18         1523  
87             Params::Util->can($_) or *$_ = Params::Util::PP->can($_) for (@EXPORT_OK);
88 18     18   106 use strict "refs";
  18         210  
  18         2560  
89              
90             #####################################################################
91             # Param Checking Functions
92              
93             =pod
94              
95             =head2 _STRING $string
96              
97             The C<_STRING> function is intended to be imported into your
98             package, and provides a convenient way to test to see if a value is
99             a normal non-false string of non-zero length.
100              
101             Note that this will NOT do anything magic to deal with the special
102             C<'0'> false negative case, but will return it.
103              
104             # '0' not considered valid data
105             my $name = _STRING(shift) or die "Bad name";
106            
107             # '0' is considered valid data
108             my $string = _STRING($_[0]) ? shift : die "Bad string";
109              
110             Please also note that this function expects a normal string. It does
111             not support overloading or other magic techniques to get a string.
112              
113             Returns the string as a convenience if it is a valid string, or
114             C if not.
115              
116             =head2 _IDENTIFIER $string
117              
118             The C<_IDENTIFIER> function is intended to be imported into your
119             package, and provides a convenient way to test to see if a value is
120             a string that is a valid Perl identifier.
121              
122             Returns the string as a convenience if it is a valid identifier, or
123             C if not.
124              
125             =head2 _CLASS $string
126              
127             The C<_CLASS> function is intended to be imported into your
128             package, and provides a convenient way to test to see if a value is
129             a string that is a valid Perl class.
130              
131             This function only checks that the format is valid, not that the
132             class is actually loaded. It also assumes "normalized" form, and does
133             not accept class names such as C<::Foo> or C.
134              
135             Returns the string as a convenience if it is a valid class name, or
136             C if not.
137              
138             =head2 _CLASSISA $string, $class
139              
140             The C<_CLASSISA> function is intended to be imported into your
141             package, and provides a convenient way to test to see if a value is
142             a string that is a particularly class, or a subclass of it.
143              
144             This function checks that the format is valid and calls the -Eisa
145             method on the class name. It does not check that the class is actually
146             loaded.
147              
148             It also assumes "normalized" form, and does
149             not accept class names such as C<::Foo> or C.
150              
151             Returns the string as a convenience if it is a valid class name, or
152             C if not.
153              
154             =head2 _CLASSDOES $string, $role
155              
156             This routine behaves exactly like C>, but checks with C<< ->DOES
157             >> rather than C<< ->isa >>. This is probably only a good idea to use on Perl
158             5.10 or later, when L has been
159             implemented.
160              
161             =head2 _SUBCLASS $string, $class
162              
163             The C<_SUBCLASS> function is intended to be imported into your
164             package, and provides a convenient way to test to see if a value is
165             a string that is a subclass of a specified class.
166              
167             This function checks that the format is valid and calls the -Eisa
168             method on the class name. It does not check that the class is actually
169             loaded.
170              
171             It also assumes "normalized" form, and does
172             not accept class names such as C<::Foo> or C.
173              
174             Returns the string as a convenience if it is a valid class name, or
175             C if not.
176              
177             =head2 _NUMBER $scalar
178              
179             The C<_NUMBER> function is intended to be imported into your
180             package, and provides a convenient way to test to see if a value is
181             a number. That is, it is defined and perl thinks it's a number.
182              
183             This function is basically a Params::Util-style wrapper around the
184             L C function.
185              
186             Returns the value as a convenience, or C if the value is not a
187             number.
188              
189             =head2 _POSINT $integer
190              
191             The C<_POSINT> function is intended to be imported into your
192             package, and provides a convenient way to test to see if a value is
193             a positive integer (of any length).
194              
195             Returns the value as a convenience, or C if the value is not a
196             positive integer.
197              
198             The name itself is derived from the XML schema constraint of the same
199             name.
200              
201             =head2 _NONNEGINT $integer
202              
203             The C<_NONNEGINT> 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 non-negative integer (of any length). That is, a positive integer,
206             or zero.
207              
208             Returns the value as a convenience, or C if the value is not a
209             non-negative integer.
210              
211             As with other tests that may return false values, care should be taken
212             to test via "defined" in boolean validly contexts.
213              
214             unless ( defined _NONNEGINT($value) ) {
215             die "Invalid value";
216             }
217              
218             The name itself is derived from the XML schema constraint of the same
219             name.
220              
221             =head2 _SCALAR \$scalar
222              
223             The C<_SCALAR> function is intended to be imported into your package,
224             and provides a convenient way to test for a raw and unblessed
225             C reference, with content of non-zero length.
226              
227             For a version that allows zero length C references, see
228             the C<_SCALAR0> function.
229              
230             Returns the C reference itself as a convenience, or C
231             if the value provided is not a C reference.
232              
233             =head2 _SCALAR0 \$scalar
234              
235             The C<_SCALAR0> function is intended to be imported into your package,
236             and provides a convenient way to test for a raw and unblessed
237             C reference, allowing content of zero-length.
238              
239             For a simpler "give me some content" version that requires non-zero
240             length, C<_SCALAR> function.
241              
242             Returns the C reference itself as a convenience, or C
243             if the value provided is not a C reference.
244              
245             =head2 _ARRAY $value
246              
247             The C<_ARRAY> function is intended to be imported into your package,
248             and provides a convenient way to test for a raw and unblessed
249             C reference containing B one element of any kind.
250              
251             For a more basic form that allows zero length ARRAY references, see
252             the C<_ARRAY0> function.
253              
254             Returns the C reference itself as a convenience, or C
255             if the value provided is not an C reference.
256              
257             =head2 _ARRAY0 $value
258              
259             The C<_ARRAY0> function is intended to be imported into your package,
260             and provides a convenient way to test for a raw and unblessed
261             C reference, allowing C references that contain no
262             elements.
263              
264             For a more basic "An array of something" form that also requires at
265             least one element, see the C<_ARRAY> function.
266              
267             Returns the C reference itself as a convenience, or C
268             if the value provided is not an C reference.
269              
270             =head2 _ARRAYLIKE $value
271              
272             The C<_ARRAYLIKE> function tests whether a given scalar value can respond to
273             array dereferencing. If it can, the value is returned. If it cannot,
274             C<_ARRAYLIKE> returns C.
275              
276             =head2 _HASH $value
277              
278             The C<_HASH> function is intended to be imported into your package,
279             and provides a convenient way to test for a raw and unblessed
280             C reference with at least one entry.
281              
282             For a version of this function that allows the C to be empty,
283             see the C<_HASH0> function.
284              
285             Returns the C reference itself as a convenience, or C
286             if the value provided is not an C reference.
287              
288             =head2 _HASH0 $value
289              
290             The C<_HASH0> function is intended to be imported into your package,
291             and provides a convenient way to test for a raw and unblessed
292             C reference, regardless of the C content.
293              
294             For a simpler "A hash of something" version that requires at least one
295             element, see the C<_HASH> function.
296              
297             Returns the C reference itself as a convenience, or C
298             if the value provided is not an C reference.
299              
300             =head2 _HASHLIKE $value
301              
302             The C<_HASHLIKE> function tests whether a given scalar value can respond to
303             hash dereferencing. If it can, the value is returned. If it cannot,
304             C<_HASHLIKE> returns C.
305              
306             =head2 _CODE $value
307              
308             The C<_CODE> function is intended to be imported into your package,
309             and provides a convenient way to test for a raw and unblessed
310             C reference.
311              
312             Returns the C reference itself as a convenience, or C
313             if the value provided is not an C reference.
314              
315             =head2 _CODELIKE $value
316              
317             The C<_CODELIKE> is the more generic version of C<_CODE>. Unlike C<_CODE>,
318             which checks for an explicit C reference, the C<_CODELIKE> function
319             also includes things that act like them, such as blessed objects that
320             overload C<'&{}'>.
321              
322             Please note that in the case of objects overloaded with '&{}', you will
323             almost always end up also testing it in 'bool' context at some stage.
324              
325             For example:
326              
327             sub foo {
328             my $code1 = _CODELIKE(shift) or die "No code param provided";
329             my $code2 = _CODELIKE(shift);
330             if ( $code2 ) {
331             print "Got optional second code param";
332             }
333             }
334              
335             As such, you will most likely always want to make sure your class has
336             at least the following to allow it to evaluate to true in boolean
337             context.
338              
339             # Always evaluate to true in boolean context
340             use overload 'bool' => sub () { 1 };
341              
342             Returns the callable value as a convenience, or C if the
343             value provided is not callable.
344              
345             Note - This function was formerly known as _CALLABLE but has been renamed
346             for greater symmetry with the other _XXXXLIKE functions.
347              
348             The use of _CALLABLE has been deprecated. It will continue to work, but
349             with a warning, until end-2006, then will be removed.
350              
351             I apologize for any inconvenience caused.
352              
353             =head2 _INVOCANT $value
354              
355             This routine tests whether the given value is a valid method invocant.
356             This can be either an instance of an object, or a class name.
357              
358             If so, the value itself is returned. Otherwise, C<_INVOCANT>
359             returns C.
360              
361             =head2 _INSTANCE $object, $class
362              
363             The C<_INSTANCE> function is intended to be imported into your package,
364             and provides a convenient way to test for an object of a particular class
365             in a strictly correct manner.
366              
367             Returns the object itself as a convenience, or C if the value
368             provided is not an object of that type.
369              
370             =head2 _INSTANCEDOES $object, $role
371              
372             This routine behaves exactly like C>, but checks with C<< ->DOES
373             >> rather than C<< ->isa >>. This is probably only a good idea to use on Perl
374             5.10 or later, when L has been
375             implemented.
376              
377             =head2 _REGEX $value
378              
379             The C<_REGEX> function is intended to be imported into your package,
380             and provides a convenient way to test for a regular expression.
381              
382             Returns the value itself as a convenience, or C if the value
383             provided is not a regular expression.
384              
385             =head2 _SET \@array, $class
386              
387             The C<_SET> function is intended to be imported into your package,
388             and provides a convenient way to test for set of at least one object of
389             a particular class in a strictly correct manner.
390              
391             The set is provided as a reference to an C of objects of the
392             class provided.
393              
394             For an alternative function that allows zero-length sets, see the
395             C<_SET0> function.
396              
397             Returns the C reference itself as a convenience, or C if
398             the value provided is not a set of that class.
399              
400             =head2 _SET0 \@array, $class
401              
402             The C<_SET0> function is intended to be imported into your package,
403             and provides a convenient way to test for a set of objects of a
404             particular class in a strictly correct manner, allowing for zero objects.
405              
406             The set is provided as a reference to an C of objects of the
407             class provided.
408              
409             For an alternative function that requires at least one object, see the
410             C<_SET> function.
411              
412             Returns the C reference itself as a convenience, or C if
413             the value provided is not a set of that class.
414              
415             =head2 _HANDLE
416              
417             The C<_HANDLE> function is intended to be imported into your package,
418             and provides a convenient way to test whether or not a single scalar
419             value is a file handle.
420              
421             Unfortunately, in Perl the definition of a file handle can be a little
422             bit fuzzy, so this function is likely to be somewhat imperfect (at first
423             anyway).
424              
425             That said, it is implement as well or better than the other file handle
426             detectors in existence (and we stole from the best of them).
427              
428             =head2 _DRIVER $string
429              
430             sub foo {
431             my $class = _DRIVER(shift, 'My::Driver::Base') or die "Bad driver";
432             ...
433             }
434              
435             The C<_DRIVER> function is intended to be imported into your
436             package, and provides a convenient way to load and validate
437             a driver class.
438              
439             The most common pattern when taking a driver class as a parameter
440             is to check that the name is a class (i.e. check against _CLASS)
441             and then to load the class (if it exists) and then ensure that
442             the class returns true for the isa method on some base driver name.
443              
444             Return the value as a convenience, or C if the value is not
445             a class name, the module does not exist, the module does not load,
446             or the class fails the isa test.
447              
448             =head1 TO DO
449              
450             - Add _CAN to help resolve the UNIVERSAL::can debacle
451              
452             - Implement an assertion-like version of this module, that dies on
453             error.
454              
455             - Implement a Test:: version of this module, for use in testing
456              
457             =head1 SUPPORT
458              
459             Bugs should be reported via the CPAN bug tracker at
460              
461             L
462              
463             =head1 AUTHOR
464              
465             Adam Kennedy Eadamk AT cpan.orgE
466              
467             Jens Rehsack Erehsack AT cpan.orgE
468              
469             =head1 SEE ALSO
470              
471             L
472              
473             =head1 COPYRIGHT
474              
475             Copyright 2005 - 2012 Adam Kennedy.
476              
477             Copyright 2020 - 2020 Jens Rehsack.
478              
479             This program is free software; you can redistribute
480             it and/or modify it under the same terms as Perl itself.
481              
482             The full text of the license can be found in the
483             LICENSE file included with this module.
484              
485             =cut
486              
487             1;