| 1 | =head1 NAME | ||||||||||
| 2 | |||||||||||
| 3 | Params::Classify - argument type classification | ||||||||||
| 4 | |||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||
| 6 | |||||||||||
| 7 | use Params::Classify qw( | ||||||||||
| 8 | scalar_class | ||||||||||
| 9 | is_undef check_undef | ||||||||||
| 10 | is_string check_string | ||||||||||
| 11 | is_number check_number | ||||||||||
| 12 | is_glob check_glob | ||||||||||
| 13 | is_regexp check_regexp | ||||||||||
| 14 | is_ref check_ref ref_type | ||||||||||
| 15 | is_blessed check_blessed blessed_class | ||||||||||
| 16 | is_strictly_blessed check_strictly_blessed | ||||||||||
| 17 | is_able check_able); | ||||||||||
| 18 | |||||||||||
| 19 | $c = scalar_class($arg); | ||||||||||
| 20 | |||||||||||
| 21 | if(is_undef($arg)) { | ||||||||||
| 22 | check_undef($arg); | ||||||||||
| 23 | |||||||||||
| 24 | if(is_string($arg)) { | ||||||||||
| 25 | check_string($arg); | ||||||||||
| 26 | if(is_number($arg)) { | ||||||||||
| 27 | check_number($arg); | ||||||||||
| 28 | |||||||||||
| 29 | if(is_glob($arg)) { | ||||||||||
| 30 | check_glob($arg); | ||||||||||
| 31 | if(is_regexp($arg)) { | ||||||||||
| 32 | check_regexp($arg); | ||||||||||
| 33 | |||||||||||
| 34 | if(is_ref($arg)) { | ||||||||||
| 35 | check_ref($arg); | ||||||||||
| 36 | $t = ref_type($arg); | ||||||||||
| 37 | if(is_ref($arg, "HASH")) { | ||||||||||
| 38 | check_ref($arg, "HASH"); | ||||||||||
| 39 | |||||||||||
| 40 | if(is_blessed($arg)) { | ||||||||||
| 41 | check_blessed($arg); | ||||||||||
| 42 | if(is_blessed($arg, "IO::Handle")) { | ||||||||||
| 43 | check_blessed($arg, "IO::Handle"); | ||||||||||
| 44 | $c = blessed_class($arg); | ||||||||||
| 45 | if(is_strictly_blessed($arg, "IO::Pipe::End")) { | ||||||||||
| 46 | check_strictly_blessed($arg, "IO::Pipe::End"); | ||||||||||
| 47 | if(is_able($arg, ["print", "flush"])) { | ||||||||||
| 48 | check_able($arg, ["print", "flush"]); | ||||||||||
| 49 | |||||||||||
| 50 | =head1 DESCRIPTION | ||||||||||
| 51 | |||||||||||
| 52 | This module provides various type-testing functions. These are intended | ||||||||||
| 53 | for functions that, unlike most Perl code, care what type of data they | ||||||||||
| 54 | are operating on. For example, some functions wish to behave differently | ||||||||||
| 55 | depending on the type of their arguments (like overloaded functions | ||||||||||
| 56 | in C++). | ||||||||||
| 57 | |||||||||||
| 58 | There are two flavours of function in this module. Functions of the first | ||||||||||
| 59 | flavour only provide type classification, to allow code to discriminate | ||||||||||
| 60 | between argument types. Functions of the second flavour package up the | ||||||||||
| 61 | most common type of type discrimination: checking that an argument is | ||||||||||
| 62 | of an expected type. The functions come in matched pairs, of the two | ||||||||||
| 63 | flavours, and so the type enforcement functions handle only the simplest | ||||||||||
| 64 | requirements for arguments of the types handled by the classification | ||||||||||
| 65 | functions. Enforcement of more complex types may, of course, be built | ||||||||||
| 66 | using the classification functions, or it may be more convenient to use | ||||||||||
| 67 | a module designed for the more complex job, such as L<Params::Validate>. | ||||||||||
| 68 | |||||||||||
| 69 | This module is implemented in XS, with a pure Perl backup version for | ||||||||||
| 70 | systems that can't handle XS. | ||||||||||
| 71 | |||||||||||
| 72 | =cut | ||||||||||
| 73 | |||||||||||
| 74 | package Params::Classify; | ||||||||||
| 75 | |||||||||||
| 76 | 12 | ▶ | { use 5.006001; } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 77 | 12 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 78 | 12 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 79 | |||||||||||
| 80 | 28 | our $VERSION = "0.015"; | |||||||||
| 81 | |||||||||||
| 82 | 12 | ▶ | use parent "Exporter"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 83 | 20 | our @EXPORT_OK = qw( | |||||||||
| 84 | scalar_class | ||||||||||
| 85 | is_undef check_undef | ||||||||||
| 86 | is_string check_string | ||||||||||
| 87 | is_number check_number | ||||||||||
| 88 | is_glob check_glob | ||||||||||
| 89 | is_regexp check_regexp | ||||||||||
| 90 | is_ref check_ref ref_type | ||||||||||
| 91 | is_blessed check_blessed blessed_class | ||||||||||
| 92 | is_strictly_blessed check_strictly_blessed | ||||||||||
| 93 | is_able check_able | ||||||||||
| 94 | ); | ||||||||||
| 95 | |||||||||||
| 96 | 10 | eval { local $SIG{__DIE__}; | |||||||||
| 97 | 112 | require Devel::CallChecker; | |||||||||
| 98 | 48 | Devel::CallChecker->VERSION(0.003); | |||||||||
| 99 | }; | ||||||||||
| 100 | 3270 | eval { local $SIG{__DIE__}; | |||||||||
| 101 | 112 | require XSLoader; | |||||||||
| 102 | 48 | XSLoader::load(__PACKAGE__, $VERSION); | |||||||||
| 103 | }; | ||||||||||
| 104 | |||||||||||
| 105 | 112 | ▶ | if($@ eq "") { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | 48 | close(DATA); | |||||||||
| 107 | } else { | ||||||||||
| 108 | 24 | (my $filename = __FILE__) =~ tr# -~##cd; | |||||||||
| 109 | 24 | local $/ = undef; | |||||||||
| 110 | 14 | my $pp_code = "#line 137 \"$filename\"\n".<DATA>; | |||||||||
| 111 | 14 | close(DATA); | |||||||||
| 112 | { | ||||||||||
| 113 | 14 | local $SIG{__DIE__}; | |||||||||
| 114 | 1728 | eval $pp_code; | |||||||||
| 115 | } | ||||||||||
| 116 | 1656 | ▶ | die $@ if $@ ne ""; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | sub is_string($); | ||||||||||
| 120 | 212 | ▶ | sub is_number($) { | ||||||||
|
Subroutine
is_number: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 121 | 212 | ▶ | return 0 unless &is_string; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 72 | my $warned; | |||||||||
| 123 | 72 | ▶ | local $SIG{__WARN__} = sub { $warned = 1; }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 124 | 72 | my $arg = $_[0]; | |||||||||
| 125 | 12 | ▶ | { no warnings "void"; 0 + $arg; } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 126 | 72 | return !$warned; | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | 84 | ▶ | sub check_number($) { | ||||||||
|
Subroutine
check_number: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 130 | 84 | ▶ | die "argument is not a number\n" unless &is_number; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | 1744 | 1; | |||||||||
| 134 | |||||||||||
| 135 | __DATA__ | ||||||||||