Suffit/RefUtil.pm untested

filter: Statementstmt 0.0% 0 / 104 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 87 MC/DCmcdc 0.0% 0 / 45 Subroutinesub 0.0% 0 / 36 Podpod 0.0% 0 / 32 totaltotal 0.0% 0 / 384 CC 41 SCAR 35.9
CC
41
Cov
0%
is_void
156.0
as_array_ref
42.0
as_hash_ref
30.0
CRAP 36.3
1 package WWW::Suffit::RefUtil;
2 use strict;
3 use utf8;
4  
5 =encoding utf8
6  
7 =head1 NAME
8  
9 WWW::Suffit::RefUtil - Pure Perl Utility functions for checking references and data
10  
11 =head1 SYNOPSIS
12  
13 use WWW::Suffit::RefUtil qw/ :all /;
14  
15 =head1 DESCRIPTION
16  
17 Pure Perl Utility functions for checking references and data
18  
19 B<STOP!> All functions in this module have been deprecated in favor of L<Acrux::RefUtil>
20  
21 =head2 AS
22  
23 The 'as' functions are introduced by the C<:as> import tag, which check
24 the type of passed argument and returns it as required type
25  
26 =over 4
27  
28 =item as_array_ref
29  
30 Deprecated! See L<Acrux::RefUtil>
31  
32 =item as_array, as_list
33  
34 Deprecated! See L<Acrux::RefUtil>
35  
36 =item as_first, as_first_val
37  
38 Deprecated! See L<Acrux::RefUtil>
39  
40 =item as_hash_ref
41  
42 Deprecated! See L<Acrux::RefUtil>
43  
44 =item as_hash
45  
46 Deprecated! See L<Acrux::RefUtil>
47  
48 =item as_last, as_last_val, as_latest
49  
50 Deprecated! See L<Acrux::RefUtil>
51  
52 =back
53  
54 =head2 CHECK
55  
56 Check functions are introduced by the C<:check> import tag, which check
57 the argument type and return a bool
58  
59 =over 4
60  
61 =item is_ref
62  
63 Deprecated! See L<Acrux::RefUtil>
64  
65 =item is_scalar_ref
66  
67 Deprecated! See L<Acrux::RefUtil>
68  
69 =item is_array_ref
70  
71 Deprecated! See L<Acrux::RefUtil>
72  
73 =item is_hash_ref
74  
75 Deprecated! See L<Acrux::RefUtil>
76  
77 =item is_code_ref
78  
79 Deprecated! See L<Acrux::RefUtil>
80  
81 =item is_glob_ref
82  
83 Deprecated! See L<Acrux::RefUtil>
84  
85 =item is_regexp_ref, is_regex_ref, is_rx
86  
87 Deprecated! See L<Acrux::RefUtil>
88  
89 =item is_value
90  
91 Deprecated! See L<Acrux::RefUtil>
92  
93 =item is_string
94  
95 Deprecated! See L<Acrux::RefUtil>
96  
97 =item is_number
98  
99 Deprecated! See L<Acrux::RefUtil>
100  
101 =item is_integer, is_int8, is_int16, is_int32, is_int64
102  
103 Deprecated! See L<Acrux::RefUtil>
104  
105 =item is_undef
106  
107 Deprecated! See L<Acrux::RefUtil>
108  
109 =back
110  
111 =head2 VOID
112  
113 Void functions are introduced by the C<:void> import tag, which check
114 the argument type in void value and return a bool
115  
116 =over 4
117  
118 =item is_void
119  
120 Deprecated! See L<Acrux::RefUtil>
121  
122 =item isnt_void
123  
124 Deprecated! See L<Acrux::RefUtil>
125  
126 =back
127  
128 =head2 FLAG
129  
130 =over 4
131  
132 =item is_false_flag
133  
134 Deprecated! See L<Acrux::RefUtil>
135  
136 =item is_true_flag
137  
138 Deprecated! See L<Acrux::RefUtil>
139  
140 =back
141  
142 =head1 HISTORY
143  
144 See C<Changes> file
145  
146 =head1 TO DO
147  
148 See C<TODO> file
149  
150 =head1 SEE ALSO
151  
152 L<Acrux::RefUtil>, L<Data::Util::PurePerl>, L<Params::Classify>,
153 L<Ref::Util>, L<CTK::TFVals>, L<CTK::ConfGenUtil>
154  
155 =head1 AUTHOR
156  
157 Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
158  
159 =head1 COPYRIGHT
160  
161 Copyright (C) 1998-2026 D&D Corporation
162  
163 =head1 LICENSE
164  
165 This program is distributed under the terms of the Artistic License Version 2.0
166  
167 See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details
168  
169 =cut
170  
171 use base qw/Exporter/;
172 our @EXPORT = (qw/
173 is_ref is_undef
174 is_scalar_ref is_array_ref is_hash_ref is_code_ref
175 is_glob_ref is_regexp_ref is_regex_ref is_rx
176 is_value is_string is_number is_integer
177 is_int8 is_int16 is_int32 is_int64
178 /);
179  
180 # Required
181 our @EXPORT_OK = (qw/
182 is_void isnt_void
183 is_true_flag is_false_flag
184 as_array as_list as_array_ref as_hash as_hash_ref
185 as_first as_first_val as_last as_last_val as_latest
186 /, @EXPORT);
187  
188 # Tags
189 our %EXPORT_TAGS = (
190 all => [@EXPORT_OK],
191 check => [@EXPORT],
192 void => [qw/
193 is_void isnt_void
194 /],
195 flag => [qw/
196 is_true_flag is_false_flag
197 /],
198 as => [qw/
199 as_array as_list as_array_ref as_hash as_hash_ref
200 as_first as_first_val as_last as_last_val as_latest
201 /],
202 );
203  
204 use constant MAX_DEPTH => 32;
205  
206 # Base functions
207 sub is_ref { ref($_[0]) ? 1 : 0 }
208 sub is_undef { !defined($_[0]) }
209 sub is_scalar_ref { ref($_[0]) eq 'SCALAR' || ref($_[0]) eq 'REF' }
210 sub is_array_ref { ref($_[0]) eq 'ARRAY' }
211 sub is_hash_ref { ref($_[0]) eq 'HASH' }
212 sub is_code_ref { ref($_[0]) eq 'CODE' }
213 sub is_glob_ref { ref($_[0]) eq 'GLOB' }
214 sub is_regexp_ref { ref($_[0]) eq 'Regexp' }
215 sub is_regex_ref { goto &is_regexp_ref }
216 sub is_rx { goto &is_regexp_ref }
217 sub is_value { defined($_[0]) && !ref($_[0]) && ref(\$_[0]) ne 'GLOB' }
218 sub is_string { defined($_[0]) && !ref($_[0]) && (ref(\$_[0]) ne 'GLOB') && length($_[0]) }
219 sub is_number { (defined($_[0]) && !ref($_[0]) && $_[0] =~ /^[+-]?(?=\d|\.\d)\d*(\.\d*)?(?:[Ee](?:[+-]?\d+))?$/) ? 1 : 0 }
220 sub is_integer { (defined($_[0]) && !ref($_[0]) && $_[0] =~ /^[+-]?\d+$/) ? 1 : 0 }
221 sub is_int8 { (defined($_[0]) && !ref($_[0]) && ($_[0] =~ /^[0-9]{1,3}$/) && ($_[0] < 2**8)) ? 1 : 0 }
222 sub is_int16 { (defined($_[0]) && !ref($_[0]) && ($_[0] =~ /^[0-9]{1,5}$/) && ($_[0] < 2**16)) ? 1 : 0 }
223 sub is_int32 { (defined($_[0]) && !ref($_[0]) && ($_[0] =~ /^[0-9]{1,10}$/) && ($_[0] < 2**32)) ? 1 : 0 }
224 sub is_int64 { (defined($_[0]) && !ref($_[0]) && $_[0] =~ /^[0-9]{1,20}$/) ? 1 : 0 }
225  
226 # Extended
227 sub is_void {
228 my $struct = shift;
229 my $depth = shift || 0;
230 return 1 unless defined($struct); # CATCHED! THIS IS REAL UNDEFINED VALUE
231 return 0 if defined($struct) && !ref($struct); # VALUE, NOT REFERENCE
232 if (is_int8($depth) && $depth > 0) {
233 return 1 unless is_int8($depth);
234 } else {
235 return 1 unless is_int8($depth);
236 }
237 $depth++;
238 return 0 if $depth >= MAX_DEPTH; # Exit from the recursion
239  
240 my $t = ref($struct);
241 if ($t eq 'SCALAR') {
242 return is_void($$struct, $depth)
243 } elsif ($t eq 'ARRAY') {
244 for (@$struct) {
245 return 0 unless is_void($_, $depth);
246 }
247 return 1; # DEFINED DATA NOT FOUND - VOID
248 } elsif ($t eq 'HASH') {
249 return 0 if keys(%$struct);
250 return 1; # DEFINED DATA NOT FOUND - VOID
251 }
252  
253 # CODE, REF, GLOB, LVALUE, FORMAT, IO, VSTRING and Regexp are not supported here
254 return 0; # NOT VOID
255 }
256 sub isnt_void {is_void(@_) ? 0 : 1}
257 sub is_true_flag {
258 my $f = shift || return 0;
259 return $f =~ /^(on|y|true|enable|1)/i ? 1 : 0;
260 }
261 sub is_false_flag {
262 my $f = shift || return 1;
263 return $f =~ /^(off|n|false|disable|0)/i ? 1 : 0;
264 }
265  
266 # As
267 sub as_array_ref {
268 return [] unless scalar @_; # if no args
269 return [@_] if scalar(@_) > 1; # if too many args
270 return [] unless defined($_[0]); # if value is undef
271 if (ref($_[0]) eq 'ARRAY') { return $_[0] } # Array
272 elsif (ref($_[0]) eq 'HASH') { return [%{$_[0]}] } # Hash
273 return [$_[0]];
274 }
275 sub as_array {
276 my $r = as_array_ref(@_);
277 return wantarray ? @$r : $r;
278 }
279 sub as_list { goto &as_array }
280 sub as_hash_ref {
281 return {} unless scalar @_; # if no args passed
282 return {@_} unless scalar(@_) % 2; # if even (not odd) args passed
283 return {} unless defined($_[0]); # if arg is undef
284 if (ref($_[0]) eq 'HASH') { return $_[0] } # Hash
285 return {};
286 }
287 sub as_hash {
288 my $r = as_hash_ref(@_);
289 return wantarray ? %$r : $r;
290 }
291 sub as_first {
292 return undef unless defined $_[0];
293 my $r = as_array_ref(@_);
294 return undef unless exists($r->[0]) && defined($r->[0]);
295 my $v = $r->[0];
296 if (!ref($v)) { return $v } # No ref
297 elsif (ref($v) eq 'SCALAR' || ref($v) eq 'REF') { return $$v } # Scalar ref
298 return $v;
299 }
300 sub as_first_val { goto &as_first }
301 sub as_last {
302 return undef unless defined $_[0];
303 my $r = as_array_ref(@_);
304 return undef unless exists($r->[0]) && defined($r->[0]);
305 my $v = $r->[-1];
306 if (!ref($v)) { return $v } # No ref
307 elsif (ref($v) eq 'SCALAR' || ref($v) eq 'REF') { return $$v } # Scalar ref
308 return $v;
309 }
310 sub as_last_val { goto &as_last }
311 sub as_latest { goto &as_last }
312  
313 1;
314  
315 __END__