File Coverage

blib/lib/Data/Validate/Type.pm
Criterion Covered Total %
statement 110 110 100.0
branch 89 96 92.7
condition 49 56 87.5
subroutine 27 27 100.0
pod 21 21 100.0
total 296 310 95.4


line stmt bran cond sub pod time code
1             package Data::Validate::Type;
2              
3 13     13   348796 use warnings;
  13         33  
  13         481  
4 13     13   76 use strict;
  13         280  
  13         483  
5              
6 13     13   84 use base 'Exporter';
  13         28  
  13         1301  
7              
8 13     13   86 use Carp;
  13         27  
  13         889  
9 13     13   7282 use Data::Dump qw();
  13         63670  
  13         333  
10 13     13   120 use Scalar::Util qw();
  13         26  
  13         28843  
11              
12             my @boolean_functions_list = qw(
13             is_string
14             is_arrayref
15             is_hashref
16             is_coderef
17             is_number
18             is_instance
19             is_regex
20             );
21              
22             my @assertion_functions_list = qw(
23             assert_string
24             assert_arrayref
25             assert_hashref
26             assert_coderef
27             assert_number
28             assert_instance
29             assert_regex
30             );
31              
32             my @filtering_functions_list = qw(
33             filter_string
34             filter_arrayref
35             filter_hashref
36             filter_coderef
37             filter_number
38             filter_instance
39             filter_regex
40             );
41              
42             our @EXPORT_OK =
43             (
44             @boolean_functions_list,
45             @assertion_functions_list,
46             @filtering_functions_list,
47             );
48             our %EXPORT_TAGS =
49             (
50             boolean_tests => \@boolean_functions_list,
51             assertions => \@assertion_functions_list,
52             filters => \@filtering_functions_list,
53             all =>
54             [
55             @boolean_functions_list,
56             @assertion_functions_list,
57             @filtering_functions_list,
58             ],
59             );
60              
61              
62             =head1 NAME
63              
64             Data::Validate::Type - Data type validation functions.
65              
66              
67             =head1 VERSION
68              
69             Version 1.5.1
70              
71             =cut
72              
73             our $VERSION = '1.5.1';
74              
75              
76             =head1 SYNOPSIS
77              
78             # Call with explicit package name.
79             use Data::Validate::Type;
80             if ( Data::Validate::Type::is_string( 'test' ) )
81             {
82             # ...
83             }
84              
85             # Import specific functions.
86             use Data::Validate::Type qw( is_string );
87             if ( is_string( 'test' ) )
88             {
89             # ...
90             }
91              
92             # Import functions for a given paradigm.
93             use Data::Validate::Type qw( :boolean_tests );
94             if ( is_string( 'test' ) )
95             {
96             # ...
97             }
98              
99              
100             =head1 DESCRIPTION
101              
102             L is a wonderful module, but suffers from a few drawbacks:
103              
104             =over 4
105              
106             =item * Function names start with an underscore, which is usually used to
107             indicate private functions.
108              
109             =item * Function names are uppercase, which is usually used to indicate file
110             handles or constants.
111              
112             =item * Function names don't pass PerlCritic's validation, making them
113             problematic to import.
114              
115             =item * Functions use by default the convention that collection that collections
116             need to not be empty to be valid (see _ARRAY0/_ARRAY for example), which is
117             counter-intuitive.
118              
119             =item * In Pure Perl mode, the functions are created via eval, which causes
120             issues for L in taint mode.
121              
122             =back
123              
124             Those drawbacks are purely cosmetic and don't affect the usefulness of the
125             functions, except for the last one. This module used to encapsulate
126             L, but I had to refactor it out to fix the issues with
127             L.
128              
129             Please note that I prefer long function names that are descriptive, to arcane
130             short ones. This increases readability, and the bulk of the typing can be
131             spared with the use of a good IDE like Padre.
132              
133             Also, this is work in progress - There is more functions that should be added
134             here, if you need one in particular feel free to contact me.
135              
136              
137             =head1 BOOLEAN TEST FUNCTIONS
138              
139             Functions in this group return a boolean to indicate whether the parameters
140             passed match the test(s) specified by the functions or not.
141              
142             All the boolean functions can be imported at once in your namespace with the
143             following line:
144              
145             use Data::Validate::Type qw( :boolean_tests );
146              
147              
148             =head2 is_string()
149              
150             Return a boolean indicating if the variable passed is a string.
151              
152             my $is_string = Data::Validate::Type::is_string( $variable );
153              
154             Note: 0 and '' (empty string) are valid strings.
155              
156             Parameters:
157              
158             =over 4
159              
160             =item * allow_empty
161              
162             Boolean, default 1. Allow the string to be empty or not.
163              
164             =back
165              
166             =cut
167              
168             sub is_string
169             {
170 198     198 1 62294 my ( $variable, %args ) = @_;
171              
172             # Check parameters.
173 198         401 my $allow_empty = delete( $args{'allow_empty'} );
174 198 100       543 $allow_empty = 1 unless defined( $allow_empty );
175 198 50       540 croak 'Arguments not recognized: ' . Data::Dump::dump( %args )
176             unless scalar( keys %args ) == 0;
177              
178             # Check variable.
179 198 100 100     1993 return 0 if !defined( $variable ) || ref( $variable );
180              
181             # Check length if we don't allow empty strings.
182 72 100 100     348 return 0 if !$allow_empty && length( $variable ) == 0;
183              
184 69         300 return 1;
185             }
186              
187              
188             =head2 is_arrayref()
189              
190             Return a boolean indicating if the variable passed is an arrayref that can be
191             dereferenced into an array.
192              
193             my $is_arrayref = Data::Validate::Type::is_arrayref( $variable );
194              
195             my $is_arrayref = Data::Validate::Type::is_arrayref(
196             $variable,
197             allow_empty => 1,
198             no_blessing => 0,
199             );
200              
201             # Check if the variable is an arrayref of hashrefs.
202             my $is_arrayref = Data::Validate::Type::is_arrayref(
203             $variable,
204             allow_empty => 1,
205             no_blessing => 0,
206             element_validate_type =>
207             sub
208             {
209             return Data::Validate::Type::is_hashref( $_[0] );
210             },
211             );
212              
213             Parameters:
214              
215             =over 4
216              
217             =item * allow_empty
218              
219             Boolean, default 1. Allow the array to be empty or not.
220              
221             =item * no_blessing
222              
223             Boolean, default 0. Require that the variable is not blessed.
224              
225             =item * element_validate_type
226              
227             None by default. Set it to a coderef to validate the elements in the array.
228             The coderef will be passed the element to validate as first parameter, and it
229             must return a boolean indicating whether the element was valid or not.
230              
231             =back
232              
233             =cut
234              
235             sub is_arrayref
236             {
237 396     396 1 151725 my ( $variable, %args ) = @_;
238              
239             # Check parameters.
240 396         786 my $allow_empty = delete( $args{'allow_empty'} );
241 396 100       1167 $allow_empty = 1 unless defined( $allow_empty );
242 396   100     1666 my $no_blessing = delete( $args{'no_blessing'} ) || 0;
243 396         643 my $element_validate_type = delete( $args{'element_validate_type'} );
244 396 50 66     1143 croak '"element_validate_type" must be a coderef'
245             if defined( $element_validate_type ) && !is_coderef( $element_validate_type );
246 396 50       945 croak 'Arguments not recognized: ' . Data::Dump::dump( %args )
247             unless scalar( keys %args ) == 0;
248              
249             # Check variable.
250 396 100 66     2631 return 0 if !defined( $variable ) || !ref( $variable );
251              
252 234 100       548 if ( $no_blessing )
253             {
254             # The variable must be a standard arrayref.
255 39 100       288 return 0 if ref( $variable ) ne 'ARRAY';
256             }
257             else
258             {
259             # Check that the variable is either an array or allows
260             # dereferencing as one.
261 195 100 66     1142 return 0 if !
262             (
263             ( Scalar::Util::reftype( $variable ) eq 'ARRAY' )
264             || overload::Method( $variable, '@{}' )
265             );
266             }
267              
268             # Check size of the array if we require a non-empty array.
269 87 100 100     329 return 0 if !$allow_empty && scalar( @$variable ) == 0;
270              
271             # If we have an element validator specified, now that we know that we have
272             # an array, it's a good time to test the individual elements.
273 84 100       285 if ( defined( $element_validate_type ) )
274             {
275 15         40 foreach my $element ( @$variable )
276             {
277 24 100       69 return 0 if !$element_validate_type->( $element );
278             }
279             }
280              
281 75         359 return 1;
282             }
283              
284              
285             =head2 is_hashref()
286              
287             Return a boolean indicating if the variable passed is a hashref that can be
288             dereferenced into a hash.
289              
290             my $is_hashref = Data::Validate::Type::is_hashref( $variable );
291              
292             my $is_hashref = Data::Validate::Type::is_hashref(
293             $variable,
294             allow_empty => 1,
295             no_blessing => 0,
296             );
297              
298             Parameters:
299              
300             =over 4
301              
302             =item * allow_empty
303              
304             Boolean, default 1. Allow the array to be empty or not.
305              
306             =item * no_blessing
307              
308             Boolean, default 0. Require that the variable is not blessed.
309              
310             =back
311              
312             =cut
313              
314             sub is_hashref
315             {
316 354     354 1 97648 my ( $variable, %args ) = @_;
317              
318             # Check parameters.
319 354         618 my $allow_empty = delete( $args{'allow_empty'} );
320 354 100       986 $allow_empty = 1 unless defined( $allow_empty );
321 354   100     1508 my $no_blessing = delete( $args{'no_blessing'} ) || 0;
322 354 50       1089 croak 'Arguments not recognized: ' . Data::Dump::dump( %args )
323             unless scalar( keys %args ) == 0;
324              
325             # Check variable.
326 354 100 66     2406 return 0 if !defined( $variable ) || !ref( $variable );
327              
328 213 100       434 if ( $no_blessing )
329             {
330             # The variable must be a standard hashref.
331 39 100       378 return 0 if ref( $variable ) ne 'HASH';
332             }
333             else
334             {
335             # Check that the variable is either a hashref or allows dereferencing
336             # as one.
337 174 100 66     945 return 0 if !
338             (
339             ( Scalar::Util::reftype( $variable ) eq 'HASH' )
340             || overload::Method( $variable, '%{}' )
341             );
342             }
343              
344             # If we don't allow empty hashes, check keys.
345 57 100 100     293 return 0 if !$allow_empty && scalar( keys %$variable ) == 0;
346              
347 54         227 return 1;
348             }
349              
350              
351             =head2 is_coderef()
352              
353             Return a boolean indicating if the variable passed is an coderef that can be
354             dereferenced into a block of code.
355              
356             my $is_coderef = Data::Validate::Type::is_coderef( $variable );
357              
358             =cut
359              
360             sub is_coderef
361             {
362 132     132 1 17339 my ( $variable, %args ) = @_;
363              
364             # Check parameters.
365 132 50       366 croak 'Arguments not recognized: ' . Data::Dump::dump( %args )
366             unless scalar( keys %args ) == 0;
367              
368             # Check variable.
369 132 100 66     744 return 0 if !defined( $variable ) || !ref( $variable );
370 105 100       509 return 0 if ref( $variable ) ne 'CODE';
371              
372 69         311 return 1;
373             }
374              
375              
376             =head2 is_number()
377              
378             Return a boolean indicating if the variable passed is a number.
379              
380             my $is_number = Data::Validate::Type::is_number( $variable );
381             my $is_number = Data::Validate::Type::is_number(
382             $variable,
383             positive => 1,
384             );
385             my $is_number = Data::Validate::Type::is_number(
386             $variable,
387             strictly_positive => 1,
388             );
389              
390             Parameters:
391              
392             =over 4
393              
394             =item * strictly_positive
395              
396             Boolean, default 0. Set to 1 to check for a strictly positive number.
397              
398             =item * positive
399              
400             Boolean, default 0. Set to 1 to check for a positive number.
401              
402             =back
403              
404             =cut
405              
406             sub is_number
407             {
408 330     330 1 94953 my ( $variable, %args ) = @_;
409              
410             # Check parameters.
411 330   100     1410 my $positive = delete( $args{'positive'} ) || 0;
412 330   100     1210 my $strictly_positive = delete( $args{'strictly_positive'} ) || 0;
413 330 50       791 croak 'Arguments not recognized: ' . Data::Dump::dump( %args )
414             unless scalar( keys %args ) == 0;
415              
416             # Check variable.
417 330 100 100     2813 return 0 if !defined( $variable ) || ref( $variable );
418              
419             # Requires Scalar::Util v1.18 or higher.
420 120 100       621 return 0 if !Scalar::Util::looks_like_number( $variable );
421              
422             # Check extra restrictions.
423 90 100 100     298 return 0 if $positive && $variable < 0;
424 84 100 100     293 return 0 if $strictly_positive && $variable <= 0;
425              
426 75         313 return 1;
427             }
428              
429              
430             =head2 is_instance()
431              
432             Return a boolean indicating if the variable is an instance of the given class.
433              
434             Note that this handles inheritance properly, so it will return true if the
435             variable is an instance of a subclass of the class given.
436              
437             my $is_instance = Data::Validate::Type::is_instance(
438             $variable,
439             class => $class,
440             );
441              
442             Parameters:
443              
444             =over 4
445              
446             =item * class
447              
448             Required, the name of the class to check the variable against.
449              
450             =back
451              
452             =cut
453              
454             sub is_instance
455             {
456 202     202 1 55210 my ( $variable, %args ) = @_;
457              
458             # Check parameters.
459 202         428 my $class = delete( $args{'class'} );
460 202 100 100     1231 croak 'A class argument is required'
461             if !defined( $class ) || $class eq '';
462 200 50       476 croak 'Arguments not recognized: ' . Data::Dump::dump( %args )
463             unless scalar( keys %args ) == 0;
464              
465             # Check variable.
466 200 100 66     2065 return 0 if !defined( $variable ) || !Scalar::Util::blessed( $variable );
467              
468             # Test that the object is a member if the class.
469 38 100       547 return 0 if !$variable->isa( $class );
470              
471 8         41 return 1;
472             }
473              
474              
475             =head2 is_regex()
476              
477             Return a boolean indicating if the variable is a regular expression.
478              
479             my $is_regex = Data::Validate::Type::is_regex( $variable );
480              
481             =cut
482              
483             sub is_regex
484             {
485 66     66 1 17209 my ( $variable ) = @_;
486              
487             # Check variable.
488 66 100 100     805 return defined( $variable ) && ( ref( $variable ) eq 'Regexp' )
489             ? 1
490             : 0;
491             }
492              
493              
494             =head1 ASSERTION-BASED FUNCTIONS
495              
496             Functions in this group do not return anything, but will die when the parameters
497             passed don't match the test(s) specified by the functions.
498              
499             All the assertion test functions can be imported at once in your namespace with
500             the following line:
501              
502             use Data::Validate::Type qw( :assertions );
503              
504              
505             =head2 assert_string()
506              
507             Die unless the variable passed is a string.
508              
509             Data::Validate::Type::assert_string( $variable );
510              
511             Note: 0 and '' (empty string) are valid strings.
512              
513             Parameters:
514              
515             =over 4
516              
517             =item * allow_empty
518              
519             Boolean, default 1. Allow the string to be empty or not.
520              
521             =back
522              
523             =cut
524              
525             sub assert_string
526             {
527 66     66 1 76515 my ( $variable, %args ) = @_;
528              
529 66 100       165 croak 'Not a string'
530             unless is_string( $variable, %args );
531              
532 23         121 return;
533             }
534              
535              
536             =head2 assert_arrayref()
537              
538             Die unless the variable passed is an arrayref that can be dereferenced into an
539             array.
540              
541             Data::Validate::Type::assert_arrayref( $variable );
542              
543             Data::Validate::Type::assert_arrayref(
544             $variable,
545             allow_empty => 1,
546             no_blessing => 0,
547             );
548              
549             # Require the variable to be an arrayref of hashrefs.
550             Data::Validate::Type::assert_arrayref(
551             $variable,
552             allow_empty => 1,
553             no_blessing => 0,
554             element_validate_type =>
555             sub
556             {
557             return Data::Validate::Type::is_hashref( $_[0] );
558             },
559             );
560              
561             Parameters:
562              
563             =over 4
564              
565             =item * allow_empty
566              
567             Boolean, default 1. Allow the array to be empty or not.
568              
569             =item * no_blessing
570              
571             Boolean, default 0. Require that the variable is not blessed.
572              
573             =item * element_validate_type
574              
575             None by default. Set it to a coderef to validate the elements in the array.
576             The coderef will be passed the element to validate as first parameter, and it
577             must return a boolean indicating whether the element was valid or not.
578              
579             =back
580              
581             =cut
582              
583             sub assert_arrayref
584             {
585 132     132 1 151779 my ( $variable, %args ) = @_;
586              
587 132 100       335 croak 'Not an arrayref'
588             unless is_arrayref( $variable, %args );
589              
590 25         166 return;
591             }
592              
593              
594             =head2 assert_hashref()
595              
596             Die unless the variable passed is a hashref that can be dereferenced into a hash.
597              
598             Data::Validate::Type::assert_hashref( $variable );
599              
600             Data::Validate::Type::assert_hashref(
601             $variable,
602             allow_empty => 1,
603             no_blessing => 0,
604             );
605              
606             Parameters:
607              
608             =over 4
609              
610             =item * allow_empty
611              
612             Boolean, default 1. Allow the array to be empty or not.
613              
614             =item * no_blessing
615              
616             Boolean, default 0. Require that the variable is not blessed.
617              
618             =back
619              
620             =cut
621              
622             sub assert_hashref
623             {
624 110     110 1 119864 my ( $variable, %args ) = @_;
625              
626 110 100       253 croak 'Not a hashref'
627             unless is_hashref( $variable, %args );
628              
629 13         66 return;
630             }
631              
632              
633             =head2 assert_coderef()
634              
635             Die unless the variable passed is an coderef that can be dereferenced into a
636             block of code.
637              
638             Data::Validate::Type::assert_coderef( $variable );
639              
640             =cut
641              
642             sub assert_coderef
643             {
644 22     22 1 29272 my ( $variable, %args ) = @_;
645              
646 22 100       56 croak 'Not a coderef'
647             unless is_coderef( $variable, %args );
648              
649 1         5 return;
650             }
651              
652              
653             =head2 assert_number()
654              
655             Die unless the variable passed is a number.
656              
657             Data::Validate::Type::assert_number( $variable );
658             Data::Validate::Type::assert_number(
659             $variable,
660             positive => 1,
661             );
662             Data::Validate::Type::assert_number(
663             $variable,
664             strictly_positive => 1,
665             );
666              
667             Parameters:
668              
669             =over 4
670              
671             =item * strictly_positive
672              
673             Boolean, default 0. Set to 1 to check for a strictly positive number.
674              
675             =item * positive
676              
677             Boolean, default 0. Set to 1 to check for a positive number.
678              
679             =back
680              
681             =cut
682              
683             sub assert_number
684             {
685 110     110 1 109188 my ( $variable, %args ) = @_;
686              
687 110 100       257 croak 'Not a number'
688             unless is_number( $variable, %args );
689              
690 25         128 return;
691             }
692              
693              
694             =head2 assert_instance()
695              
696             Die unless the variable is an instance of the given class.
697              
698             Note that this handles inheritance properly, so it will not die if the
699             variable is an instance of a subclass of the class given.
700              
701             Data::Validate::Type::assert_instance(
702             $variable,
703             class => $class,
704             );
705              
706             Parameters:
707              
708             =over 4
709              
710             =item * class
711              
712             Required, the name of the class to check the variable against.
713              
714             =back
715              
716             =cut
717              
718             sub assert_instance
719             {
720 66     66 1 95248 my ( $variable, %args ) = @_;
721              
722 66 100       173 croak 'Not an instance of the class'
723             unless is_instance( $variable, %args );
724              
725 2         12 return;
726             }
727              
728              
729             =head2 assert_regex()
730              
731             Die unless the variable is a regular expression.
732              
733             Data::Validate::Type::assert_regex( $variable );
734              
735             =cut
736              
737             sub assert_regex
738             {
739 22     22 1 29418 my ( $variable ) = @_;
740              
741 22 100       56 croak 'Not a regular expression'
742             unless is_regex( $variable );
743              
744 2         12 return;
745             }
746              
747              
748             =head1 FILTERING FUNCTIONS
749              
750             Functions in this group return the variable tested against when it matches the
751             test(s) specified by the functions.
752              
753             All the filtering functions can be imported at once in your namespace with the
754             following line:
755              
756             use Data::Validate::Type qw( :filters );
757              
758              
759             =head2 filter_string()
760              
761             Return the variable passed if it is a string, otherwise return undef.
762              
763             Data::Validate::Type::filter_string( $variable );
764              
765             Note: 0 and '' (empty string) are valid strings.
766              
767             Parameters:
768              
769             =over 4
770              
771             =item * allow_empty
772              
773             Boolean, default 1. Allow the string to be empty or not.
774              
775             =back
776              
777             =cut
778              
779             sub filter_string
780             {
781 66     66 1 86217 my ( $variable, %args ) = @_;
782              
783 66 100       182 return is_string( $variable, %args )
784             ? $variable
785             : undef;
786             }
787              
788              
789             =head2 filter_arrayref()
790              
791             Return the variable passed if it is an arrayref that can be dereferenced into an
792             array, otherwise undef.
793              
794             Data::Validate::Type::filter_arrayref( $variable );
795              
796             Data::Validate::Type::filter_arrayref(
797             $variable,
798             allow_empty => 1,
799             no_blessing => 0,
800             );
801              
802             # Only return the variable if it is an arrayref of hashrefs.
803             Data::Validate::Type::filter_arrayref(
804             $variable,
805             allow_empty => 1,
806             no_blessing => 0,
807             element_validate_type =>
808             sub
809             {
810             return Data::Validate::Type::is_hashref( $_[0] );
811             },
812             );
813              
814             Parameters:
815              
816             =over 4
817              
818             =item * allow_empty
819              
820             Boolean, default 1. Allow the array to be empty or not.
821              
822             =item * no_blessing
823              
824             Boolean, default 0. Require that the variable is not blessed.
825              
826             =item * element_validate_type
827              
828             None by default. Set it to a coderef to validate the elements in the array.
829             The coderef will be passed the element to validate as first parameter, and it
830             must return a boolean indicating whether the element was valid or not.
831              
832             =back
833              
834             =cut
835              
836             sub filter_arrayref
837             {
838 132     132 1 109507 my ( $variable, %args ) = @_;
839              
840 132 100       341 return is_arrayref( $variable, %args )
841             ? $variable
842             : undef;
843             }
844              
845              
846             =head2 filter_hashref()
847              
848             Return the variable passed if it is a hashref that can be dereferenced into a
849             hash, otherwise return undef.
850              
851             Data::Validate::Type::filter_hashref( $variable );
852              
853             Data::Validate::Type::filter_hashref(
854             $variable,
855             allow_empty => 1,
856             no_blessing => 0,
857             );
858              
859             Parameters:
860              
861             =over 4
862              
863             =item * allow_empty
864              
865             Boolean, default 1. Allow the array to be empty or not.
866              
867             =item * no_blessing
868              
869             Boolean, default 0. Require that the variable is not blessed.
870              
871             =back
872              
873             =cut
874              
875             sub filter_hashref
876             {
877 110     110 1 69098 my ( $variable, %args ) = @_;
878              
879 110 100       267 return is_hashref( $variable, %args )
880             ? $variable
881             : undef;
882             }
883              
884              
885             =head2 filter_coderef()
886              
887             Return the variable passed if it is a coderef that can be dereferenced into a
888             block of code, otherwise return undef.
889              
890             Data::Validate::Type::filter_coderef( $variable );
891              
892             =cut
893              
894             sub filter_coderef
895             {
896 22     22 1 28949 my ( $variable, %args ) = @_;
897              
898 22 100       60 return is_coderef( $variable, %args )
899             ? $variable
900             : undef;
901             }
902              
903              
904             =head2 filter_number()
905              
906             Return the variable passed if it is a number, otherwise return undef.
907              
908             Data::Validate::Type::filter_number( $variable );
909             Data::Validate::Type::filter_number(
910             $variable,
911             positive => 1,
912             );
913             Data::Validate::Type::filter_number(
914             $variable,
915             strictly_positive => 1,
916             );
917              
918             Parameters:
919              
920             =over 4
921              
922             =item * strictly_positive
923              
924             Boolean, default 0. Set to 1 to check for a strictly positive number.
925              
926             =item * positive
927              
928             Boolean, default 0. Set to 1 to check for a positive number.
929              
930             =back
931              
932             =cut
933              
934             sub filter_number
935             {
936 110     110 1 86171 my ( $variable, %args ) = @_;
937              
938 110 100       268 return is_number( $variable, %args )
939             ? $variable
940             : undef;
941             }
942              
943              
944             =head2 filter_instance()
945              
946             Return the variable passed if it is an instance of the given class.
947              
948             Note that this handles inheritance properly, so it will return the variable if
949             it is an instance of a subclass of the class given.
950              
951             Data::Validate::Type::filter_instance(
952             $variable,
953             class => $class,
954             );
955              
956             Parameters:
957              
958             =over 4
959              
960             =item * class
961              
962             Required, the name of the class to check the variable against.
963              
964             =back
965              
966             =cut
967              
968             sub filter_instance
969             {
970 66     66 1 57651 my ( $variable, %args ) = @_;
971              
972 66 100       184 return is_instance( $variable, %args )
973             ? $variable
974             : undef;
975             }
976              
977              
978             =head2 filter_regex()
979              
980             Return the variable passed if it is a regular expression.
981              
982             Data::Validate::Type::filter_regex( $variable );
983              
984             =cut
985              
986             sub filter_regex
987             {
988 22     22 1 26264 my ( $variable ) = @_;
989              
990 22 100       58 return is_regex( $variable )
991             ? $variable
992             : undef;
993             }
994              
995              
996             =head1 BUGS
997              
998             Please report any bugs or feature requests through the web interface at
999             L.
1000             I will be notified, and then you'll automatically be notified of progress on
1001             your bug as I make changes.
1002              
1003              
1004             =head1 SUPPORT
1005              
1006             You can find documentation for this module with the perldoc command.
1007              
1008             perldoc Data::Validate::Type
1009              
1010              
1011             You can also look for information at:
1012              
1013             =over 4
1014              
1015             =item * GitHub (report bugs there)
1016              
1017             L
1018              
1019             =item * AnnoCPAN: Annotated CPAN documentation
1020              
1021             L
1022              
1023             =item * CPAN Ratings
1024              
1025             L
1026              
1027             =item * MetaCPAN
1028              
1029             L
1030              
1031             =back
1032              
1033              
1034             =head1 AUTHOR
1035              
1036             L,
1037             C<< >>.
1038              
1039              
1040             =head1 ACKNOWLEDGEMENTS
1041              
1042             Thanks to Adam Kennedy for writing L. This module started as an
1043             encapsulation for Params::Util and I learnt quite a bit from it.
1044              
1045              
1046             =head1 COPYRIGHT & LICENSE
1047              
1048             Copyright 2012-2014 Guillaume Aubert.
1049              
1050             This program is free software: you can redistribute it and/or modify it under
1051             the terms of the GNU General Public License version 3 as published by the Free
1052             Software Foundation.
1053              
1054             This program is distributed in the hope that it will be useful, but WITHOUT ANY
1055             WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
1056             PARTICULAR PURPOSE. See the GNU General Public License for more details.
1057              
1058             You should have received a copy of the GNU General Public License along with
1059             this program. If not, see http://www.gnu.org/licenses/
1060              
1061             =cut
1062              
1063             1;