line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
5
|
|
|
5
|
|
279112
|
use warnings; use strict; |
|
5
|
|
|
5
|
|
40
|
|
|
5
|
|
|
|
|
196
|
|
|
5
|
|
|
|
|
28
|
|
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
185
|
|
3
|
|
|
|
|
|
|
# vim=:SetNumberAndWidth |
4
|
|
|
|
|
|
|
=encoding utf-8 |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Types::Core - Core types defined as tests and literals (ease of use) |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 VERSION |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Version "0.3.0"; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
################################################################################ |
18
|
|
|
|
|
|
|
{ package Types::Core; |
19
|
5
|
|
|
5
|
|
24
|
use strict; use warnings; |
|
5
|
|
|
5
|
|
9
|
|
|
5
|
|
|
|
|
102
|
|
|
5
|
|
|
|
|
33
|
|
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
245
|
|
20
|
|
|
|
|
|
|
our $VERSION='0.3.0'; |
21
|
5
|
|
|
5
|
|
2502
|
use mem; |
|
5
|
|
|
|
|
1348
|
|
|
5
|
|
|
|
|
19
|
|
22
|
5
|
|
|
5
|
|
288
|
use constant Self => __PACKAGE__; |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
1035
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# 0.3.0 - NOTE: change in false value of blessed + type from |
25
|
|
|
|
|
|
|
# "undef" to "" (0 length string) |
26
|
|
|
|
|
|
|
# This was done to provide compatibility with "ref" when |
27
|
|
|
|
|
|
|
# it is used on a non-reference or on undef. Logic _seems_ |
28
|
|
|
|
|
|
|
# to be that a non-ref, untyped or unblessed value is |
29
|
|
|
|
|
|
|
# empty -- not undefined. While this is arbitrary, that |
30
|
|
|
|
|
|
|
# "ref" returns an empty string, establishes precedent. |
31
|
|
|
|
|
|
|
# The same logic would not apply using 'length' on an |
32
|
|
|
|
|
|
|
# "undef" value, as "undef" would likely be considered |
33
|
|
|
|
|
|
|
# dimensionless and therefore would have no length. |
34
|
|
|
|
|
|
|
# classname for 'undef'. |
35
|
|
|
|
|
|
|
# - Finished test 't06' that exercises 'Cmp' and made sure |
36
|
|
|
|
|
|
|
# Cmp is working (as per test). Will fix problems as found. |
37
|
|
|
|
|
|
|
# 0.2.8 - isnum was broken |
38
|
|
|
|
|
|
|
# - ErV+EhV didn't argcheck undefs. Hmph! |
39
|
|
|
|
|
|
|
# 0.2.7 - EhV didn't properly test a blessed HASH (but ErV did) |
40
|
|
|
|
|
|
|
# - Added tests for both in t00.t and fixed code |
41
|
|
|
|
|
|
|
# 0.2.6 - Removed another spurious ref, this time to Carp::Always. |
42
|
|
|
|
|
|
|
# 0.2.5 - Removed spurious reference to unneeded module in t/t00.t. |
43
|
|
|
|
|
|
|
# No other source changes. |
44
|
|
|
|
|
|
|
# 0.2.4 - fixed current tests in 5.{12,10,8}.x; added some tests for |
45
|
|
|
|
|
|
|
# Cmp function to allow comparing objects and sorting them |
46
|
|
|
|
|
|
|
# though still leaving it undocumented, as not sure how |
47
|
|
|
|
|
|
|
# useful it is |
48
|
|
|
|
|
|
|
# 0.2.2 - fixed prototype of isnum, tighted up interface and documented it |
49
|
|
|
|
|
|
|
# - fix some test suit failures under older (<5.12) perls |
50
|
|
|
|
|
|
|
# 0.2.1 - re-add 'type' as OK synonym for 'typ' (both in EXPORT_OK) |
51
|
|
|
|
|
|
|
# 0.2.0 - Allow isnum to take inferred $_ as param. |
52
|
|
|
|
|
|
|
# 0.1.10 - Added Cmp function for nested data structs |
53
|
|
|
|
|
|
|
# 0.1.9 Features: |
54
|
|
|
|
|
|
|
# - add ErV supercedes EhV, but also works for arrays (EXPORT) |
55
|
|
|
|
|
|
|
# - change EhV proto to support multiply nested refs. |
56
|
|
|
|
|
|
|
# - add 'LongFunc' & 'ShortFunc' for name of current function either |
57
|
|
|
|
|
|
|
# with Package(Long) or without(Short); (EXPORT_OK) |
58
|
|
|
|
|
|
|
# - Add 'mk_array/mkARRAY' + 'mk_hash/mkHASH' optional exports |
59
|
|
|
|
|
|
|
# Fixes: |
60
|
|
|
|
|
|
|
# - delete unused sub referencing 'B' |
61
|
|
|
|
|
|
|
# - PerlDoc updates |
62
|
|
|
|
|
|
|
# 0.1.8 - (fix) remove reference created during development, but not needed |
63
|
|
|
|
|
|
|
# - in final version, in t04.t in the testing directory |
64
|
|
|
|
|
|
|
# 0.1.7 - Attempt to fix a parsing problem in 5.8.9 |
65
|
|
|
|
|
|
|
# 0.1.6 - Needed to split a statement that parsed in a different order under |
66
|
|
|
|
|
|
|
# 5.8.x |
67
|
|
|
|
|
|
|
# 0.1.5 - Added code and test case to handle type-named classes |
68
|
|
|
|
|
|
|
# - use Scalar::Utils for blessed and 'ref' if available. |
69
|
|
|
|
|
|
|
# 0.1.4 - Add BUILD_REQ for more modern Ext:MM |
70
|
|
|
|
|
|
|
# 0.1.3 - investigate fails on perl 5.12.x: |
71
|
|
|
|
|
|
|
# - changed prototypes on single arg tests to use '$' instead of '*'; |
72
|
|
|
|
|
|
|
# - changed test to use parens around unary ops (needed in 5.12 & before) |
73
|
|
|
|
|
|
|
# - tested back to 5.8.9 ( & remove version restriction "use 5.12"). |
74
|
|
|
|
|
|
|
# - added tests for CODE & REF |
75
|
|
|
|
|
|
|
# - clarified true/false returned values |
76
|
|
|
|
|
|
|
# 0.1.2 - Write tests to verify solo string equality, returning $var on true, |
77
|
|
|
|
|
|
|
# capturing undef and returning false; |
78
|
|
|
|
|
|
|
# - doc updates |
79
|
|
|
|
|
|
|
# 0.1.1 - move to using Xporter so EXPORT_OK works w/o deleting defaults |
80
|
|
|
|
|
|
|
# - narrow focus of module -- Default to: Basic types, EhV & |
81
|
|
|
|
|
|
|
# possible addon of "blessed", "typ" |
82
|
|
|
|
|
|
|
# 0.1.0 - regularized some naming (Type->type cf. ref; Ref->ref, cf ref) in |
83
|
|
|
|
|
|
|
# function names; modularized/functionized type checks |
84
|
|
|
|
|
|
|
# - Made previous True/False values return the original value for True |
85
|
|
|
|
|
|
|
# 0.0.6 - Add ability to use Scalar::Util 'reftype' to determine which |
86
|
|
|
|
|
|
|
# of the base types something is (sans classes). Fall back |
87
|
|
|
|
|
|
|
# to pattern matching if it isn't available. |
88
|
|
|
|
|
|
|
# - Add IO & GLOB to fill out basic type representation |
89
|
|
|
|
|
|
|
# - remove obsolute function calls prior to publishing; |
90
|
|
|
|
|
|
|
# 0.0.5 - Added type_check function |
91
|
|
|
|
|
|
|
# 0.0.4 - add RefInit |
92
|
|
|
|
|
|
|
# 0.0.3 - add SCALAR test |
93
|
|
|
|
|
|
|
# - code simplification |
94
|
|
|
|
|
|
|
# 0.0.2 - Export EhV by default |
95
|
|
|
|
|
|
|
#}}} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# MAINT NOTE: this module must not "use P" (literally _use_) |
98
|
|
|
|
|
|
|
# as "P" needs to use this mod (or dup the functionality) |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
our (@CORETYPES, @EXPORT, @EXPORT_OK, %type_lens); |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
BEGIN { |
103
|
5
|
|
|
5
|
|
25
|
@CORETYPES = qw(ARRAY CODE GLOB HASH IO REF SCALAR); |
104
|
5
|
|
|
|
|
14
|
%type_lens = map { ($_, length $_ ) } @CORETYPES; |
|
35
|
|
|
|
|
121
|
|
105
|
5
|
|
|
|
|
22
|
@EXPORT = (@CORETYPES, qw(EhV ErV)); |
106
|
5
|
|
|
|
|
18
|
@EXPORT_OK = ( qw( typ type blessed |
107
|
|
|
|
|
|
|
LongSub ShortSub |
108
|
|
|
|
|
|
|
isnum Cmp |
109
|
|
|
|
|
|
|
InClass IsClass |
110
|
|
|
|
|
|
|
Obj |
111
|
|
|
|
|
|
|
mk_array mkARRAY |
112
|
|
|
|
|
|
|
mk_hash mkHASH |
113
|
|
|
|
|
|
|
) ); |
114
|
5
|
|
|
5
|
|
2497
|
use Xporter; |
|
5
|
|
|
|
|
9770
|
|
|
5
|
|
|
|
|
20
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub InClass($;$) { |
117
|
0
|
|
|
0
|
0
|
0
|
my $class = shift; |
118
|
0
|
0
|
|
0
|
|
0
|
if (!@_) { return sub ($) { ref $_[0] eq $class } } |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
119
|
0
|
|
|
|
|
0
|
else { return ref $_[0] eq $class } |
120
|
|
|
|
|
|
|
} |
121
|
0
|
|
|
0
|
0
|
0
|
sub IsClass($;$) { goto &InClass } |
122
|
|
|
|
|
|
|
|
123
|
5
|
|
|
5
|
|
763
|
use constant shortest_type => 'REF'; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
355
|
|
124
|
5
|
|
|
5
|
|
30
|
use constant last_let_offset => length(shortest_type)-1; |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
230
|
|
125
|
5
|
|
|
5
|
|
45
|
use constant Empty => ""; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
531
|
|
126
|
|
|
|
|
|
|
|
127
|
5
|
|
|
|
|
1510
|
eval '# line ' . __LINE__ .' '. __FILE__ .' |
128
|
11
|
50
|
|
11
|
|
30
|
sub _type ($) { |
129
|
11
|
|
|
|
|
26
|
return Empty unless defined $_[0]; |
130
|
11
|
50
|
|
|
|
22
|
my $end = index $_[0], "("; |
131
|
11
|
|
|
|
|
22
|
return Empty unless $end > '. &last_let_offset .'; |
132
|
11
|
|
|
|
|
80
|
my $start = 1+rindex($_[0], "=", $end); |
133
|
|
|
|
|
|
|
substr $_[0], $start, $end-$start; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
17
|
|
|
17
|
|
36
|
sub _isatype($$) { |
137
|
17
|
100
|
100
|
|
|
160
|
my ($var, $type) = @_; |
138
|
|
|
|
|
|
|
ref $var && (1 + index($var, $type."(" )) ? $var : Empty; |
139
|
|
|
|
|
|
|
} |
140
|
10
|
|
|
10
|
0
|
2361
|
sub blessed ($) { |
141
|
10
|
50
|
|
|
|
27
|
my $arg = $_[0]; |
142
|
10
|
|
|
|
|
22
|
return Empty unless defined $arg; |
143
|
10
|
50
|
|
|
|
23
|
my $ref_arg = ref $arg; |
144
|
10
|
100
|
66
|
|
|
68
|
return Empty unless $ref_arg; |
145
|
6
|
|
|
|
|
10
|
($ref_arg && !$type_lens{$ref_arg}) ? $arg : do { |
146
|
6
|
100
|
|
|
|
58
|
my $len = $type_lens{$ref_arg}; |
147
|
|
|
|
|
|
|
$ref_arg."=" eq substr($arg, 0, $len+1) ? $arg : Empty }; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
'; #end of eval |
150
|
5
|
50
|
|
|
|
859
|
$@ && die "_isatype+blessed eval(2): $@"; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
17
|
|
|
17
|
0
|
54
|
sub isatype($$) {goto &_isatype} |
155
|
11
|
|
|
11
|
0
|
912
|
sub typ($) {goto &_type} |
156
|
0
|
|
|
0
|
0
|
0
|
sub type($) {goto &_type} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 SYNOPSIS |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
my @data_types = (ARRAY CODE GLOB HASH IO REF SCALAR); |
164
|
|
|
|
|
|
|
my $ref = $_[0]; |
165
|
|
|
|
|
|
|
P "Error: expected %s", HASH unless HASH $ref; |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Syntax symplifier for type checking. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Allows easy, unquoted use of var types (ARRAY, SCALAR, etc.) |
170
|
|
|
|
|
|
|
as literals, and allows standard type names to be used as boolean |
171
|
|
|
|
|
|
|
checks of the type of a reference as well as passing through the value |
172
|
|
|
|
|
|
|
of the reference. For example: C will return true |
173
|
|
|
|
|
|
|
if the reference points to a HASH or a HASH-based object. |
174
|
|
|
|
|
|
|
For example, "HASH $href" |
175
|
|
|
|
|
|
|
check routines of references. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head1 USAGE |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=over |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
B>> - Check if I[ has underlying type, I ] |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
B> - Literal usage equal to itself |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=back |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head1 EXAMPLE |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
printf "type = %s\n", HASH if HASH $var; |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
Same as: |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
printf "type = %s\n", 'HASH' if ref $var eq 'HASH';) |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=head1 DESCRIPTION |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
For the most basic functions listed in the Synopsis, they take |
200
|
|
|
|
|
|
|
either 0 or 1 arguments. If 1 parameter, then they test it |
201
|
|
|
|
|
|
|
to see if the C[ is of the given I (blessed or not). ] |
202
|
|
|
|
|
|
|
If false, I> is returned, of true, the ref, itself is returned. |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
For no args, they return literals of themselves, allowing the |
205
|
|
|
|
|
|
|
named strings to be used as Literals w/o quotes. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head1 MORE EXAMPLES |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=head4 Initialization |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
our %field_types = (Paths{type => ARRAY, ...}); |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=head4 Flow Routing |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
... |
216
|
|
|
|
|
|
|
my $ref_arg = ref $arg; |
217
|
|
|
|
|
|
|
return ARRAY $ref_arg ? statAR_2_Ino_t($path,$arg) : |
218
|
|
|
|
|
|
|
InClass('stat_t', $ref_arg) ? stat_t_2_Ino_t($path, $arg) : |
219
|
|
|
|
|
|
|
_path_2_Ino_t($path); } |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head4 Data Verification |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
sub Type_check($;$) { ... |
224
|
|
|
|
|
|
|
if (ARRAY $cfp) { |
225
|
|
|
|
|
|
|
for (@$cfp) { |
226
|
|
|
|
|
|
|
die P "Field %s does not exist", $_ unless exists $v->{$_}; |
227
|
|
|
|
|
|
|
my $cls_ftpp = $class."::field_types"; |
228
|
|
|
|
|
|
|
if (HASH $cls_ftpp) { |
229
|
|
|
|
|
|
|
if ($cls_ftpp->{type} eq ARRAY) { ... |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=head4 Param Checking |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
sub popable (+) { |
234
|
|
|
|
|
|
|
my $ar = $_[0]; |
235
|
|
|
|
|
|
|
ARRAY $ar or die P "popable only works with arrays, not %s", ref $ar; } |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=head4 Return Value Checks and Dereference Protection |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
my $Inos = $mp->get_sorted_Ino_t_Array; |
240
|
|
|
|
|
|
|
return undef unless ARRAY $Inos and @$Inos >= 2; |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=cut |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
BEGIN { # create the type functions... |
248
|
11
|
100
|
|
11
|
0
|
224
|
eval '# line ' . __LINE__ .' '. __FILE__ .' |
|
4
|
100
|
|
4
|
0
|
102
|
|
|
1
|
50
|
|
1
|
0
|
5
|
|
|
9
|
100
|
|
9
|
0
|
2350
|
|
|
0
|
0
|
|
0
|
0
|
0
|
|
|
3
|
100
|
|
3
|
0
|
14
|
|
|
1
|
50
|
|
1
|
|
5
|
|
249
|
|
|
|
|
|
|
sub ' . $_ . ' (;*) { @_ ? isatype($_[0], '.$_.') : '.$_.' } ' |
250
|
5
|
|
|
5
|
|
6145
|
for @CORETYPES; |
|
|
|
|
0
|
|
|
|
251
|
|
|
|
|
|
|
} |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=head2 Non-instantiating existence checks in references: C. |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
S< > |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
ErV $ref, FIELDNAME; # Exist[in]reference? Value : C |
259
|
|
|
|
|
|
|
ErV $hashref, FIELDNAME; # Exist[in]hashref? Value : C |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=over |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
If fieldname exists in the ref pointed to by the reference, return the value, |
264
|
|
|
|
|
|
|
else return undef. |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
=back |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
=head2 Note: What's EhV? (Deprecated) |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=over |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
You may see older code using C. M only had this checker |
273
|
|
|
|
|
|
|
for hashes, but given combinations of various references, the more |
274
|
|
|
|
|
|
|
general C replaced it. |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=back |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=head1 OPTIONAL FUNCTIONS: C & C |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
S< > |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
typ REF; #return underlying type of REF |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
Once you bless a reference to an object, its type becomes hidden |
287
|
|
|
|
|
|
|
from C[. C allows you to peek into a class reference to ] |
288
|
|
|
|
|
|
|
see the basic perl type that the class is based on. |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
Most users of a class won't have a need for that information, |
291
|
|
|
|
|
|
|
but a 'friend' of the class might in order to offer helper functions. |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
blessed REF; #test if REF is blessed or not |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
Needed for consistency with 'ref' (and typ). 'ref' passes back the |
298
|
|
|
|
|
|
|
actual value of the 'ref' if it is a ref. Following that example, |
299
|
|
|
|
|
|
|
'typ' return the underlying type of a perl-ref if it is a reference. |
300
|
|
|
|
|
|
|
In the same way, 'blessed' returns the name of the object's |
301
|
|
|
|
|
|
|
blessing (its class or package name) if it is 'blessed'. |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
Warning: take care that L's version of C |
304
|
|
|
|
|
|
|
isn't also included, as it throws away the package or blessing |
305
|
|
|
|
|
|
|
name and only returns '1' if its argument is blessed. |
306
|
|
|
|
|
|
|
perl-type of a reference if it is a reference.Included for it's usefulness in type checking. Similar functionality |
307
|
|
|
|
|
|
|
as implemented in L. This version of C |
308
|
|
|
|
|
|
|
will use the C version if it is already present. |
309
|
|
|
|
|
|
|
Otherwise it uses a pure-perl implementation. |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
=head1 EXAMPLE: C |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
S< > |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
To prevent automatic creation of variables when accessed |
318
|
|
|
|
|
|
|
or tested for C, (i.e. autovivification), one must test |
319
|
|
|
|
|
|
|
for existence first, before attempting to read or test the |
320
|
|
|
|
|
|
|
'defined'-ness of the value. |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
This results in a 2 step process to retrive a value: |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
exists $name{$testname} ? $name{testname} : undef; |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
If you have multiple levels of hash tables say retrieving SSN's |
327
|
|
|
|
|
|
|
via {$lastname}{$firstname} in object member 'name2ssns' but |
328
|
|
|
|
|
|
|
don't know if the object member is valid or not, the safe way |
329
|
|
|
|
|
|
|
to write this would be: |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
my $p = $this; |
332
|
|
|
|
|
|
|
if (exists $p->{name2ssns} && defined $p->{name2ssns}) { |
333
|
|
|
|
|
|
|
$p = $p->{name2ssns}; |
334
|
|
|
|
|
|
|
if (exists $p->{$lastname} && defined $p->{$lastname}) { |
335
|
|
|
|
|
|
|
$p = $p->{$lastname}; |
336
|
|
|
|
|
|
|
if (exists $p->{$firstname}) { |
337
|
|
|
|
|
|
|
return $p->{$firstname}; |
338
|
|
|
|
|
|
|
} |
339
|
|
|
|
|
|
|
} |
340
|
|
|
|
|
|
|
} |
341
|
|
|
|
|
|
|
return undef; |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
C saves some steps. Instead of testing for existence, 'definedness', |
344
|
|
|
|
|
|
|
and then use the value to go deeper in the structuer, C does the |
345
|
|
|
|
|
|
|
testing and returns the value (or undef) in one step. |
346
|
|
|
|
|
|
|
Thus, the above could be written: |
347
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
my $p = $this; |
349
|
|
|
|
|
|
|
return $p = ErV $p, name2ssns and |
350
|
|
|
|
|
|
|
$p = ErV $p, $lastname and |
351
|
|
|
|
|
|
|
ErV $p, $firstname; |
352
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
This not only saves coding space & time, but allows faster |
354
|
|
|
|
|
|
|
comprehension of what is going on (presuming familiarity |
355
|
|
|
|
|
|
|
with C). |
356
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
Multiple levels of hashes or arrays may be tested in one usage. Example: |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
my $nested_refs = {}; |
360
|
|
|
|
|
|
|
$nested_refs->{a}{b}{c}{d}[2]{f}[1] = 7; |
361
|
|
|
|
|
|
|
P "---\nval=%s", ErV $nested_refs, a, b, c, d, e, f, g; |
362
|
|
|
|
|
|
|
--- |
363
|
|
|
|
|
|
|
val=7 |
364
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
The current ErV handles around thirty levels of nested hashing. |
366
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
=cut |
368
|
|
|
|
|
|
|
|
369
|
0
|
|
|
|
|
0
|
BEGIN { |
370
|
|
|
|
|
|
|
sub ErV ($*;******************************) { |
371
|
10
|
|
|
10
|
0
|
26
|
my ($arg, $field) = (shift, shift); |
372
|
10
|
100
|
100
|
|
|
50
|
return undef unless $field && $arg; |
373
|
5
|
|
|
|
|
7
|
my $offset; |
374
|
5
|
50
|
|
|
|
13
|
$field = substr $field,$offset+2 if 1 + ($offset = rindex $field,'::'); |
375
|
5
|
|
|
|
|
7
|
my $h; |
376
|
5
|
|
66
|
|
|
17
|
while (defined $field and |
|
|
|
33
|
|
|
|
|
377
|
|
|
|
|
|
|
(($h=HASH $arg) && exists $arg->{$field} or |
378
|
|
|
|
|
|
|
ARRAY $arg && $field =~ /^[-\d]+$/ && exists $arg->[$field])) { |
379
|
3
|
50
|
|
|
|
9
|
$arg = $h ? $arg->{$field} : $arg->[$field]; |
380
|
3
|
50
|
|
|
|
8
|
$field = shift, next if @_; |
381
|
3
|
|
|
|
|
12
|
return $arg; |
382
|
|
|
|
|
|
|
} |
383
|
2
|
|
|
|
|
10
|
return undef; |
384
|
|
|
|
|
|
|
} |
385
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
sub EhV ($*;******************************) { |
387
|
10
|
|
|
10
|
0
|
23
|
my ($arg, $field) = (shift, shift); |
388
|
10
|
100
|
100
|
|
|
50
|
return undef unless $field && $arg; |
389
|
5
|
|
|
|
|
7
|
my $offset; |
390
|
5
|
50
|
|
|
|
12
|
$field = substr $field,$offset+2 if 1 + ($offset = rindex $field,'::'); |
391
|
5
|
|
33
|
|
|
18
|
while (defined($arg) && typ($arg) eq 'HASH' and |
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
392
|
|
|
|
|
|
|
defined($field) && exists $arg->{$field}) { |
393
|
3
|
50
|
|
|
|
21
|
return $arg->{$field} unless @_ > 0; |
394
|
0
|
|
|
|
|
0
|
$arg = $arg->{$field}; |
395
|
0
|
|
|
|
|
0
|
$field = shift; |
396
|
|
|
|
|
|
|
} |
397
|
2
|
|
|
|
|
9
|
return undef; |
398
|
|
|
|
|
|
|
} |
399
|
|
|
|
|
|
|
|
400
|
|
|
|
|
|
|
|
401
|
0
|
0
|
|
0
|
0
|
|
sub LongSub(;$) { ((caller (@_ ? 1+$_[0] : 1))[3]) || __PACKAGE__."::" } |
|
|
0
|
|
|
|
|
|
402
|
|
|
|
|
|
|
sub ShortSub(;$) { |
403
|
0
|
|
0
|
0
|
0
|
|
my $f = (@_ ? LongSub(1+$_[0]) : LongSub(1) ) || ""; |
404
|
0
|
|
|
|
|
|
substr $f, (1+rindex $f,':') } |
405
|
|
|
|
|
|
|
|
406
|
0
|
0
|
|
0
|
1
|
|
sub mk_array($) { $_[0] = [] unless q(ARRAY) eq ref $_[0] ; $_[0] } |
|
0
|
|
|
|
|
|
|
407
|
0
|
|
|
0
|
0
|
|
sub mkARRAY($) { goto &mk_array } |
408
|
0
|
0
|
|
0
|
1
|
|
sub mk_hash($) { $_[0] = {} unless q(HASH) eq ref $_[0] ; $_[0] } |
|
0
|
|
|
|
|
|
|
409
|
0
|
|
|
0
|
0
|
|
sub mkHASH($) { goto &mk_hash } |
410
|
|
|
|
|
|
|
|
411
|
|
|
|
|
|
|
# Obj - 1 or 2 parms (on top of "objref" ($p)) |
412
|
|
|
|
|
|
|
##1st param - name to verify against; verify against objptr by default |
413
|
|
|
|
|
|
|
#2nd optional parm = verify against this ref instead of objptr |
414
|
|
|
|
|
|
|
# |
415
|
|
|
|
|
|
|
sub Obj($;$) { |
416
|
0
|
0
|
0
|
0
|
0
|
|
my $objref = ref $_[0] || $_[0] eq Self ? shift : ""; |
417
|
0
|
|
|
|
|
|
my $objname = shift; # txt name |
418
|
0
|
0
|
|
|
|
|
$objref = ref $_[0] if @_; # if another parm, chk it as ref |
419
|
0
|
0
|
|
|
|
|
return $objref eq $objname ? $objname : ""; |
420
|
|
|
|
|
|
|
} |
421
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
} |
423
|
|
|
|
|
|
|
|
424
|
|
|
|
|
|
|
|
425
|
|
|
|
|
|
|
=head2 MORE OPTIONAL FUNCTIONS C and C |
426
|
|
|
|
|
|
|
|
427
|
|
|
|
|
|
|
|
428
|
|
|
|
|
|
|
$< > |
429
|
|
|
|
|
|
|
|
430
|
|
|
|
|
|
|
mk_array $p->ar; |
431
|
|
|
|
|
|
|
|
432
|
|
|
|
|
|
|
without C, the following generates a runtime error (can't |
433
|
|
|
|
|
|
|
use an undefined value as an ARRAY reference): |
434
|
|
|
|
|
|
|
|
435
|
|
|
|
|
|
|
my $ar; |
436
|
|
|
|
|
|
|
printf "items in ar:%s\n", 0+@{$ar}; |
437
|
|
|
|
|
|
|
|
438
|
|
|
|
|
|
|
but using mk_array will ensure there is an ARRAY ref there if there |
439
|
|
|
|
|
|
|
is not one there already: |
440
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
my $ar; |
442
|
|
|
|
|
|
|
mk_array $ar; |
443
|
|
|
|
|
|
|
printf "items in ar:%s\n", 0+@{$ar}; |
444
|
|
|
|
|
|
|
|
445
|
|
|
|
|
|
|
While the above would be solved by initalizing $ar when defined, |
446
|
|
|
|
|
|
|
expicit initialization might be useful to protect against the same |
447
|
|
|
|
|
|
|
type of error in dynamically allocated variables. |
448
|
|
|
|
|
|
|
|
449
|
|
|
|
|
|
|
|
450
|
|
|
|
|
|
|
=head1 UTILITY FUNCTIONS: C & C |
451
|
|
|
|
|
|
|
|
452
|
|
|
|
|
|
|
S< > |
453
|
|
|
|
|
|
|
|
454
|
|
|
|
|
|
|
isnum STR #return if it starts at beginning of STR |
455
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
Cmp [$p1,$p2] # C-like function for nested structures |
457
|
|
|
|
|
|
|
# uses C<$a>, C<$b> as default inputs |
458
|
|
|
|
|
|
|
# can be used in sort for well-behaved data |
459
|
|
|
|
|
|
|
# (incompare-able data will return undef) |
460
|
|
|
|
|
|
|
# builtin debug to see where compare fails |
461
|
|
|
|
|
|
|
# |
462
|
|
|
|
|
|
|
|
463
|
|
|
|
|
|
|
C checks for a number (int, float, or with exponent) at the |
464
|
|
|
|
|
|
|
beginning of the string passed in. With no argument uses C<$_> |
465
|
|
|
|
|
|
|
as the parameter. Returns the number with any non-number suffix |
466
|
|
|
|
|
|
|
stripped off or C if no num is found at the beginning |
467
|
|
|
|
|
|
|
of the string. C is an optional import that must be included |
468
|
|
|
|
|
|
|
via C<@EXPORTS_OK>. Note: to determine if false, you must use |
469
|
|
|
|
|
|
|
C since numeric '0' can be returned and would also |
470
|
|
|
|
|
|
|
evaluate to false. |
471
|
|
|
|
|
|
|
|
472
|
|
|
|
|
|
|
The existence of C is a B needs. To compare |
473
|
|
|
|
|
|
|
validity of released functions, it was necessary to recursively |
474
|
|
|
|
|
|
|
compare nested data structures. To support development, debug |
475
|
|
|
|
|
|
|
output was added that can be toggled on at runtime to see where |
476
|
|
|
|
|
|
|
a compare fails. |
477
|
|
|
|
|
|
|
|
478
|
|
|
|
|
|
|
Normally you only use two parameters C<$a> and C<$b> that are references |
479
|
|
|
|
|
|
|
to the data structures to be compared. If debugging is wanted, |
480
|
|
|
|
|
|
|
a third (or first if C<$a> and C<$b> are used) parameter can be |
481
|
|
|
|
|
|
|
pass with a non-zero value to enable primitive debug output. |
482
|
|
|
|
|
|
|
|
483
|
|
|
|
|
|
|
Additionally, if the compare I and does not return an integer |
484
|
|
|
|
|
|
|
value (returning C instead), a 2nd return value can tell you |
485
|
|
|
|
|
|
|
where in the compare it failed. To grab that return value, |
486
|
|
|
|
|
|
|
use a two element list or an array to catch the status, like |
487
|
|
|
|
|
|
|
|
488
|
|
|
|
|
|
|
C and C<$b>) |
489
|
|
|
|
|
|
|
|
490
|
|
|
|
|
|
|
If the compare was successful, it will return -1, 0 or 1 as 'cmp' |
491
|
|
|
|
|
|
|
does. If it fails, C<$result> will contain C and C<$err> will |
492
|
|
|
|
|
|
|
contain a number indicating what test failed. |
493
|
|
|
|
|
|
|
|
494
|
|
|
|
|
|
|
Failures can occur if Cmp is asked to compare different object with |
495
|
|
|
|
|
|
|
different refs ('blessed' refname), or same blessed class and different |
496
|
|
|
|
|
|
|
underlying types. Unbless values and those in the same classes can |
497
|
|
|
|
|
|
|
be compared. |
498
|
|
|
|
|
|
|
|
499
|
|
|
|
|
|
|
|
500
|
|
|
|
|
|
|
|
501
|
|
|
|
|
|
|
=cut |
502
|
|
|
|
|
|
|
|
503
|
5
|
|
|
|
|
3784
|
use constant numRE => qr{^ ( |
504
|
|
|
|
|
|
|
[-+]? (?: (?: \d* \.? \d+ ) | |
505
|
|
|
|
|
|
|
(?: \d+ \.? \d* ) ) |
506
|
5
|
|
|
5
|
|
41
|
(?: [eE] [-+]? \d+)? ) }x; |
|
5
|
|
|
|
|
10
|
|
507
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
sub isnum(;$) { |
509
|
0
|
0
|
|
0
|
0
|
|
local $_ = @_ ? $_[0] : $_; |
510
|
0
|
0
|
|
|
|
|
return undef unless defined $_; |
511
|
|
|
|
|
|
|
#my $numRE = numRE; |
512
|
0
|
0
|
|
|
|
|
m{&numRE} ? 1 : 0; |
513
|
|
|
|
|
|
|
} |
514
|
|
|
|
|
|
|
|
515
|
|
|
|
|
|
|
|
516
|
|
|
|
|
|
|
sub Cmp($$;$); |
517
|
0
|
|
|
0
|
0
|
|
sub Cmp ($$;$) { my $r=0; |
518
|
0
|
|
|
|
|
|
my ($a, $b) = @_; |
519
|
0
|
0
|
|
|
|
|
my $dbg = @_==3 ? $_[2] : undef; |
520
|
0
|
0
|
|
|
|
|
require P if $dbg; |
521
|
0
|
0
|
0
|
|
|
|
return undef unless defined $a && defined $b; |
522
|
0
|
|
|
|
|
|
my ($ra, $rb) = (ref $a, ref $b); |
523
|
0
|
|
|
|
|
|
my ($ta, $tb) = (typ $a, typ $b); |
524
|
0
|
0
|
0
|
|
|
|
return undef unless defined $ra && defined $rb; |
525
|
0
|
|
|
|
|
|
my ($dta, $dtb) = (defined $ta, defined $tb); |
526
|
0
|
0
|
0
|
|
|
|
return undef unless $dta && $dtb; |
527
|
0
|
0
|
|
|
|
|
do { my $out = P::P("a='%s', b='%s'; ", $a, $b); |
|
0
|
|
|
|
|
|
|
528
|
0
|
|
|
|
|
|
$out .= P::P("ta='%s', tb='%s'; ", $ta, $tb); |
529
|
0
|
|
|
|
|
|
$out .= P::P("ra='%s', rb='%s'; ", $ra, $rb); |
530
|
0
|
|
|
|
|
|
P::Pe("%s", $out) } if $dbg; |
531
|
|
|
|
|
|
|
|
532
|
0
|
0
|
|
|
|
|
return undef if $ta ne $tb; |
533
|
|
|
|
|
|
|
|
534
|
0
|
0
|
0
|
|
|
|
unless ($ta || $tb) { # do val processing if both are vals |
535
|
|
|
|
|
|
|
# handle values, nums if nums, else as strings |
536
|
0
|
0
|
0
|
|
|
|
$r = isnum($a) && isnum($b) |
537
|
|
|
|
|
|
|
? $a <=> $b |
538
|
|
|
|
|
|
|
: $a cmp $b; |
539
|
0
|
0
|
|
|
|
|
P::Pe("isnum(a)=%s, isnum(b)=%s, r=%s", isnum($a), isnum($b), $r) if $dbg; |
540
|
0
|
|
|
|
|
|
return $r |
541
|
|
|
|
|
|
|
} |
542
|
|
|
|
|
|
|
|
543
|
|
|
|
|
|
|
# then handle unequal type references |
544
|
0
|
0
|
|
|
|
|
if ($dta ^ $dtb) { # one defined |
545
|
0
|
|
|
|
|
|
return (undef, 1); |
546
|
|
|
|
|
|
|
} |
547
|
0
|
0
|
0
|
|
|
|
unless ($dta && $dtb) { return (undef, 2) } #either undef |
|
0
|
|
|
|
|
|
|
548
|
|
|
|
|
|
|
|
549
|
|
|
|
|
|
|
# now, either do same thing again, or handle differing classes |
550
|
|
|
|
|
|
|
# the no-class on either implies no type-ref on either & is handled above |
551
|
0
|
|
|
|
|
|
my ($dra, $drb) = (defined $ra, defined $rb); |
552
|
0
|
0
|
0
|
|
|
|
if ($dra ^ $drb) { return (undef, 4) } |
|
0
|
0
|
0
|
|
|
|
|
553
|
0
|
|
|
|
|
|
elsif ($dra && $drb && $ra ne $rb) { return (undef, 5) } |
554
|
|
|
|
|
|
|
|
555
|
|
|
|
|
|
|
# now start comparing references: dereference and call Cmp again |
556
|
0
|
0
|
|
|
|
|
if ($ta eq SCALAR) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
557
|
0
|
|
|
|
|
|
return Cmp($$a, $$b, $dbg) } |
558
|
|
|
|
|
|
|
elsif ($ta eq ARRAY) { |
559
|
|
|
|
|
|
|
|
560
|
0
|
0
|
|
|
|
|
P::Pe("len of array a vs. b: (%s <=> %s)", 0+@$a, 0+@$b) if $dbg; |
561
|
0
|
0
|
|
|
|
|
return $r if $r = @$a <=> @$b; |
562
|
|
|
|
|
|
|
|
563
|
|
|
|
|
|
|
# for each member, compare them using Cmp |
564
|
0
|
|
|
|
|
|
for (my $i=0; $i < 0+@$a; ++$i) { |
565
|
0
|
0
|
|
|
|
|
P::Pe("a->[$i] Cmp b->[$i]...\x83") if $dbg; |
566
|
|
|
|
|
|
|
|
567
|
0
|
|
|
|
|
|
$r = Cmp($a->[$i], $b->[$i], $dbg); |
568
|
|
|
|
|
|
|
|
569
|
0
|
0
|
|
|
|
|
P::Pe("%s Cmp %s, r=%s", $a->[$i], $b->[$i], $r) if $dbg; |
570
|
0
|
0
|
|
|
|
|
return $r if $r; |
571
|
|
|
|
|
|
|
} |
572
|
0
|
|
|
|
|
|
return 0; # arrays are equal |
573
|
|
|
|
|
|
|
} elsif ($ta eq HASH) { |
574
|
0
|
|
|
|
|
|
my @ka = sort keys %$a; |
575
|
0
|
|
|
|
|
|
my @kb = sort keys %$b; |
576
|
0
|
|
|
|
|
|
$r = Cmp(0+@ka, 0+@kb, $dbg); |
577
|
0
|
0
|
|
|
|
|
P::Pe("Cmp #keys a(%s) b(%s), in hashes: r=%s", 0+@ka, 0+@kb, $r) if $dbg; |
578
|
0
|
0
|
|
|
|
|
return $r if $r; |
579
|
|
|
|
|
|
|
|
580
|
0
|
|
|
|
|
|
$r = Cmp(\@ka, \@kb, $dbg); |
581
|
0
|
0
|
|
|
|
|
P::Pe("Cmp keys of hash: r=%s", $r) if $dbg; |
582
|
0
|
0
|
|
|
|
|
return $r if $r; |
583
|
|
|
|
|
|
|
|
584
|
0
|
|
|
|
|
|
my @va = map {$a->{$_}} @ka; |
|
0
|
|
|
|
|
|
|
585
|
0
|
|
|
|
|
|
my @vb = map {$b->{$_}} @kb; |
|
0
|
|
|
|
|
|
|
586
|
0
|
|
|
|
|
|
$r = Cmp(\@va, \@vb, $dbg); |
587
|
0
|
0
|
|
|
|
|
P::Pe("Cmp values for each key, r=%s", $r) if $dbg; |
588
|
0
|
|
|
|
|
|
return $r; |
589
|
|
|
|
|
|
|
} else { |
590
|
0
|
0
|
|
|
|
|
P::Pe("no comparison for type %s, ref %s", $ta, $ra) if $dbg; |
591
|
0
|
|
|
|
|
|
return (undef,6); ## unimplemented comparison |
592
|
|
|
|
|
|
|
} |
593
|
|
|
|
|
|
|
} |
594
|
5
|
|
|
5
|
|
40
|
use Xporter; |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
24
|
|
595
|
|
|
|
|
|
|
|
596
|
|
|
|
|
|
|
|
597
|
|
|
|
|
|
|
1} |
598
|
|
|
|
|
|
|
|
599
|
|
|
|
|
|
|
=head1 NOTE on INCLUDING OPTIONAL (EXPORT_OK) FUNCTIONS |
600
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
Importing optional functions B cancel default imports |
602
|
|
|
|
|
|
|
as this module uses L. To dselect default exports, add |
603
|
|
|
|
|
|
|
'C<->' (I or I) at the beginning of argument list to |
604
|
|
|
|
|
|
|
C as in C |
605
|
|
|
|
|
|
|
See L for more details. |
606
|
|
|
|
|
|
|
|
607
|
|
|
|
|
|
|
=back |
608
|
|
|
|
|
|
|
|
609
|
|
|
|
|
|
|
=head3 COMPATIBILITY NOTE: with Perl 5.12.5 and earlier |
610
|
|
|
|
|
|
|
|
611
|
|
|
|
|
|
|
=over |
612
|
|
|
|
|
|
|
|
613
|
|
|
|
|
|
|
In order for earlier perls to parse things correctly parentheses are needed |
614
|
|
|
|
|
|
|
for two or more arguments after a B test verb. |
615
|
|
|
|
|
|
|
|
616
|
|
|
|
|
|
|
=cut |
617
|
|
|
|
|
|
|
|
618
|
|
|
|
|
|
|
# vim: ts=2 sw=2 sts=2 |