← Index
NYTProf Performance Profile   « line view »
For ../prof.pl
  Run on Thu Dec 15 15:23:56 2022
Reported on Thu Dec 15 15:27:02 2022

Filename/Users/ether/.perlbrew/libs/36.0@std/lib/perl5/darwin-2level/Params/Util.pm
StatementsExecuted 21 statements in 5.06ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
1111.96ms2.06msParams::Util::::BEGIN@63Params::Util::BEGIN@63
562155µs55µsParams::Util::::_STRINGParams::Util::_STRING (xsub)
11120µs20µsParams::Util::::BEGIN@58Params::Util::BEGIN@58
1116µs9µsParams::Util::::BEGIN@59Params::Util::BEGIN@59
1116µs24µsParams::Util::::BEGIN@61Params::Util::BEGIN@61
1115µs11µsParams::Util::::BEGIN@85Params::Util::BEGIN@85
1115µs7µsParams::Util::::BEGIN@87Params::Util::BEGIN@87
1113µs30µsParams::Util::::BEGIN@60Params::Util::BEGIN@60
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package Params::Util;
2
3=pod
4
5=head1 NAME
6
7Params::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
26C<Params::Util> provides a basic set of importable functions that makes
27checking parameters a hell of a lot easier
28
29While they can be (and are) used in other contexts, the main point
30behind this module is that the functions B<both> Do What You Mean,
31and Do The Right Thing, so they are most useful when you are getting
32params passed into your code from someone and/or somewhere else
33and you can't really trust the quality.
34
35Thus, C<Params::Util> is of most use at the edges of your API, where
36params and data are coming in from outside your code.
37
38The functions provided by C<Params::Util> check in the most strictly
39correct manner known, are documented as thoroughly as possible so their
40exact behaviour is clear, and heavily tested so make sure they are not
41fooled by weird data and Really Bad Things.
42
43To use, simply load the module providing the functions you want to use
44as arguments (as shown in the SYNOPSIS).
45
46To aid in maintainability, C<Params::Util> will B<never> export by
47default.
48
49You must explicitly name the functions you want to export, or use the
50C<:ALL> param to just have it export everything (although this is not
51recommended if you have any _FOO functions yourself with which future
52additions to C<Params::Util> may clash)
53
54=head1 FUNCTIONS
55
56=cut
57
58238µs120µs
# spent 20µs within Params::Util::BEGIN@58 which was called: # once (20µs+0s) by File::ShareDir::BEGIN@1.26 at line 58
use 5.00503;
# spent 20µs making 1 call to Params::Util::BEGIN@58
59218µs212µs
# spent 9µs (6+3) within Params::Util::BEGIN@59 which was called: # once (6µs+3µs) by File::ShareDir::BEGIN@1.26 at line 59
use strict;
# spent 9µs making 1 call to Params::Util::BEGIN@59 # spent 3µs making 1 call to strict::import
60216µs257µs
# spent 30µs (3+27) within Params::Util::BEGIN@60 which was called: # once (3µs+27µs) by File::ShareDir::BEGIN@1.26 at line 60
use warnings;
# spent 30µs making 1 call to Params::Util::BEGIN@60 # spent 27µs making 1 call to warnings::import
61218µs242µs
# spent 24µs (6+18) within Params::Util::BEGIN@61 which was called: # once (6µs+18µs) by File::ShareDir::BEGIN@1.26 at line 61
use parent qw{Exporter XSLoader};
# spent 24µs making 1 call to Params::Util::BEGIN@61 # spent 18µs making 1 call to parent::import
62
632567µs12.06ms
# spent 2.06ms (1.96+101µs) within Params::Util::BEGIN@63 which was called: # once (1.96ms+101µs) by File::ShareDir::BEGIN@1.26 at line 63
use Params::Util::PP qw();
# spent 2.06ms making 1 call to Params::Util::BEGIN@63
64
6510sour $VERSION = '1.102';
66
6710slocal $ENV{PERL_DL_NONLAZY} = 0 if $ENV{PERL_DL_NONLAZY};
6813.70ms13.69msXSLoader::load("Params::Util", $VERSION) unless $ENV{PERL_PARAMS_UTIL_PP};
# spent 3.69ms making 1 call to XSLoader::load
69
7015µsour @EXPORT_OK = qw{
71 _STRING _IDENTIFIER
72 _CLASS _CLASSISA _SUBCLASS _DRIVER _CLASSDOES
73 _NUMBER _POSINT _NONNEGINT
74 _SCALAR _SCALAR0
75 _ARRAY _ARRAY0 _ARRAYLIKE
76 _HASH _HASH0 _HASHLIKE
77 _CODE _CODELIKE
78 _INVOCANT _REGEX _INSTANCE _INSTANCEDOES
79 _SET _SET0
80 _HANDLE
81};
8212µsour %EXPORT_TAGS = (ALL => \@EXPORT_OK);
83
84## no critic (TestingAndDebugging::ProhibitNoStrict)
85234µs217µs
# spent 11µs (5+6) within Params::Util::BEGIN@85 which was called: # once (5µs+6µs) by File::ShareDir::BEGIN@1.26 at line 85
no strict "refs";
# spent 11µs making 1 call to Params::Util::BEGIN@85 # spent 6µs making 1 call to strict::unimport
861129µs4054µsParams::Util->can($_) or *$_ = Params::Util::PP->can($_) for (@EXPORT_OK);
# spent 54µs making 40 calls to UNIVERSAL::can, avg 1µs/call
872523µs29µs
# spent 7µs (5+2) within Params::Util::BEGIN@87 which was called: # once (5µs+2µs) by File::ShareDir::BEGIN@1.26 at line 87
use strict "refs";
# spent 7µs making 1 call to Params::Util::BEGIN@87 # spent 2µs making 1 call to strict::import
88
89#####################################################################
90# Param Checking Functions
91
92=pod
93
94=head2 _STRING $string
95
96The C<_STRING> function is intended to be imported into your
97package, and provides a convenient way to test to see if a value is
98a normal non-false string of non-zero length.
99
100Note that this will NOT do anything magic to deal with the special
101C<'0'> false negative case, but will return it.
102
103 # '0' not considered valid data
104 my $name = _STRING(shift) or die "Bad name";
105
106 # '0' is considered valid data
107 my $string = _STRING($_[0]) ? shift : die "Bad string";
108
109Please also note that this function expects a normal string. It does
110not support overloading or other magic techniques to get a string.
111
112Returns the string as a convenience if it is a valid string, or
113C<undef> if not.
114
115=head2 _IDENTIFIER $string
116
117The C<_IDENTIFIER> function is intended to be imported into your
118package, and provides a convenient way to test to see if a value is
119a string that is a valid Perl identifier.
120
121Returns the string as a convenience if it is a valid identifier, or
122C<undef> if not.
123
124=head2 _CLASS $string
125
126The C<_CLASS> function is intended to be imported into your
127package, and provides a convenient way to test to see if a value is
128a string that is a valid Perl class.
129
130This function only checks that the format is valid, not that the
131class is actually loaded. It also assumes "normalized" form, and does
132not accept class names such as C<::Foo> or C<D'Oh>.
133
134Returns the string as a convenience if it is a valid class name, or
135C<undef> if not.
136
137=head2 _CLASSISA $string, $class
138
139The C<_CLASSISA> function is intended to be imported into your
140package, and provides a convenient way to test to see if a value is
141a string that is a particularly class, or a subclass of it.
142
143This function checks that the format is valid and calls the -E<gt>isa
144method on the class name. It does not check that the class is actually
145loaded.
146
147It also assumes "normalized" form, and does
148not accept class names such as C<::Foo> or C<D'Oh>.
149
150Returns the string as a convenience if it is a valid class name, or
151C<undef> if not.
152
153=head2 _CLASSDOES $string, $role
154
155This routine behaves exactly like C<L</_CLASSISA>>, but checks with C<< ->DOES
156>> rather than C<< ->isa >>. This is probably only a good idea to use on Perl
1575.10 or later, when L<UNIVERSAL::DOES|UNIVERSAL::DOES/DOES> has been
158implemented.
159
160=head2 _SUBCLASS $string, $class
161
162The C<_SUBCLASS> function is intended to be imported into your
163package, and provides a convenient way to test to see if a value is
164a string that is a subclass of a specified class.
165
166This function checks that the format is valid and calls the -E<gt>isa
167method on the class name. It does not check that the class is actually
168loaded.
169
170It also assumes "normalized" form, and does
171not accept class names such as C<::Foo> or C<D'Oh>.
172
173Returns the string as a convenience if it is a valid class name, or
174C<undef> if not.
175
176=head2 _NUMBER $scalar
177
178The C<_NUMBER> function is intended to be imported into your
179package, and provides a convenient way to test to see if a value is
180a number. That is, it is defined and perl thinks it's a number.
181
182This function is basically a Params::Util-style wrapper around the
183L<Scalar::Util> C<looks_like_number> function.
184
185Returns the value as a convenience, or C<undef> if the value is not a
186number.
187
188=head2 _POSINT $integer
189
190The C<_POSINT> function is intended to be imported into your
191package, and provides a convenient way to test to see if a value is
192a positive integer (of any length).
193
194Returns the value as a convenience, or C<undef> if the value is not a
195positive integer.
196
197The name itself is derived from the XML schema constraint of the same
198name.
199
200=head2 _NONNEGINT $integer
201
202The C<_NONNEGINT> function is intended to be imported into your
203package, and provides a convenient way to test to see if a value is
204a non-negative integer (of any length). That is, a positive integer,
205or zero.
206
207Returns the value as a convenience, or C<undef> if the value is not a
208non-negative integer.
209
210As with other tests that may return false values, care should be taken
211to test via "defined" in boolean validly contexts.
212
213 unless ( defined _NONNEGINT($value) ) {
214 die "Invalid value";
215 }
216
217The name itself is derived from the XML schema constraint of the same
218name.
219
220=head2 _SCALAR \$scalar
221
222The C<_SCALAR> function is intended to be imported into your package,
223and provides a convenient way to test for a raw and unblessed
224C<SCALAR> reference, with content of non-zero length.
225
226For a version that allows zero length C<SCALAR> references, see
227the C<_SCALAR0> function.
228
229Returns the C<SCALAR> reference itself as a convenience, or C<undef>
230if the value provided is not a C<SCALAR> reference.
231
232=head2 _SCALAR0 \$scalar
233
234The C<_SCALAR0> function is intended to be imported into your package,
235and provides a convenient way to test for a raw and unblessed
236C<SCALAR0> reference, allowing content of zero-length.
237
238For a simpler "give me some content" version that requires non-zero
239length, C<_SCALAR> function.
240
241Returns the C<SCALAR> reference itself as a convenience, or C<undef>
242if the value provided is not a C<SCALAR> reference.
243
244=head2 _ARRAY $value
245
246The C<_ARRAY> function is intended to be imported into your package,
247and provides a convenient way to test for a raw and unblessed
248C<ARRAY> reference containing B<at least> one element of any kind.
249
250For a more basic form that allows zero length ARRAY references, see
251the C<_ARRAY0> function.
252
253Returns the C<ARRAY> reference itself as a convenience, or C<undef>
254if the value provided is not an C<ARRAY> reference.
255
256=head2 _ARRAY0 $value
257
258The C<_ARRAY0> function is intended to be imported into your package,
259and provides a convenient way to test for a raw and unblessed
260C<ARRAY> reference, allowing C<ARRAY> references that contain no
261elements.
262
263For a more basic "An array of something" form that also requires at
264least one element, see the C<_ARRAY> function.
265
266Returns the C<ARRAY> reference itself as a convenience, or C<undef>
267if the value provided is not an C<ARRAY> reference.
268
269=head2 _ARRAYLIKE $value
270
271The C<_ARRAYLIKE> function tests whether a given scalar value can respond to
272array dereferencing. If it can, the value is returned. If it cannot,
273C<_ARRAYLIKE> returns C<undef>.
274
275=head2 _HASH $value
276
277The C<_HASH> function is intended to be imported into your package,
278and provides a convenient way to test for a raw and unblessed
279C<HASH> reference with at least one entry.
280
281For a version of this function that allows the C<HASH> to be empty,
282see the C<_HASH0> function.
283
284Returns the C<HASH> reference itself as a convenience, or C<undef>
285if the value provided is not an C<HASH> reference.
286
287=head2 _HASH0 $value
288
289The C<_HASH0> function is intended to be imported into your package,
290and provides a convenient way to test for a raw and unblessed
291C<HASH> reference, regardless of the C<HASH> content.
292
293For a simpler "A hash of something" version that requires at least one
294element, see the C<_HASH> function.
295
296Returns the C<HASH> reference itself as a convenience, or C<undef>
297if the value provided is not an C<HASH> reference.
298
299=head2 _HASHLIKE $value
300
301The C<_HASHLIKE> function tests whether a given scalar value can respond to
302hash dereferencing. If it can, the value is returned. If it cannot,
303C<_HASHLIKE> returns C<undef>.
304
305=head2 _CODE $value
306
307The C<_CODE> function is intended to be imported into your package,
308and provides a convenient way to test for a raw and unblessed
309C<CODE> reference.
310
311Returns the C<CODE> reference itself as a convenience, or C<undef>
312if the value provided is not an C<CODE> reference.
313
314=head2 _CODELIKE $value
315
316The C<_CODELIKE> is the more generic version of C<_CODE>. Unlike C<_CODE>,
317which checks for an explicit C<CODE> reference, the C<_CODELIKE> function
318also includes things that act like them, such as blessed objects that
319overload C<'&{}'>.
320
321Please note that in the case of objects overloaded with '&{}', you will
322almost always end up also testing it in 'bool' context at some stage.
323
324For example:
325
326 sub foo {
327 my $code1 = _CODELIKE(shift) or die "No code param provided";
328 my $code2 = _CODELIKE(shift);
329 if ( $code2 ) {
330 print "Got optional second code param";
331 }
332 }
333
334As such, you will most likely always want to make sure your class has
335at least the following to allow it to evaluate to true in boolean
336context.
337
338 # Always evaluate to true in boolean context
339 use overload 'bool' => sub () { 1 };
340
341Returns the callable value as a convenience, or C<undef> if the
342value provided is not callable.
343
344Note - This function was formerly known as _CALLABLE but has been renamed
345for greater symmetry with the other _XXXXLIKE functions.
346
347The use of _CALLABLE has been deprecated. It will continue to work, but
348with a warning, until end-2006, then will be removed.
349
350I apologize for any inconvenience caused.
351
352=head2 _INVOCANT $value
353
354This routine tests whether the given value is a valid method invocant.
355This can be either an instance of an object, or a class name.
356
357If so, the value itself is returned. Otherwise, C<_INVOCANT>
358returns C<undef>.
359
360=head2 _INSTANCE $object, $class
361
362The C<_INSTANCE> function is intended to be imported into your package,
363and provides a convenient way to test for an object of a particular class
364in a strictly correct manner.
365
366Returns the object itself as a convenience, or C<undef> if the value
367provided is not an object of that type.
368
369=head2 _INSTANCEDOES $object, $role
370
371This routine behaves exactly like C<L</_INSTANCE>>, but checks with C<< ->DOES
372>> rather than C<< ->isa >>. This is probably only a good idea to use on Perl
3735.10 or later, when L<UNIVERSAL::DOES|UNIVERSAL::DOES/DOES> has been
374implemented.
375
376=head2 _REGEX $value
377
378The C<_REGEX> function is intended to be imported into your package,
379and provides a convenient way to test for a regular expression.
380
381Returns the value itself as a convenience, or C<undef> if the value
382provided is not a regular expression.
383
384=head2 _SET \@array, $class
385
386The C<_SET> function is intended to be imported into your package,
387and provides a convenient way to test for set of at least one object of
388a particular class in a strictly correct manner.
389
390The set is provided as a reference to an C<ARRAY> of objects of the
391class provided.
392
393For an alternative function that allows zero-length sets, see the
394C<_SET0> function.
395
396Returns the C<ARRAY> reference itself as a convenience, or C<undef> if
397the value provided is not a set of that class.
398
399=head2 _SET0 \@array, $class
400
401The C<_SET0> function is intended to be imported into your package,
402and provides a convenient way to test for a set of objects of a
403particular class in a strictly correct manner, allowing for zero objects.
404
405The set is provided as a reference to an C<ARRAY> of objects of the
406class provided.
407
408For an alternative function that requires at least one object, see the
409C<_SET> function.
410
411Returns the C<ARRAY> reference itself as a convenience, or C<undef> if
412the value provided is not a set of that class.
413
414=head2 _HANDLE
415
416The C<_HANDLE> function is intended to be imported into your package,
417and provides a convenient way to test whether or not a single scalar
418value is a file handle.
419
420Unfortunately, in Perl the definition of a file handle can be a little
421bit fuzzy, so this function is likely to be somewhat imperfect (at first
422anyway).
423
424That said, it is implement as well or better than the other file handle
425detectors in existence (and we stole from the best of them).
426
427=head2 _DRIVER $string
428
429 sub foo {
430 my $class = _DRIVER(shift, 'My::Driver::Base') or die "Bad driver";
431 ...
432 }
433
434The C<_DRIVER> function is intended to be imported into your
435package, and provides a convenient way to load and validate
436a driver class.
437
438The most common pattern when taking a driver class as a parameter
439is to check that the name is a class (i.e. check against _CLASS)
440and then to load the class (if it exists) and then ensure that
441the class returns true for the isa method on some base driver name.
442
443Return the value as a convenience, or C<undef> if the value is not
444a class name, the module does not exist, the module does not load,
445or the class fails the isa test.
446
447=head1 TO DO
448
449- Add _CAN to help resolve the UNIVERSAL::can debacle
450
451- Implement an assertion-like version of this module, that dies on
452error.
453
454- Implement a Test:: version of this module, for use in testing
455
456=head1 SUPPORT
457
458Bugs should be reported via the CPAN bug tracker at
459
460L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Params-Util>
461
462=head1 AUTHOR
463
464Adam Kennedy E<lt>adamk AT cpan.orgE<gt>
465
466Jens Rehsack E<lt>rehsack AT cpan.orgE<gt>
467
468=head1 SEE ALSO
469
470L<Params::Validate>
471
472=head1 COPYRIGHT
473
474Copyright 2005 - 2012 Adam Kennedy.
475
476Copyright 2020 - 2020 Jens Rehsack.
477
478This program is free software; you can redistribute
479it and/or modify it under the same terms as Perl itself.
480
481The full text of the license can be found in the
482LICENSE file included with this module.
483
484=cut
485
486112µs1;
 
# spent 55µs within Params::Util::_STRING which was called 56 times, avg 982ns/call: # 42 times (31µs+0s) by List::MoreUtils::XS::firstres at line 462 of File/ShareDir.pm, avg 738ns/call # 14 times (24µs+0s) by File::ShareDir::_DIST at line 511 of File/ShareDir.pm, avg 2µs/call
sub Params::Util::_STRING; # xsub