File Coverage

blib/lib/Params/Util/PP.pm
Criterion Covered Total %
statement 65 71 91.5
branch 67 72 93.0
condition 58 62 93.5
subroutine 31 32 96.8
pod n/a
total 221 237 93.2


line stmt bran cond sub pod time code
1             package Params::Util::PP;
2              
3 18     18   128 use strict;
  18         42  
  18         642  
4 18     18   86 use warnings;
  18         31  
  18         1645  
5              
6             our $VERSION = '1.103_01';
7             $VERSION =~ tr/_//d;
8              
9             =pod
10              
11             =head1 NAME
12              
13             Params::Util::PP - PurePerl Params::Util routines
14              
15             =cut
16              
17 18     18   116 use Scalar::Util ();
  18         33  
  18         538  
18 18     18   8835 use overload ();
  18         27573  
  18         48685  
19              
20             Scalar::Util->can("looks_like_number") and Scalar::Util->import("looks_like_number");
21             # Use a private pure-perl copy of looks_like_number if the version of
22             # Scalar::Util is old (for whatever reason).
23             Params::Util::PP->can("looks_like_number") or *looks_like_number = sub {
24             local $_ = shift;
25              
26             # checks from perlfaq4
27             return 0 if !defined($_);
28             if (ref($_))
29             {
30             return overload::Overloaded($_) ? defined(0 + $_) : 0;
31             }
32             return 1 if (/^[+-]?[0-9]+$/); # is a +/- integer
33             ## no critic (RegularExpressions::ProhibitComplexRegexes)
34             return 1 if (/^(?:[+-]?)(?=[0-9]|\.[0-9])[0-9]*(?:\.[0-9]*)?(?:[Ee](?:[+-]?[0-9]+))?$/); # a C float
35             return 1 if ($] >= 5.008 and /^(?:Inf(?:inity)?|NaN)$/i) or ($] >= 5.006001 and /^Inf$/i);
36              
37             0;
38             };
39              
40             ## no critic (Subroutines::ProhibitSubroutinePrototypes, Subroutines::RequireArgUnpacking)
41             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
42              
43 0     0   0 sub _XScompiled { return 0; }
44              
45             sub _STRING ($)
46             {
47 50     50   247359 my $arg = $_[0];
48 50 100 100     549 return (defined $arg and not ref $arg and length($arg)) ? $arg : undef;
49             }
50              
51             sub _IDENTIFIER ($)
52             {
53 76     76   326160 my $arg = $_[0];
54 76 100 100     1110 return (defined $arg and not ref $arg and $arg =~ m/^[^\W\d]\w*\z/s) ? $arg : undef;
55             }
56              
57             sub _CLASS ($)
58             {
59 152     152   37573 my $arg = $_[0];
60 152 100 100     3453 return (defined $arg and not ref $arg and $arg =~ m/^[^\W\d]\w*(?:::\w+)*\z/s) ? $arg : undef;
61             }
62              
63             sub _CLASSISA ($$)
64             {
65 52 100 100 52   434232 return (defined $_[0] and not ref $_[0] and $_[0] =~ m/^[^\W\d]\w*(?:::\w+)*\z/s and $_[0]->isa($_[1])) ? $_[0] : undef;
66             }
67              
68             sub _CLASSDOES ($$)
69             {
70 8 100 66 8   279 return (defined $_[0] and not ref $_[0] and $_[0] =~ m/^[^\W\d]\w*(?:::\w+)*\z/s and $_[0]->DOES($_[1])) ? $_[0] : undef;
71             }
72              
73             sub _SUBCLASS ($$)
74             {
75 52 100 100 52   1011 return (defined $_[0] and not ref $_[0] and $_[0] =~ m/^[^\W\d]\w*(?:::\w+)*\z/s and $_[0] ne $_[1] and $_[0]->isa($_[1]))
76             ? $_[0]
77             : undef;
78             }
79              
80             sub _NUMBER ($)
81             {
82 62     62   20454 my $arg = $_[0];
83 62 100 100     764 return (defined $arg and not ref $arg and looks_like_number($arg)) ? $arg : undef;
84             }
85              
86             sub _POSINT ($)
87             {
88 84     84   90700 my $arg = $_[0];
89 84 100 100     1091 return (defined $arg and not ref $arg and $arg =~ m/^[1-9]\d*$/) ? $arg : undef;
90             }
91              
92             sub _NONNEGINT ($)
93             {
94 88     88   23475 my $arg = $_[0];
95 88 100 100     1122 return (defined $arg and not ref $arg and $arg =~ m/^(?:0|[1-9]\d*)$/) ? $arg : undef;
96             }
97              
98             sub _SCALAR ($)
99             {
100 24 100 100 24   2392 return (ref $_[0] eq 'SCALAR' and defined ${$_[0]} and ${$_[0]} ne '') ? $_[0] : undef;
101             }
102              
103             sub _SCALAR0 ($)
104             {
105 28 100   28   194 return ref $_[0] eq 'SCALAR' ? $_[0] : undef;
106             }
107              
108             sub _ARRAY ($)
109             {
110 68 100 100 68   475 return (ref $_[0] eq 'ARRAY' and @{$_[0]}) ? $_[0] : undef;
111             }
112              
113             sub _ARRAY0 ($)
114             {
115 68 100   68   504 return ref $_[0] eq 'ARRAY' ? $_[0] : undef;
116             }
117              
118             sub _ARRAYLIKE
119             {
120             return (
121 36 100 100 36   162500 defined $_[0] and ref $_[0] and ((Scalar::Util::reftype($_[0]) eq 'ARRAY')
122             or overload::Method($_[0], '@{}'))
123             ) ? $_[0] : undef;
124             }
125              
126             sub _HASH ($)
127             {
128 22 100 100 22   142 return (ref $_[0] eq 'HASH' and scalar %{$_[0]}) ? $_[0] : undef;
129             }
130              
131             sub _HASH0 ($)
132             {
133 22 100   22   142 return ref $_[0] eq 'HASH' ? $_[0] : undef;
134             }
135              
136             sub _HASHLIKE
137             {
138             return (
139 36 100 100 36   12812 defined $_[0] and ref $_[0] and ((Scalar::Util::reftype($_[0]) eq 'HASH')
140             or overload::Method($_[0], '%{}'))
141             ) ? $_[0] : undef;
142             }
143              
144             sub _CODE ($)
145             {
146 24 100   24   158 return ref $_[0] eq 'CODE' ? $_[0] : undef;
147             }
148              
149             sub _CODELIKE($)
150             {
151             return (
152 17 100 100 17   7299 (Scalar::Util::reftype($_[0]) || '') eq 'CODE'
153             or Scalar::Util::blessed($_[0]) and overload::Method($_[0], '&{}')
154             ) ? $_[0] : undef;
155             }
156              
157             sub _INVOCANT($)
158             {
159             return (
160 20 100 100 20   452819 defined $_[0]
161             and (
162             defined Scalar::Util::blessed($_[0])
163             or
164             # We used to check for stash definedness, but any class-like name is a
165             # valid invocant for UNIVERSAL methods, so we stopped. -- rjbs, 2006-07-02
166             _CLASS($_[0])
167             )
168             ) ? $_[0] : undef;
169             }
170              
171             sub _INSTANCE ($$)
172             {
173 97 100 100 97   279185 return (Scalar::Util::blessed($_[0]) and $_[0]->isa($_[1])) ? $_[0] : undef;
174             }
175              
176             sub _INSTANCEDOES ($$)
177             {
178 38 100 66 38   204419 return (Scalar::Util::blessed($_[0]) and $_[0]->DOES($_[1])) ? $_[0] : undef;
179             }
180              
181             sub _REGEX ($)
182             {
183 22 100 100 22   1081 return (defined $_[0] and 'Regexp' eq ref($_[0])) ? $_[0] : undef;
184             }
185              
186             sub _SET ($$)
187             {
188 44     44   22175 my $set_param = shift;
189 44 100       125 _ARRAY($set_param) or return undef;
190 12         34 foreach my $item (@$set_param)
191             {
192 20 100       56 _INSTANCE($item, $_[0]) or return undef;
193             }
194 4         26 return $set_param;
195             }
196              
197             sub _SET0 ($$)
198             {
199 44     44   124 my $set_param = shift;
200 44 100       116 _ARRAY0($set_param) or return undef;
201 16         46 foreach my $item (@$set_param)
202             {
203 20 100       52 _INSTANCE($item, $_[0]) or return undef;
204             }
205 8         45 return $set_param;
206             }
207              
208             # We're doing this longhand for now. Once everything is perfect,
209             # we'll compress this into something that compiles more efficiently.
210             # Further, testing file handles is not something that is generally
211             # done millions of times, so doing it slowly is not a big speed hit.
212             sub _HANDLE
213             {
214 30     30   433323 my $it = shift;
215              
216             # It has to be defined, of course
217 30 100       97 unless (defined $it)
218             {
219 2         7 return undef;
220             }
221              
222             # Normal globs are considered to be file handles
223 28 100       117 if (ref $it eq 'GLOB')
224             {
225 6         22 return $it;
226             }
227              
228             # Check for a normal tied filehandle
229             # Side Note: 5.5.4's tied() and can() doesn't like getting undef
230 22 50 33     59 if (tied($it) and tied($it)->can('TIEHANDLE'))
231             {
232 0         0 return $it;
233             }
234              
235             # There are no other non-object handles that we support
236 22 100       56 unless (Scalar::Util::blessed($it))
237             {
238 20         91 return undef;
239             }
240              
241             # Check for a common base classes for conventional IO::Handle object
242 2 50       29 if ($it->isa('IO::Handle'))
243             {
244 0         0 return $it;
245             }
246              
247             # Check for tied file handles using Tie::Handle
248 2 50       13 if ($it->isa('Tie::Handle'))
249             {
250 0         0 return $it;
251             }
252              
253             # IO::Scalar is not a proper seekable, but it is valid is a
254             # regular file handle
255 2 50       10 if ($it->isa('IO::Scalar'))
256             {
257 0         0 return $it;
258             }
259              
260             # Yet another special case for IO::String, which refuses (for now
261             # anyway) to become a subclass of IO::Handle.
262 2 50       9 if ($it->isa('IO::String'))
263             {
264 0         0 return $it;
265             }
266              
267             # This is not any sort of object we know about
268 2         6 return undef;
269             }
270              
271             sub _DRIVER ($$)
272             {
273             ## no critic (BuiltinFunctions::ProhibitStringyEval)
274 38 100 100 38   139 return (defined _CLASS($_[0]) and eval "require $_[0];" and not $@ and $_[0]->isa($_[1]) and $_[0] ne $_[1]) ? $_[0] : undef;
275             }
276              
277             1;