Util.pm
Go to the documentation of this file.
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.
3 
4 package BASIS::Params::Util;
5 
6 =pod
7 
8 =head1 NAME
9 
10 BASIS::Params::Util - Simple, compact and correct param-checking functions
11 
12 =head1 SYNOPSIS
13 
14  # Import some functions
15  use BASIS::Params::Util qw{_SCALAR _HASH _INSTANCE};
16 
17  # If you are lazy, or need a lot of them...
18  use BASIS::Params::Util ':ALL';
19 
20  sub foo {
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;
24  # etc...
25  }
26 
27 =head1 DESCRIPTION
28 
29 C<BASIS::Params::Util> provides a basic set of importable functions that makes
30 checking parameters a hell of a lot easier
31 
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.
37 
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.
40 
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.
45 
46 To use, simply load the module providing the functions you want to use
47 as arguments (as shown in the SYNOPSIS).
48 
49 To aid in maintainability, C<BASIS::Params::Util> will B<never> export by
50 default.
51 
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)
56 
57 =head1 FUNCTIONS
58 
59 =cut
60 
61 use 5.00503;
62 use strict;
63 require overload;
64 require Exporter;
65 require Scalar::Util;
66 require DynaLoader;
67 
68 use vars qw{$VERSION @ISA @EXPORT_OK %EXPORT_TAGS};
69 
70 $VERSION = '1.07';
71 @ISA = qw{
72  Exporter
73  DynaLoader
74 };
75 @EXPORT_OK = qw{
76  _STRING _IDENTIFIER
77  _CLASS _CLASSISA _SUBCLASS _DRIVER _CLASSDOES
78  _NUMBER _POSINT _NONNEGINT
79  _SCALAR _SCALAR0
80  _ARRAY _ARRAY0 _ARRAYLIKE
81  _HASH _HASH0 _HASHLIKE
82  _CODE _CODELIKE
83  _INVOCANT _REGEX _INSTANCE _INSTANCEDOES
84  _SET _SET0
85  _HANDLE
86 };
87 %EXPORT_TAGS = ( ALL => \@EXPORT_OK );
88 
89 eval {
90  local $ENV{PERL_DL_NONLAZY} = 0 if $ENV{PERL_DL_NONLAZY};
91  bootstrap BASIS::Params::Util $VERSION;
92  1;
93 } unless $ENV{PERL_PARAMS_UTIL_PP};
94 
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;
98 if ( $SU >= 1.18 ) {
99  Scalar::Util->import('looks_like_number');
100 } else {
101  eval <<'END_PERL';
102 sub looks_like_number {
103  local $_ = shift;
104 
105  # checks from perlfaq4
106  return 0 if !defined($_);
107  if (ref($_)) {
108  return overload::Overloaded($_) ? defined(0 + $_) : 0;
109  }
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);
113 
114  0;
115 }
116 END_PERL
117 }
118 
119 
120 
121 
122 
123 #####################################################################
124 # Param Checking Functions
125 
126 =pod
127 
128 =head2 _STRING $string
129 
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.
133 
134 Note that this will NOT do anything magic to deal with the special
135 C<'0'> false negative case, but will return it.
136 
137  # '0' not considered valid data
138  my $name = _STRING(shift) or die "Bad name";
139 
140  # '0' is considered valid data
141  my $string = _STRING($_[0]) ? shift : die "Bad string";
142 
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.
145 
146 Returns the string as a conveince if it is a valid string, or
147 C<undef> if not.
148 
149 =cut
150 
151 eval <<'END_PERL' unless defined &_STRING;
152 sub _STRING ($) {
153  (defined $_[0] and ! ref $_[0] and length($_[0])) ? $_[0] : undef;
154 }
155 END_PERL
156 
157 =pod
158 
159 =head2 _IDENTIFIER $string
160 
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.
164 
165 Returns the string as a convenience if it is a valid identifier, or
166 C<undef> if not.
167 
168 =cut
169 
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;
173 }
174 END_PERL
175 
176 =pod
177 
178 =head2 _CLASS $string
179 
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.
183 
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>.
187 
188 Returns the string as a convenience if it is a valid class name, or
189 C<undef> if not.
190 
191 =cut
192 
193 eval <<'END_PERL' unless defined &_CLASS;
194 sub _CLASS ($) {
195  (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^[^\W\d]\w*(?:::\w+)*\z/s) ? $_[0] : undef;
196 }
197 END_PERL
198 
199 =pod
200 
201 =head2 _CLASSISA $string, $class
202 
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.
206 
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
209 loaded.
210 
211 It also assumes "normalised" form, and does
212 not accept class names such as C<::Foo> or C<D'Oh>.
213 
214 Returns the string as a convenience if it is a valid class name, or
215 C<undef> if not.
216 
217 =cut
218 
219 eval <<'END_PERL' unless defined &_CLASSISA;
220 sub _CLASSISA ($$) {
221  (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^[^\W\d]\w*(?:::\w+)*\z/s and $_[0]->isa($_[1])) ? $_[0] : undef;
222 }
223 END_PERL
224 
225 =head2 _CLASSDOES $string, $role
226 
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
230 implemented.
231 
232 =cut
233 
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;
237 }
238 END_PERL
239 
240 =pod
241 
242 =head2 _SUBCLASS $string, $class
243 
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.
247 
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
250 loaded.
251 
252 It also assumes "normalised" form, and does
253 not accept class names such as C<::Foo> or C<D'Oh>.
254 
255 Returns the string as a convenience if it is a valid class name, or
256 C<undef> if not.
257 
258 =cut
259 
260 eval <<'END_PERL' unless defined &_SUBCLASS;
261 sub _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;
263 }
264 END_PERL
265 
266 =pod
267 
268 =head2 _NUMBER $scalar
269 
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.
273 
274 This function is basically a BASIS::Params::Util-style wrapper around the
275 L<Scalar::Util> C<looks_like_number> function.
276 
277 Returns the value as a convience, or C<undef> if the value is not a
278 number.
279 
280 =cut
281 
282 eval <<'END_PERL' unless defined &_NUMBER;
283 sub _NUMBER ($) {
284  ( defined $_[0] and ! ref $_[0] and looks_like_number($_[0]) )
285  ? $_[0]
286  : undef;
287 }
288 END_PERL
289 
290 =pod
291 
292 =head2 _POSINT $integer
293 
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).
297 
298 Returns the value as a convience, or C<undef> if the value is not a
299 positive integer.
300 
301 The name itself is derived from the XML schema constraint of the same
302 name.
303 
304 =cut
305 
306 eval <<'END_PERL' unless defined &_POSINT;
307 sub _POSINT ($) {
308  (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^[1-9]\d*$/) ? $_[0] : undef;
309 }
310 END_PERL
311 
312 =pod
313 
314 =head2 _NONNEGINT $integer
315 
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,
319 or zero.
320 
321 Returns the value as a convience, or C<undef> if the value is not a
322 non-negative integer.
323 
324 As with other tests that may return false values, care should be taken
325 to test via "defined" in boolean validy contexts.
326 
327  unless ( defined _NONNEGINT($value) ) {
328  die "Invalid value";
329  }
330 
331 The name itself is derived from the XML schema constraint of the same
332 name.
333 
334 =cut
335 
336 eval <<'END_PERL' unless defined &_NONNEGINT;
337 sub _NONNEGINT ($) {
338  (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^(?:0|[1-9]\d*)$/) ? $_[0] : undef;
339 }
340 END_PERL
341 
342 =pod
343 
344 =head2 _SCALAR \$scalar
345 
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.
349 
350 For a version that allows zero length C<SCALAR> references, see
351 the C<_SCALAR0> function.
352 
353 Returns the C<SCALAR> reference itself as a convenience, or C<undef>
354 if the value provided is not a C<SCALAR> reference.
355 
356 =cut
357 
358 eval <<'END_PERL' unless defined &_SCALAR;
359 sub _SCALAR ($) {
360  (ref $_[0] eq 'SCALAR' and defined ${$_[0]} and ${$_[0]} ne '') ? $_[0] : undef;
361 }
362 END_PERL
363 
364 =pod
365 
366 =head2 _SCALAR0 \$scalar
367 
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.
371 
372 For a simpler "give me some content" version that requires non-zero
373 length, C<_SCALAR> function.
374 
375 Returns the C<SCALAR> reference itself as a convenience, or C<undef>
376 if the value provided is not a C<SCALAR> reference.
377 
378 =cut
379 
380 eval <<'END_PERL' unless defined &_SCALAR0;
381 sub _SCALAR0 ($) {
382  ref $_[0] eq 'SCALAR' ? $_[0] : undef;
383 }
384 END_PERL
385 
386 =pod
387 
388 =head2 _ARRAY $value
389 
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.
393 
394 For a more basic form that allows zero length ARRAY references, see
395 the C<_ARRAY0> function.
396 
397 Returns the C<ARRAY> reference itself as a convenience, or C<undef>
398 if the value provided is not an C<ARRAY> reference.
399 
400 =cut
401 
402 eval <<'END_PERL' unless defined &_ARRAY;
403 sub _ARRAY ($) {
404  (ref $_[0] eq 'ARRAY' and @{$_[0]}) ? $_[0] : undef;
405 }
406 END_PERL
407 
408 =pod
409 
410 =head2 _ARRAY0 $value
411 
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
415 elements.
416 
417 For a more basic "An array of something" form that also requires at
418 least one element, see the C<_ARRAY> function.
419 
420 Returns the C<ARRAY> reference itself as a convenience, or C<undef>
421 if the value provided is not an C<ARRAY> reference.
422 
423 =cut
424 
425 eval <<'END_PERL' unless defined &_ARRAY0;
426 sub _ARRAY0 ($) {
427  ref $_[0] eq 'ARRAY' ? $_[0] : undef;
428 }
429 END_PERL
430 
431 =pod
432 
433 =head2 _ARRAYLIKE $value
434 
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>.
438 
439 =cut
440 
441 eval <<'END_PERL' unless defined &_ARRAYLIKE;
442 sub _ARRAYLIKE {
443  (defined $_[0] and ref $_[0] and (
444  (Scalar::Util::reftype($_[0]) eq 'ARRAY')
445  or
446  overload::Method($_[0], '@{}')
447  )) ? $_[0] : undef;
448 }
449 END_PERL
450 
451 =pod
452 
453 =head2 _HASH $value
454 
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.
458 
459 For a version of this function that allows the C<HASH> to be empty,
460 see the C<_HASH0> function.
461 
462 Returns the C<HASH> reference itself as a convenience, or C<undef>
463 if the value provided is not an C<HASH> reference.
464 
465 =cut
466 
467 eval <<'END_PERL' unless defined &_HASH;
468 sub _HASH ($) {
469  (ref $_[0] eq 'HASH' and scalar %{$_[0]}) ? $_[0] : undef;
470 }
471 END_PERL
472 
473 =pod
474 
475 =head2 _HASH0 $value
476 
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.
480 
481 For a simpler "A hash of something" version that requires at least one
482 element, see the C<_HASH> function.
483 
484 Returns the C<HASH> reference itself as a convenience, or C<undef>
485 if the value provided is not an C<HASH> reference.
486 
487 =cut
488 
489 eval <<'END_PERL' unless defined &_HASH0;
490 sub _HASH0 ($) {
491  ref $_[0] eq 'HASH' ? $_[0] : undef;
492 }
493 END_PERL
494 
495 =pod
496 
497 =head2 _HASHLIKE $value
498 
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>.
502 
503 =cut
504 
505 eval <<'END_PERL' unless defined &_HASHLIKE;
506 sub _HASHLIKE {
507  (defined $_[0] and ref $_[0] and (
508  (Scalar::Util::reftype($_[0]) eq 'HASH')
509  or
510  overload::Method($_[0], '%{}')
511  )) ? $_[0] : undef;
512 }
513 END_PERL
514 
515 =pod
516 
517 =head2 _CODE $value
518 
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
521 C<CODE> reference.
522 
523 Returns the C<CODE> reference itself as a convenience, or C<undef>
524 if the value provided is not an C<CODE> reference.
525 
526 =cut
527 
528 eval <<'END_PERL' unless defined &_CODE;
529 sub _CODE ($) {
530  ref $_[0] eq 'CODE' ? $_[0] : undef;
531 }
532 END_PERL
533 
534 =pod
535 
536 =head2 _CODELIKE $value
537 
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
541 overload C<'&{}'>.
542 
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.
545 
546 For example:
547 
548  sub foo {
549  my $code1 = _CODELIKE(shift) or die "No code param provided";
550  my $code2 = _CODELIKE(shift);
551  if ( $code2 ) {
552  print "Got optional second code param";
553  }
554  }
555 
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
558 context.
559 
560  # Always evaluate to true in boolean context
561  use overload 'bool' => sub () { 1 };
562 
563 Returns the callable value as a convenience, or C<undef> if the
564 value provided is not callable.
565 
566 Note - This function was formerly known as _CALLABLE but has been renamed
567 for greater symmetry with the other _XXXXLIKE functions.
568 
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.
571 
572 I apologise for any inconvenience caused.
573 
574 =cut
575 
576 eval <<'END_PERL' unless defined &_CODELIKE;
577 sub _CODELIKE($) {
578  (
579  (Scalar::Util::reftype($_[0])||'') eq 'CODE'
580  or
581  Scalar::Util::blessed($_[0]) and overload::Method($_[0],'&{}')
582  )
583  ? $_[0] : undef;
584 }
585 END_PERL
586 
587 =pod
588 
589 =head2 _INVOCANT $value
590 
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.
593 
594 If so, the value itself is returned. Otherwise, C<_INVOCANT>
595 returns C<undef>.
596 
597 =cut
598 
599 eval <<'END_PERL' unless defined &_INVOCANT;
600 sub _INVOCANT($) {
601  (defined $_[0] and
602  (defined Scalar::Util::blessed($_[0])
603  or
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]))
607  ) ? $_[0] : undef;
608 }
609 END_PERL
610 
611 =pod
612 
613 =head2 _INSTANCE $object, $class
614 
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.
618 
619 Returns the object itself as a convenience, or C<undef> if the value
620 provided is not an object of that type.
621 
622 =cut
623 
624 eval <<'END_PERL' unless defined &_INSTANCE;
625 sub _INSTANCE ($$) {
626  (Scalar::Util::blessed($_[0]) and $_[0]->isa($_[1])) ? $_[0] : undef;
627 }
628 END_PERL
629 
630 =head2 _INSTANCEDOES $object, $role
631 
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
635 implemented.
636 
637 =cut
638 
639 eval <<'END_PERL' unless defined &_INSTANCEDOES;
640 sub _INSTANCEDOES ($$) {
641  (Scalar::Util::blessed($_[0]) and $_[0]->DOES($_[1])) ? $_[0] : undef;
642 }
643 END_PERL
644 
645 =pod
646 
647 =head2 _REGEX $value
648 
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.
651 
652 Returns the value itself as a convenience, or C<undef> if the value
653 provided is not a regular expression.
654 
655 =cut
656 
657 eval <<'END_PERL' unless defined &_REGEX;
658 sub _REGEX ($) {
659  (defined $_[0] and 'Regexp' eq ref($_[0])) ? $_[0] : undef;
660 }
661 END_PERL
662 
663 =pod
664 
665 =head2 _SET \@array, $class
666 
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.
670 
671 The set is provided as a reference to an C<ARRAY> of objects of the
672 class provided.
673 
674 For an alternative function that allows zero-length sets, see the
675 C<_SET0> function.
676 
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.
679 
680 =cut
681 
682 eval <<'END_PERL' unless defined &_SET;
683 sub _SET ($$) {
684  my $set = shift;
685  _ARRAY($set) or return undef;
686  foreach my $item ( @$set ) {
687  _INSTANCE($item,$_[0]) or return undef;
688  }
689  $set;
690 }
691 END_PERL
692 
693 =pod
694 
695 =head2 _SET0 \@array, $class
696 
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.
700 
701 The set is provided as a reference to an C<ARRAY> of objects of the
702 class provided.
703 
704 For an alternative function that requires at least one object, see the
705 C<_SET> function.
706 
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.
709 
710 =cut
711 
712 eval <<'END_PERL' unless defined &_SET0;
713 sub _SET0 ($$) {
714  my $set = shift;
715  _ARRAY0($set) or return undef;
716  foreach my $item ( @$set ) {
717  _INSTANCE($item,$_[0]) or return undef;
718  }
719  $set;
720 }
721 END_PERL
722 
723 =pod
724 
725 =head2 _HANDLE
726 
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.
730 
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
733 anyway).
734 
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).
737 
738 =cut
739 
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;
745 sub _HANDLE {
746  my $it = shift;
747 
748  # It has to be defined, of course
749  unless ( defined $it ) {
750  return undef;
751  }
752 
753  # Normal globs are considered to be file handles
754  if ( ref $it eq 'GLOB' ) {
755  return $it;
756  }
757 
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') ) {
761  return $it;
762  }
763 
764  # There are no other non-object handles that we support
765  unless ( Scalar::Util::blessed($it) ) {
766  return undef;
767  }
768 
769  # Check for a common base classes for conventional IO::Handle object
770  if ( $it->isa('IO::Handle') ) {
771  return $it;
772  }
773 
774 
775  # Check for tied file handles using Tie::Handle
776  if ( $it->isa('Tie::Handle') ) {
777  return $it;
778  }
779 
780  # IO::Scalar is not a proper seekable, but it is valid is a
781  # regular file handle
782  if ( $it->isa('IO::Scalar') ) {
783  return $it;
784  }
785 
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') ) {
789  return $it;
790  }
791 
792  # This is not any sort of object we know about
793  return undef;
794 }
795 END_PERL
796 
797 =pod
798 
799 =head2 _DRIVER $string
800 
801  sub foo {
802  my $class = _DRIVER(shift, 'My::Driver::Base') or die "Bad driver";
803  ...
804  }
805 
806 The C<_DRIVER> function is intended to be imported into your
807 package, and provides a convenient way to load and validate
808 a driver class.
809 
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.
814 
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.
818 
819 =cut
820 
821 eval <<'END_PERL' unless defined &_DRIVER;
822 sub _DRIVER ($$) {
823  (defined _CLASS($_[0]) and eval "require $_[0];" and ! $@ and $_[0]->isa($_[1]) and $_[0] ne $_[1]) ? $_[0] : undef;
824 }
825 END_PERL
826 
827 1;
828 
829 =pod
830 
831 =head1 TO DO
832 
833 - Add _CAN to help resolve the UNIVERSAL::can debacle
834 
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. :/
837 
838 - Implement an assertion-like version of this module, that dies on
839 error.
840 
841 - Implement a Test:: version of this module, for use in testing
842 
843 =head1 SUPPORT
844 
845 Bugs should be reported via the CPAN bug tracker at
846 
847 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Params-Util>
848 
849 For other issues, contact the author.
850 
851 =head1 AUTHOR
852 
853 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
854 
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.
857 
858 =head1 SEE ALSO
859 
860 L<Params::Validate>
861 
862 =head1 COPYRIGHT
863 
864 Copyright 2005 - 2012 Adam Kennedy.
865 
866 This program is free software; you can redistribute
867 it and/or modify it under the same terms as Perl itself.
868 
869 The full text of the license can be found in the
870 LICENSE file included with this module.
871 
872 =cut