line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Carp::Ensure; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Copyright 2002 Stefan Merten |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify |
6
|
|
|
|
|
|
|
# it under the terms of the GNU General Public License as published by |
7
|
|
|
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or |
8
|
|
|
|
|
|
|
# (at your option) any later version. |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful, |
11
|
|
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
|
|
|
|
# GNU General Public License for more details. |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License |
16
|
|
|
|
|
|
|
# along with this program; if not, write to the Free Software |
17
|
|
|
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
1
|
|
12227
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
20
|
1
|
|
|
1
|
|
7
|
use vars qw( $VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
72
|
|
21
|
|
|
|
|
|
|
|
22
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
655
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
require Exporter; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
$VERSION = '$Name: Carp_Ensure_1_23 $' =~ /_(\d+)_(\d+)\b/ && sprintf("%d.%02d", $1 - 1, $2); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
@ISA = qw( Exporter ); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
############################################################################### |
31
|
|
|
|
|
|
|
# The idea of the following is shamelessly stolen from `Carp::Assert' |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
@EXPORT = qw( ensure DEBUG ); |
34
|
|
|
|
|
|
|
@EXPORT_OK = qw( is_a ); |
35
|
|
|
|
|
|
|
$EXPORT_TAGS{NDEBUG} = $EXPORT_TAGS{DEBUG} = [ @EXPORT ]; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub REAL_DEBUG() { 1 } |
38
|
|
|
|
|
|
|
sub NDEBUG() { 0 } |
39
|
0
|
|
|
0
|
0
|
0
|
sub noop { undef } |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Export the proper DEBUG flag according to if :NDEBUG is set. |
42
|
|
|
|
|
|
|
# Also export noop versions of our routines if NDEBUG |
43
|
|
|
|
|
|
|
sub import($@) { |
44
|
1
|
|
|
1
|
|
10
|
my( $cls, @syms ) = @_; |
45
|
|
|
|
|
|
|
|
46
|
1
|
50
|
33
|
|
|
3
|
if(scalar(grep{ $_ eq ':NDEBUG' }(@syms)) || |
|
2
|
50
|
|
|
|
15
|
|
47
|
|
|
|
|
|
|
(exists($ENV{PERL_NDEBUG}) ? $ENV{PERL_NDEBUG} : $ENV{'NDEBUG'})) { |
48
|
0
|
|
|
|
|
0
|
my $dst = caller(); |
49
|
0
|
|
|
|
|
0
|
foreach ( @{$EXPORT_TAGS{NDEBUG}} ) { |
|
0
|
|
|
|
|
0
|
|
50
|
1
|
|
|
1
|
|
6
|
no strict 'refs'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
12614
|
|
51
|
0
|
0
|
|
|
|
0
|
*{$dst . '::' . $_} = $_ eq 'DEBUG' ? \&NDEBUG : \&noop; |
|
0
|
|
|
|
|
0
|
|
52
|
|
|
|
|
|
|
} |
53
|
0
|
|
|
|
|
0
|
Carp::Ensure->export_to_level(1, $cls, grep{ $_ ne ':NDEBUG' }(@syms)); |
|
0
|
|
|
|
|
0
|
|
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
else { |
56
|
1
|
|
|
|
|
4
|
*DEBUG = *REAL_DEBUG; |
57
|
1
|
|
|
|
|
17404
|
Carp::Ensure->export_to_level(1, $cls, @syms); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub unimport($@) { |
62
|
0
|
|
|
0
|
|
0
|
my( $cls, @syms ) = @_; |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
0
|
*DEBUG = *NDEBUG; |
65
|
0
|
|
|
|
|
0
|
import($cls, ':NDEBUG', @syms); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# End of stolen idea |
69
|
|
|
|
|
|
|
############################################################################### |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 NAME |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Carp::Ensure - Ensure a value is of the expected type |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 SYNOPSIS |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
use Carp::Ensure( qw( is_a ) ); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
ensure('string', "Some arbitrary string") if DEBUG; |
80
|
|
|
|
|
|
|
ensure('@integer', 1, 2, 3) if DEBUG; |
81
|
|
|
|
|
|
|
ensure('@\integer', \1, \2, \3) if DEBUG; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
my %word2Int = ( one => 1, two => 2, three => 3 ); |
84
|
|
|
|
|
|
|
my @ints = values(%word2Int); |
85
|
|
|
|
|
|
|
my @wordsInts = ( keys(%word2Int), @ints ); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
ensure('\@integer', \@ints) if DEBUG; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
ensure('@word|integer', %word2Int) if DEBUG; |
90
|
|
|
|
|
|
|
ensure('%word=>integer', %word2Int) if DEBUG; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
die("Unexpected type") |
93
|
|
|
|
|
|
|
unless is_a('@word|integer', @wordsInts); |
94
|
|
|
|
|
|
|
die("Unexpected type") |
95
|
|
|
|
|
|
|
unless is_a('@\@word|integer', \@wordsInts, [ "four", 4 ]); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# Receives a string, a `Mail::Internet' object, a reference to a hash mapping |
98
|
|
|
|
|
|
|
# strings to integers |
99
|
|
|
|
|
|
|
sub someSub($$%) { |
100
|
|
|
|
|
|
|
ensure([ qw( string Mail::Internet HASH %string=>integer ) ], \@_) if DEBUG; |
101
|
|
|
|
|
|
|
my( $string, $object, %hash ) = @_; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# ... |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 DESCRIPTION |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Most of the time it's a nice feature, that Perl has no really strict type |
109
|
|
|
|
|
|
|
checking as in C++. However, sometimes you want to ensure, that you subs |
110
|
|
|
|
|
|
|
actually get the type of arguments they expect. Or they return what you expect. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
That is where B may be useful. You can check every value whether |
113
|
|
|
|
|
|
|
it has the type you expect. You may fine tune the type checking from very |
114
|
|
|
|
|
|
|
coarse checking like defined vs. undefined to very detailed checks which check |
115
|
|
|
|
|
|
|
even the keys and values of a hash. In most places you may give alternative |
116
|
|
|
|
|
|
|
types so for instance a parameter can easily be checked to be of a certain type |
117
|
|
|
|
|
|
|
or undefined. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
There are checking routines for a few commonly used base types included and you |
120
|
|
|
|
|
|
|
may add your own checking routines so you can check for the types specific to |
121
|
|
|
|
|
|
|
your program. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
The types are described by a simple grammar which picks up as much as possible |
124
|
|
|
|
|
|
|
you already know from the Perl type system. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=cut |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
############################################################################### |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 FUNCTIONS |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=over 4 |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=item B |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
use Carp::Ensure; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
ensure("some_type", $value) if DEBUG; |
139
|
|
|
|
|
|
|
ensure("@value_type", @array) if DEBUG; |
140
|
|
|
|
|
|
|
ensure("%key_type=>value_type", %hash) if DEBUG; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
ensure([ qw( type1 type2 ... ) ], [ $value1, $value2, ... ]) if DEBUG; |
143
|
|
|
|
|
|
|
ensure([ qw( type1 type2 ... ) ], \@_) if DEBUG; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Checks whether the types described in the first argument are matched by the |
146
|
|
|
|
|
|
|
values given in the following arguments. If the values match the type B |
147
|
|
|
|
|
|
|
returns an aribtrary value. If a value doesn't match the specified type, |
148
|
|
|
|
|
|
|
B Bes with an approriate error message and thus stops |
149
|
|
|
|
|
|
|
the program. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
If the first argument is a string, it describes the type of the remaining |
152
|
|
|
|
|
|
|
arguments which may be arbitrary many (including none). This is useful for list |
153
|
|
|
|
|
|
|
types (i.e. arrays and hashes) and to check single values. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
If the first argument is a reference to an array, the second argument must be a |
156
|
|
|
|
|
|
|
reference to an array, too. In this calling scheme the first array describes |
157
|
|
|
|
|
|
|
the types contained in the second argument. It is particularly useful to check |
158
|
|
|
|
|
|
|
the argument list of a sub. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Care is taken to not change the second argument in any way. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Note, that usually ot only makes sense when the last of the described types |
163
|
|
|
|
|
|
|
checks for a list type. This is because in Perl a list type sucks up all the |
164
|
|
|
|
|
|
|
remaining values. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
See L<"TYPE GRAMMAR"> for how the types are described. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
The C concept is taken from L where it is explained in |
169
|
|
|
|
|
|
|
detail (particularly in L. Actually the |
170
|
|
|
|
|
|
|
B value is probably shared between L and this module. So |
171
|
|
|
|
|
|
|
take care when enabling it in one and disabling it in the other package C |
172
|
|
|
|
|
|
|
In short: If you say C |
173
|
|
|
|
|
|
|
works as expected. If you say C then the whole call is |
174
|
|
|
|
|
|
|
compiled away from the program and has no impact on efficiency. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=cut |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub ensure($@) { |
179
|
|
|
|
|
|
|
# Call it with our arguments to save a copy |
180
|
202
|
|
|
202
|
1
|
16298
|
my $err = &_is_not; |
181
|
202
|
100
|
|
|
|
23547
|
confess("ensure: $err") |
182
|
|
|
|
|
|
|
if $err; |
183
|
107
|
|
|
|
|
781
|
return !undef; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
############################################################################### |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=item B |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
# Both are possible |
191
|
|
|
|
|
|
|
use Carp::Ensure( qw( :DEBUG is_a ) ); |
192
|
|
|
|
|
|
|
use Carp::Ensure( qw( :NDEBUG is_a ) ); |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
$is_of_type = is_a("some_type", $value); |
195
|
|
|
|
|
|
|
$is_of_type = is_a("@value_type", @array); |
196
|
|
|
|
|
|
|
$is_of_type = is_a("%key_type=>value_type", %hash); |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
$is_of_type = is_a([ qw( type1 type2 ... ) ], [ $value1, $value2, ... ]); |
199
|
|
|
|
|
|
|
$is_of_type = is_a([ qw( type1 type2 ... ) ], \@_); |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
This does the same as B, however, it only returns true or false instead |
202
|
|
|
|
|
|
|
of Bing. You can use this to check types of values without |
203
|
|
|
|
|
|
|
immediately stopping the program on failure or to build your own testing subs |
204
|
|
|
|
|
|
|
like this: |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
sub Carp::Ensure::is_a_word1empty { Carp::Ensure::is_a('word|empty', ${shift()}) } |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
If a false value is returned I<$@> is set to an error message. Otherwise I<$@> |
209
|
|
|
|
|
|
|
is undefined. |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=cut |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
sub is_a($@) { |
214
|
|
|
|
|
|
|
# Call it with our arguments to save a copy |
215
|
31
|
|
|
31
|
1
|
684
|
$@ = &_is_not; |
216
|
31
|
|
|
|
|
179
|
return !$@; |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
############################################################################### |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
my $ErrTpCall = 1; |
222
|
|
|
|
|
|
|
my $ErrTpDscr = 2; |
223
|
|
|
|
|
|
|
my $ErrTpType = 3; |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
# This does the real work. Returns an error message or undef. |
226
|
|
|
|
|
|
|
sub _is_not($@) { |
227
|
233
|
|
|
233
|
|
350
|
my $tp = shift(); |
228
|
|
|
|
|
|
|
|
229
|
233
|
|
|
|
|
228
|
my $err; |
230
|
233
|
100
|
|
|
|
882
|
unless(defined($tp)) |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
231
|
1
|
|
|
|
|
4
|
{ $err = "$ErrTpCall Undefined first argument"; } |
232
|
|
|
|
|
|
|
elsif(!ref($tp)) { |
233
|
222
|
|
|
|
|
277
|
my $cTp = $tp; |
234
|
222
|
|
|
|
|
516
|
$cTp =~ s/\s+//g; |
235
|
222
|
|
|
|
|
527
|
$err = _type($cTp, 0, \@_); |
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
elsif(ref($tp) eq "ARRAY") { |
238
|
9
|
|
|
|
|
12
|
my $vals = shift(); |
239
|
9
|
100
|
|
|
|
27
|
if(@_) |
|
|
100
|
|
|
|
|
|
240
|
1
|
|
|
|
|
3
|
{ $err = "$ErrTpCall Too many arguments"; } |
241
|
|
|
|
|
|
|
elsif(ref($vals) ne "ARRAY") |
242
|
1
|
|
|
|
|
3
|
{ $err = "$ErrTpCall Second argument must be an array reference, too"; } |
243
|
|
|
|
|
|
|
else { |
244
|
7
|
|
100
|
|
|
39
|
for(my $i = 0; !$err && $i < @$tp; $i++) { |
245
|
12
|
100
|
66
|
|
|
56
|
if(!defined($tp->[$i]) || ref($tp->[$i])) |
246
|
1
|
|
|
|
|
7
|
{ $err = "$ErrTpCall Not a string element at index $i of first argument"; } |
247
|
|
|
|
|
|
|
else { |
248
|
11
|
|
|
|
|
12
|
my $cTp = $tp->[$i]; |
249
|
11
|
|
|
|
|
18
|
$cTp =~ s/\s+//g; |
250
|
11
|
|
|
|
|
22
|
$err = _type($cTp, $i, $vals); |
251
|
|
|
|
|
|
|
} |
252
|
|
|
|
|
|
|
} |
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
else |
256
|
1
|
|
|
|
|
5
|
{ $err = "$ErrTpCall First argument must be a string or array reference"; } |
257
|
|
|
|
|
|
|
return undef |
258
|
233
|
100
|
|
|
|
691
|
unless $err; |
259
|
|
|
|
|
|
|
|
260
|
103
|
|
|
|
|
98
|
my $errTp; |
261
|
103
|
|
|
|
|
648
|
( $errTp, $err ) = $err =~ /^(\d+)(.*)$/; |
262
|
103
|
50
|
|
|
|
567
|
return "Invalid " . |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
263
|
|
|
|
|
|
|
($errTp == $ErrTpCall ? "call" : |
264
|
|
|
|
|
|
|
$errTp == $ErrTpDscr ? "description" : |
265
|
|
|
|
|
|
|
$errTp == $ErrTpType ? "type" : "unknown") . ":$err"; |
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
############################################################################### |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=back |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
=head1 TYPE GRAMMAR |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
You may create rather complex type descriptions from the following grammar. |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=head2 Lexical rules |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
Since whitespace is not relevant in the grammar, it may occur anywhere outside |
279
|
|
|
|
|
|
|
of identifiers. Actually any whitespace is removed before parsing the type |
280
|
|
|
|
|
|
|
description starts. |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
=head2 Grammar rules |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
=cut |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
# All subs implementing the grammar return an error message or `undef' if |
287
|
|
|
|
|
|
|
# everything worked. |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
=over 4 |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=item I := |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
I | I | I |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=cut |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
sub _type($$$ ) { |
298
|
294
|
|
|
294
|
|
488
|
my( $tp, $idx, $arr ) = @_; |
299
|
|
|
|
|
|
|
|
300
|
294
|
100
|
|
|
|
1523
|
if($tp =~ /^\@/) |
|
|
100
|
|
|
|
|
|
301
|
26
|
|
|
|
|
126
|
{ return _array($tp, $idx, $arr); } |
302
|
|
|
|
|
|
|
elsif($tp =~ /^\%/) |
303
|
13
|
|
|
|
|
30
|
{ return _hash($tp, $idx, $arr); } |
304
|
|
|
|
|
|
|
else |
305
|
255
|
|
|
|
|
872
|
{ return _alternative($tp, \$arr->[$idx]); } |
306
|
|
|
|
|
|
|
} |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
############################################################################### |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
=item I := |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
'C<%>' I 'C<=>>' I |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
=cut |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
sub _hash($$$ ) { |
317
|
13
|
|
|
13
|
|
16
|
my( $tp, $idx, $arr ) = @_; |
318
|
|
|
|
|
|
|
|
319
|
13
|
|
|
|
|
40
|
$tp =~ s/^\%//; |
320
|
13
|
100
|
|
|
|
194
|
return "$ErrTpDscr Missing `=>' in hash type `\%$tp'" |
321
|
|
|
|
|
|
|
unless $tp =~ /=>/; |
322
|
|
|
|
|
|
|
|
323
|
11
|
|
|
|
|
30
|
my( $keyTp, $valTp ) = ( $`, $' ); |
324
|
11
|
|
|
|
|
11
|
my $err; |
325
|
11
|
|
100
|
|
|
67
|
$err = _alternative($keyTp, \$arr->[$idx++]) || |
|
|
|
66
|
|
|
|
|
326
|
|
|
|
|
|
|
_alternative($valTp, \$arr->[$idx++]) |
327
|
|
|
|
|
|
|
while !$err && $idx < @$arr; |
328
|
11
|
|
|
|
|
96
|
return $err; |
329
|
|
|
|
|
|
|
} |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
############################################################################### |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
=item I := |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
'C<@>' I |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
=cut |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
sub _array($$$ ) { |
340
|
26
|
|
|
26
|
|
41
|
my( $tp, $idx, $arr ) = @_; |
341
|
|
|
|
|
|
|
|
342
|
26
|
|
|
|
|
84
|
$tp =~ s/^\@//; |
343
|
|
|
|
|
|
|
|
344
|
26
|
|
|
|
|
35
|
my $err; |
345
|
26
|
|
100
|
|
|
153
|
$err = _alternative($tp, \$arr->[$idx++]) |
346
|
|
|
|
|
|
|
while !$err && $idx < @$arr; |
347
|
26
|
|
|
|
|
257
|
return $err; |
348
|
|
|
|
|
|
|
} |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
############################################################################### |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
=item I := |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
I 'C<|>' I | I |
355
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
=cut |
357
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
sub _alternative($$) { |
359
|
373
|
|
|
373
|
|
783
|
my( $tp, $val ) = @_; |
360
|
|
|
|
|
|
|
|
361
|
373
|
100
|
|
|
|
1102
|
return _simple($tp, $val) |
362
|
|
|
|
|
|
|
unless $tp =~ /\|/; |
363
|
|
|
|
|
|
|
|
364
|
97
|
|
|
|
|
356
|
foreach my $alt ( split(/\|/, $tp) ) { |
365
|
177
|
|
|
|
|
312
|
my $err = _simple($alt, $val); |
366
|
|
|
|
|
|
|
return undef |
367
|
177
|
100
|
|
|
|
993
|
unless $err; |
368
|
|
|
|
|
|
|
|
369
|
101
|
|
|
|
|
344
|
my ( $errTp ) = $err =~ /^(\d+)/; |
370
|
101
|
100
|
|
|
|
408
|
return $err |
371
|
|
|
|
|
|
|
if $errTp < $ErrTpType; |
372
|
|
|
|
|
|
|
} |
373
|
18
|
|
|
|
|
94
|
return "$ErrTpType `" . $$val . "' is not one of `$tp'"; |
374
|
|
|
|
|
|
|
} |
375
|
|
|
|
|
|
|
|
376
|
|
|
|
|
|
|
############################################################################### |
377
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
=item I := |
379
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
I | I | I | I |
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
=item I := |
383
|
|
|
|
|
|
|
|
384
|
|
|
|
|
|
|
'C<\>' I | I | I |
385
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
Note: Take care with the C<\>. Even in a string using single quotes a directly |
387
|
|
|
|
|
|
|
following backslash quotes a backslash! Whitespace between subsequent |
388
|
|
|
|
|
|
|
backslashes simplifies things greatly. |
389
|
|
|
|
|
|
|
|
390
|
|
|
|
|
|
|
=cut |
391
|
|
|
|
|
|
|
|
392
|
|
|
|
|
|
|
my @referenceSs = qw( HASH ARRAY CODE GLOB ); |
393
|
|
|
|
|
|
|
|
394
|
|
|
|
|
|
|
sub _reference($$) { |
395
|
171
|
|
|
171
|
|
263
|
my( $tp, $val ) = @_; |
396
|
|
|
|
|
|
|
|
397
|
171
|
100
|
|
|
|
212
|
if(grep{ $tp eq $_ }(@referenceSs)) |
|
684
|
100
|
|
|
|
1650
|
|
|
|
100
|
|
|
|
|
|
398
|
48
|
|
|
|
|
85
|
{ return _is_a($tp, $val); } |
399
|
|
|
|
|
|
|
elsif($tp =~ /^\^/) |
400
|
25
|
|
|
|
|
67
|
{ return _class($tp, $val); } |
401
|
|
|
|
|
|
|
elsif($tp =~ s/^\\//) { |
402
|
68
|
100
|
|
|
|
203
|
return "$ErrTpType `" . $$val . "' is not a reference" |
403
|
|
|
|
|
|
|
unless ref($val) eq "REF"; |
404
|
|
|
|
|
|
|
|
405
|
61
|
|
|
|
|
95
|
my $refTp = ref($$val); |
406
|
61
|
100
|
100
|
|
|
272
|
if($refTp eq "SCALAR" || $refTp eq "REF") |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
407
|
31
|
|
|
|
|
94
|
{ return _type($tp, 0, [ $$$val ]); } |
408
|
|
|
|
|
|
|
elsif($refTp eq "HASH") |
409
|
4
|
|
|
|
|
19
|
{ return _type($tp, 0, [ %$$val ]); } |
410
|
|
|
|
|
|
|
elsif($refTp eq "ARRAY") |
411
|
20
|
|
|
|
|
64
|
{ return _type($tp, 0, [ @$$val ]); } |
412
|
|
|
|
|
|
|
elsif($refTp eq "CODE") |
413
|
0
|
|
|
|
|
0
|
{ return _type($tp, 0, [ &$$val ]); } |
414
|
|
|
|
|
|
|
elsif($refTp eq "GLOB") |
415
|
1
|
|
|
|
|
5
|
{ return _type($tp, 0, [ *$$val ]); } |
416
|
|
|
|
|
|
|
else # object |
417
|
5
|
|
|
|
|
16
|
{ return _type($tp, 0, [ $$val ]); } |
418
|
|
|
|
|
|
|
} |
419
|
|
|
|
|
|
|
else |
420
|
30
|
|
|
|
|
60
|
{ return _object($tp, $val); } |
421
|
|
|
|
|
|
|
} |
422
|
|
|
|
|
|
|
|
423
|
|
|
|
|
|
|
############################################################################### |
424
|
|
|
|
|
|
|
|
425
|
|
|
|
|
|
|
=item I := |
426
|
|
|
|
|
|
|
|
427
|
|
|
|
|
|
|
I |
428
|
|
|
|
|
|
|
|
429
|
|
|
|
|
|
|
=cut |
430
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
sub _dynamic($$) { |
432
|
86
|
|
|
86
|
|
114
|
my( $tp, $val ) = @_; |
433
|
|
|
|
|
|
|
|
434
|
86
|
|
|
|
|
128
|
return _user($tp, $val); |
435
|
|
|
|
|
|
|
} |
436
|
|
|
|
|
|
|
|
437
|
|
|
|
|
|
|
############################################################################### |
438
|
|
|
|
|
|
|
|
439
|
|
|
|
|
|
|
=item I := |
440
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
'C' | 'C' | 'C' |
442
|
|
|
|
|
|
|
|
443
|
|
|
|
|
|
|
=cut |
444
|
|
|
|
|
|
|
|
445
|
|
|
|
|
|
|
my @specialSs = qw( undefined defined anything ); |
446
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
sub _special($$) { |
448
|
26
|
|
|
26
|
|
38
|
my( $tp, $val ) = @_; |
449
|
|
|
|
|
|
|
|
450
|
26
|
|
|
|
|
40
|
return _is_a($tp, $val); |
451
|
|
|
|
|
|
|
} |
452
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
############################################################################### |
454
|
|
|
|
|
|
|
|
455
|
|
|
|
|
|
|
=item I := |
456
|
|
|
|
|
|
|
|
457
|
|
|
|
|
|
|
'C' | 'C' | 'C' | 'C' | 'C' | 'C' | 'C' |
458
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
These common simple types are predefined. |
460
|
|
|
|
|
|
|
|
461
|
|
|
|
|
|
|
=cut |
462
|
|
|
|
|
|
|
|
463
|
|
|
|
|
|
|
my @scalarSs = qw( string word empty integer float boolean regex ); |
464
|
|
|
|
|
|
|
|
465
|
|
|
|
|
|
|
sub _scalar($$) { |
466
|
167
|
|
|
167
|
|
298
|
my( $tp, $val ) = @_; |
467
|
|
|
|
|
|
|
|
468
|
1169
|
|
|
|
|
2267
|
return "$ErrTpDscr Unknown scalar type `$tp'" |
469
|
167
|
50
|
|
|
|
270
|
unless grep{ $tp eq $_ }(@scalarSs); |
470
|
|
|
|
|
|
|
|
471
|
167
|
|
|
|
|
274
|
return _is_a($tp, $val); |
472
|
|
|
|
|
|
|
} |
473
|
|
|
|
|
|
|
|
474
|
|
|
|
|
|
|
############################################################################### |
475
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
=item I := |
477
|
|
|
|
|
|
|
|
478
|
|
|
|
|
|
|
'C<^>' I |
479
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
A value matching such a type is a name of a class (i.e. a string) represented |
481
|
|
|
|
|
|
|
by the name matching the regular expression I |
482
|
|
|
|
|
|
|
class is a superclass of the class given by the value. |
483
|
|
|
|
|
|
|
|
484
|
|
|
|
|
|
|
Thus the first parameter of a method which might be used static as well as with |
485
|
|
|
|
|
|
|
an object has a type of |
486
|
|
|
|
|
|
|
|
487
|
|
|
|
|
|
|
Some::Class|^Some::Class |
488
|
|
|
|
|
|
|
|
489
|
|
|
|
|
|
|
=cut |
490
|
|
|
|
|
|
|
|
491
|
|
|
|
|
|
|
sub _class($$) { |
492
|
25
|
|
|
25
|
|
36
|
my( $tp, $val ) = @_; |
493
|
|
|
|
|
|
|
|
494
|
25
|
|
|
|
|
47
|
$tp =~ /^\^/; |
495
|
25
|
|
|
|
|
47
|
my $cls = $'; |
496
|
25
|
100
|
100
|
|
|
110
|
return ref($val) eq "SCALAR" && eval { $$val->isa($cls) } ? undef : |
497
|
|
|
|
|
|
|
"$ErrTpType `" . $$val . "' is not of type `$tp'"; |
498
|
|
|
|
|
|
|
} |
499
|
|
|
|
|
|
|
|
500
|
|
|
|
|
|
|
############################################################################### |
501
|
|
|
|
|
|
|
|
502
|
|
|
|
|
|
|
=item I |
503
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
I^[A-Z]\w*(::\w+)*$/> |
505
|
|
|
|
|
|
|
|
506
|
|
|
|
|
|
|
The value is a object (i.e. a blessed reference) of the class represented by |
507
|
|
|
|
|
|
|
the name matching the regular expression. This may mean, that the class is a |
508
|
|
|
|
|
|
|
superclass of the object's class. |
509
|
|
|
|
|
|
|
|
510
|
|
|
|
|
|
|
=cut |
511
|
|
|
|
|
|
|
|
512
|
|
|
|
|
|
|
my $objectRe = '[A-Z]\w*(::\w+)*'; |
513
|
|
|
|
|
|
|
|
514
|
|
|
|
|
|
|
sub _object($$) { |
515
|
30
|
|
|
30
|
|
1948
|
my( $tp, $val ) = @_; |
516
|
|
|
|
|
|
|
|
517
|
30
|
100
|
100
|
|
|
400
|
return ref($val) eq "REF" && UNIVERSAL::isa($$val, $tp) ? undef : |
518
|
|
|
|
|
|
|
"$ErrTpType `" . $$val . "' is not of type `$tp'"; |
519
|
|
|
|
|
|
|
} |
520
|
|
|
|
|
|
|
|
521
|
|
|
|
|
|
|
############################################################################### |
522
|
|
|
|
|
|
|
|
523
|
|
|
|
|
|
|
=item I := |
524
|
|
|
|
|
|
|
|
525
|
|
|
|
|
|
|
I^[a-z]\w*$/> |
526
|
|
|
|
|
|
|
|
527
|
|
|
|
|
|
|
This might be a string I matching the regular expression. For this a |
528
|
|
|
|
|
|
|
sub |
529
|
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
CI |
531
|
|
|
|
|
|
|
|
532
|
|
|
|
|
|
|
must be defined. When checking a value for being a I, the sub is |
533
|
|
|
|
|
|
|
called with a single argument being a B(!) to the value it should |
534
|
|
|
|
|
|
|
check. This minimizes copying. The sub must return false if the referenced |
535
|
|
|
|
|
|
|
value is not of the desired type and a true value otherwise. See C for an |
536
|
|
|
|
|
|
|
example. |
537
|
|
|
|
|
|
|
|
538
|
|
|
|
|
|
|
=cut |
539
|
|
|
|
|
|
|
|
540
|
|
|
|
|
|
|
my $userRe = '[a-z]\w*'; |
541
|
|
|
|
|
|
|
|
542
|
|
|
|
|
|
|
sub _user($$) { |
543
|
86
|
|
|
86
|
|
103
|
my( $tp, $val ) = @_; |
544
|
|
|
|
|
|
|
|
545
|
86
|
|
|
|
|
150
|
return _is_a($tp, $val); |
546
|
|
|
|
|
|
|
} |
547
|
|
|
|
|
|
|
|
548
|
|
|
|
|
|
|
############################################################################### |
549
|
|
|
|
|
|
|
|
550
|
|
|
|
|
|
|
sub _simple($$) { |
551
|
453
|
|
|
453
|
|
677
|
my( $tp, $val ) = @_; |
552
|
|
|
|
|
|
|
|
553
|
453
|
100
|
100
|
|
|
810
|
if(grep{ $tp eq $_ }(@scalarSs)) |
|
3171
|
100
|
100
|
|
|
6330
|
|
|
|
100
|
100
|
|
|
|
|
|
|
100
|
|
|
|
|
|
554
|
167
|
|
|
|
|
273
|
{ return _scalar($tp, $val); } |
|
858
|
|
|
|
|
2747
|
|
555
|
|
|
|
|
|
|
elsif(grep{ $tp eq $_ }(@specialSs)) |
556
|
26
|
|
|
|
|
50
|
{ return _special($tp, $val); } |
557
|
|
|
|
|
|
|
elsif($tp =~ /^$userRe$/) |
558
|
86
|
|
|
|
|
160
|
{ return _dynamic($tp, $val); } |
|
696
|
|
|
|
|
2562
|
|
559
|
|
|
|
|
|
|
elsif(scalar(grep{ $tp eq $_ }(@referenceSs)) || |
560
|
|
|
|
|
|
|
$tp =~ /^\\/ || $tp =~ /^$objectRe$/ || $tp =~ /^\^$objectRe$/) |
561
|
171
|
|
|
|
|
346
|
{ return _reference($tp, $val); } |
562
|
|
|
|
|
|
|
else |
563
|
3
|
|
|
|
|
16
|
{ return "$ErrTpDscr Unparsable simple type `$tp'"; } |
564
|
|
|
|
|
|
|
} |
565
|
|
|
|
|
|
|
|
566
|
|
|
|
|
|
|
############################################################################### |
567
|
|
|
|
|
|
|
|
568
|
|
|
|
|
|
|
=back |
569
|
|
|
|
|
|
|
|
570
|
|
|
|
|
|
|
=head2 Terminal symbols |
571
|
|
|
|
|
|
|
|
572
|
|
|
|
|
|
|
The terminal symbols have the following meaning: |
573
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
=cut |
575
|
|
|
|
|
|
|
|
576
|
|
|
|
|
|
|
############################################################################### |
577
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
# Calls the `is_a_$tp'(`$val') sub. |
579
|
|
|
|
|
|
|
sub _is_a($$) { |
580
|
327
|
|
|
327
|
|
629
|
my( $tp, $val ) = @_; |
581
|
|
|
|
|
|
|
|
582
|
327
|
|
|
|
|
478
|
my $sub = "is_a_$tp"; |
583
|
1
|
|
|
1
|
|
10
|
no strict 'refs'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
874
|
|
584
|
327
|
100
|
|
|
|
817
|
return "$ErrTpDscr No user defined test `Carp::Ensure::$sub'" |
585
|
|
|
|
|
|
|
unless defined(&$sub); |
586
|
|
|
|
|
|
|
|
587
|
326
|
100
|
|
|
|
784
|
return &$sub($val) ? undef : |
588
|
|
|
|
|
|
|
"$ErrTpType `" . $$val . "' is not of type `$tp'"; |
589
|
|
|
|
|
|
|
} |
590
|
|
|
|
|
|
|
|
591
|
|
|
|
|
|
|
############################################################################### |
592
|
|
|
|
|
|
|
|
593
|
|
|
|
|
|
|
=over 4 |
594
|
|
|
|
|
|
|
|
595
|
|
|
|
|
|
|
=item C |
596
|
|
|
|
|
|
|
|
597
|
|
|
|
|
|
|
The value is a reference(!) to a hash with arbitrary keys and values. Use this |
598
|
|
|
|
|
|
|
if you don't want to check the hash content. |
599
|
|
|
|
|
|
|
|
600
|
|
|
|
|
|
|
=cut |
601
|
|
|
|
|
|
|
|
602
|
|
|
|
|
|
|
sub is_a_HASH($ ) { |
603
|
9
|
|
|
9
|
0
|
12
|
my( $r ) = @_; |
604
|
|
|
|
|
|
|
|
605
|
9
|
|
100
|
|
|
88
|
return ref($r) eq "REF" && ref($$r) eq "HASH"; |
606
|
|
|
|
|
|
|
} |
607
|
|
|
|
|
|
|
|
608
|
|
|
|
|
|
|
############################################################################### |
609
|
|
|
|
|
|
|
|
610
|
|
|
|
|
|
|
=item C |
611
|
|
|
|
|
|
|
|
612
|
|
|
|
|
|
|
The value is a reference(!) to an array with arbitrary content. Use this if you |
613
|
|
|
|
|
|
|
don't want to check the array content. |
614
|
|
|
|
|
|
|
|
615
|
|
|
|
|
|
|
=cut |
616
|
|
|
|
|
|
|
|
617
|
|
|
|
|
|
|
sub is_a_ARRAY($ ) { |
618
|
19
|
|
|
19
|
0
|
22
|
my( $r ) = @_; |
619
|
|
|
|
|
|
|
|
620
|
19
|
|
100
|
|
|
169
|
return ref($r) eq "REF" && ref($$r) eq "ARRAY"; |
621
|
|
|
|
|
|
|
} |
622
|
|
|
|
|
|
|
|
623
|
|
|
|
|
|
|
############################################################################### |
624
|
|
|
|
|
|
|
|
625
|
|
|
|
|
|
|
=item C |
626
|
|
|
|
|
|
|
|
627
|
|
|
|
|
|
|
The value is a reference to some code. This may be an anonymous or a named sub. |
628
|
|
|
|
|
|
|
|
629
|
|
|
|
|
|
|
=cut |
630
|
|
|
|
|
|
|
|
631
|
|
|
|
|
|
|
sub is_a_CODE($ ) { |
632
|
8
|
|
|
8
|
0
|
11
|
my( $r ) = @_; |
633
|
|
|
|
|
|
|
|
634
|
8
|
|
100
|
|
|
85
|
return ref($r) eq "REF" && ref($$r) eq "CODE"; |
635
|
|
|
|
|
|
|
} |
636
|
|
|
|
|
|
|
|
637
|
|
|
|
|
|
|
############################################################################### |
638
|
|
|
|
|
|
|
|
639
|
|
|
|
|
|
|
=item C |
640
|
|
|
|
|
|
|
|
641
|
|
|
|
|
|
|
The value is a GLOB. |
642
|
|
|
|
|
|
|
|
643
|
|
|
|
|
|
|
=cut |
644
|
|
|
|
|
|
|
|
645
|
|
|
|
|
|
|
sub is_a_GLOB($ ) { |
646
|
12
|
|
|
12
|
0
|
15
|
my( $r ) = @_; |
647
|
|
|
|
|
|
|
|
648
|
12
|
|
|
|
|
82
|
return ref($r) eq "GLOB"; |
649
|
|
|
|
|
|
|
} |
650
|
|
|
|
|
|
|
|
651
|
|
|
|
|
|
|
############################################################################### |
652
|
|
|
|
|
|
|
|
653
|
|
|
|
|
|
|
=item C |
654
|
|
|
|
|
|
|
|
655
|
|
|
|
|
|
|
Only the undefined value is permitted. Often used as one part of an |
656
|
|
|
|
|
|
|
alternative. Missing optional arguments of a sub are undefined, also. |
657
|
|
|
|
|
|
|
|
658
|
|
|
|
|
|
|
=cut |
659
|
|
|
|
|
|
|
|
660
|
|
|
|
|
|
|
sub is_a_undefined($ ) { |
661
|
17
|
|
|
17
|
0
|
21
|
my( $r ) = @_; |
662
|
|
|
|
|
|
|
|
663
|
17
|
|
100
|
|
|
137
|
return ref($r) eq "SCALAR" && !defined($$r); |
664
|
|
|
|
|
|
|
} |
665
|
|
|
|
|
|
|
|
666
|
|
|
|
|
|
|
############################################################################### |
667
|
|
|
|
|
|
|
|
668
|
|
|
|
|
|
|
=item C |
669
|
|
|
|
|
|
|
|
670
|
|
|
|
|
|
|
The value only needs to be defined. |
671
|
|
|
|
|
|
|
|
672
|
|
|
|
|
|
|
=cut |
673
|
|
|
|
|
|
|
|
674
|
|
|
|
|
|
|
sub is_a_defined($ ) { |
675
|
4
|
|
|
4
|
0
|
6
|
my( $r ) = @_; |
676
|
|
|
|
|
|
|
|
677
|
4
|
|
|
|
|
20
|
return defined($$r); |
678
|
|
|
|
|
|
|
} |
679
|
|
|
|
|
|
|
|
680
|
|
|
|
|
|
|
############################################################################### |
681
|
|
|
|
|
|
|
|
682
|
|
|
|
|
|
|
=item C |
683
|
|
|
|
|
|
|
|
684
|
|
|
|
|
|
|
Actually not a test since anything is permitted. |
685
|
|
|
|
|
|
|
|
686
|
|
|
|
|
|
|
=cut |
687
|
|
|
|
|
|
|
|
688
|
|
|
|
|
|
|
sub is_a_anything($ ) { |
689
|
5
|
|
|
5
|
0
|
30
|
return !undef; |
690
|
|
|
|
|
|
|
} |
691
|
|
|
|
|
|
|
|
692
|
|
|
|
|
|
|
############################################################################### |
693
|
|
|
|
|
|
|
|
694
|
|
|
|
|
|
|
=item C |
695
|
|
|
|
|
|
|
|
696
|
|
|
|
|
|
|
An arbitrary string. |
697
|
|
|
|
|
|
|
|
698
|
|
|
|
|
|
|
=cut |
699
|
|
|
|
|
|
|
|
700
|
|
|
|
|
|
|
sub is_a_string($ ) { |
701
|
3
|
|
|
3
|
0
|
4
|
my( $r ) = @_; |
702
|
|
|
|
|
|
|
|
703
|
3
|
|
|
|
|
25
|
return ref($r) eq "SCALAR"; |
704
|
|
|
|
|
|
|
} |
705
|
|
|
|
|
|
|
|
706
|
|
|
|
|
|
|
############################################################################### |
707
|
|
|
|
|
|
|
|
708
|
|
|
|
|
|
|
=item C |
709
|
|
|
|
|
|
|
|
710
|
|
|
|
|
|
|
A string matching C. |
711
|
|
|
|
|
|
|
|
712
|
|
|
|
|
|
|
=cut |
713
|
|
|
|
|
|
|
|
714
|
|
|
|
|
|
|
sub is_a_word($ ) { |
715
|
73
|
|
|
73
|
0
|
81
|
my( $r ) = @_; |
716
|
|
|
|
|
|
|
|
717
|
73
|
|
100
|
|
|
688
|
return ref($r) eq "SCALAR" && $$r =~ /^\w+$/; |
718
|
|
|
|
|
|
|
} |
719
|
|
|
|
|
|
|
|
720
|
|
|
|
|
|
|
############################################################################### |
721
|
|
|
|
|
|
|
|
722
|
|
|
|
|
|
|
=item C |
723
|
|
|
|
|
|
|
|
724
|
|
|
|
|
|
|
An empty string. |
725
|
|
|
|
|
|
|
|
726
|
|
|
|
|
|
|
=cut |
727
|
|
|
|
|
|
|
|
728
|
|
|
|
|
|
|
sub is_a_empty($ ) { |
729
|
18
|
|
|
18
|
0
|
24
|
my( $r ) = @_; |
730
|
|
|
|
|
|
|
|
731
|
18
|
|
100
|
|
|
177
|
return ref($r) eq "SCALAR" && defined($$r) && $$r eq ""; |
732
|
|
|
|
|
|
|
} |
733
|
|
|
|
|
|
|
|
734
|
|
|
|
|
|
|
############################################################################### |
735
|
|
|
|
|
|
|
|
736
|
|
|
|
|
|
|
=item C |
737
|
|
|
|
|
|
|
|
738
|
|
|
|
|
|
|
An integer. |
739
|
|
|
|
|
|
|
|
740
|
|
|
|
|
|
|
=cut |
741
|
|
|
|
|
|
|
|
742
|
|
|
|
|
|
|
sub is_a_integer($ ) { |
743
|
28
|
|
|
28
|
0
|
33
|
my( $r ) = @_; |
744
|
|
|
|
|
|
|
|
745
|
28
|
|
100
|
|
|
323
|
return ref($r) eq "SCALAR" && $$r =~/^[-+]?\d+$/; |
746
|
|
|
|
|
|
|
} |
747
|
|
|
|
|
|
|
|
748
|
|
|
|
|
|
|
############################################################################### |
749
|
|
|
|
|
|
|
|
750
|
|
|
|
|
|
|
=item C |
751
|
|
|
|
|
|
|
|
752
|
|
|
|
|
|
|
An floating point number. |
753
|
|
|
|
|
|
|
|
754
|
|
|
|
|
|
|
=cut |
755
|
|
|
|
|
|
|
|
756
|
|
|
|
|
|
|
sub is_a_float($ ) { |
757
|
16
|
|
|
16
|
0
|
19
|
my( $r ) = @_; |
758
|
|
|
|
|
|
|
|
759
|
16
|
|
100
|
|
|
262
|
return ref($r) eq "SCALAR" && $$r =~ /^[-+]?(\d+(\.\d*)?|\.\d+)([Ee][-+]?\d+)?$/; |
760
|
|
|
|
|
|
|
} |
761
|
|
|
|
|
|
|
|
762
|
|
|
|
|
|
|
############################################################################### |
763
|
|
|
|
|
|
|
|
764
|
|
|
|
|
|
|
=item C |
765
|
|
|
|
|
|
|
|
766
|
|
|
|
|
|
|
A boolean. Actually every scalar is a boolean in Perl, so this is more a |
767
|
|
|
|
|
|
|
description of how a certain value is used. |
768
|
|
|
|
|
|
|
|
769
|
|
|
|
|
|
|
=cut |
770
|
|
|
|
|
|
|
|
771
|
|
|
|
|
|
|
sub is_a_boolean($ ) { |
772
|
6
|
|
|
6
|
0
|
10
|
my( $r ) = @_; |
773
|
|
|
|
|
|
|
|
774
|
6
|
|
|
|
|
38
|
return ref($r) eq "SCALAR"; |
775
|
|
|
|
|
|
|
} |
776
|
|
|
|
|
|
|
|
777
|
|
|
|
|
|
|
############################################################################### |
778
|
|
|
|
|
|
|
|
779
|
|
|
|
|
|
|
=item C |
780
|
|
|
|
|
|
|
|
781
|
|
|
|
|
|
|
A string which compiles cleanly as a regular expression. The C is |
782
|
|
|
|
|
|
|
applied to an empty string so any parentheses in the C will probably |
783
|
|
|
|
|
|
|
don't result in anything useful. |
784
|
|
|
|
|
|
|
|
785
|
|
|
|
|
|
|
Note, that nothing prevents the C from executing arbitrary code if you |
786
|
|
|
|
|
|
|
manage to include this somehow. The results are completly undefined. |
787
|
|
|
|
|
|
|
|
788
|
|
|
|
|
|
|
=cut |
789
|
|
|
|
|
|
|
|
790
|
|
|
|
|
|
|
sub is_a_regex($ ) { |
791
|
23
|
|
|
23
|
0
|
30
|
my( $r ) = @_; |
792
|
|
|
|
|
|
|
|
793
|
23
|
|
100
|
|
|
214
|
return ref($r) eq "SCALAR" && defined($$r) && defined(eval { "" =~ /$$r/ }); |
794
|
|
|
|
|
|
|
} |
795
|
|
|
|
|
|
|
|
796
|
|
|
|
|
|
|
############################################################################### |
797
|
|
|
|
|
|
|
|
798
|
|
|
|
|
|
|
=back |
799
|
|
|
|
|
|
|
|
800
|
|
|
|
|
|
|
=head2 Precedence |
801
|
|
|
|
|
|
|
|
802
|
|
|
|
|
|
|
The precedence of the operators is as indicated by the grammar. Because most |
803
|
|
|
|
|
|
|
operators are prefix operators there is not much room for ambiguity anyway. |
804
|
|
|
|
|
|
|
However, the grammar for alternatives opens some traps. In particular the |
805
|
|
|
|
|
|
|
current grammar means, that it is not possible to have |
806
|
|
|
|
|
|
|
|
807
|
|
|
|
|
|
|
=over 4 |
808
|
|
|
|
|
|
|
|
809
|
|
|
|
|
|
|
=item * references to alternatives |
810
|
|
|
|
|
|
|
|
811
|
|
|
|
|
|
|
A type description C<\type1|type2> would be parsed as an alternative between |
812
|
|
|
|
|
|
|
C<\type1> and C instead of a reference to either C or C. |
813
|
|
|
|
|
|
|
Use C<\type1|\type2> instead. |
814
|
|
|
|
|
|
|
|
815
|
|
|
|
|
|
|
=item * alternatives between array types |
816
|
|
|
|
|
|
|
|
817
|
|
|
|
|
|
|
A type description C<@type1|@type2> is indeed not allowed by the grammar. |
818
|
|
|
|
|
|
|
Probably you're thinking of C<@type1|type2> anyway which describes an array |
819
|
|
|
|
|
|
|
consisting of C and/or C values. |
820
|
|
|
|
|
|
|
|
821
|
|
|
|
|
|
|
If you want to describe arrays consisting of exactly one or another type use an |
822
|
|
|
|
|
|
|
additional reference for your value and try C<\@type1|\@type2>. |
823
|
|
|
|
|
|
|
|
824
|
|
|
|
|
|
|
=item * lists as hash value types |
825
|
|
|
|
|
|
|
|
826
|
|
|
|
|
|
|
Similarly C<%typeK=>>C<@typeV1|typeV2> is not allowed by the grammar. It would |
827
|
|
|
|
|
|
|
not make sense anyway because a list can not be the value of a hash key. |
828
|
|
|
|
|
|
|
|
829
|
|
|
|
|
|
|
However, C<%typeK=>>C<\@typeV1|\@typeV2> is possible and describes a hash |
830
|
|
|
|
|
|
|
mapping C values to references to arrays consisting of either C |
831
|
|
|
|
|
|
|
or C elements. |
832
|
|
|
|
|
|
|
|
833
|
|
|
|
|
|
|
=item * references to list types with alternatives |
834
|
|
|
|
|
|
|
|
835
|
|
|
|
|
|
|
A type description C<\@type1|type2> describes a reference to an array of |
836
|
|
|
|
|
|
|
C elements or a C value. It is B a reference to an array |
837
|
|
|
|
|
|
|
consisting of C and/or C elements. |
838
|
|
|
|
|
|
|
|
839
|
|
|
|
|
|
|
Even worse C<\%typeK1|typeK2=>>C can't be parsed at all because the |
840
|
|
|
|
|
|
|
alternative is evaluated before the hash designator. |
841
|
|
|
|
|
|
|
|
842
|
|
|
|
|
|
|
=back |
843
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
Note, that you can always define your own test functions which may break down |
845
|
|
|
|
|
|
|
complex types to simple names. With the C function this is usually done |
846
|
|
|
|
|
|
|
with a few key strokes. |
847
|
|
|
|
|
|
|
|
848
|
|
|
|
|
|
|
=head1 TODO |
849
|
|
|
|
|
|
|
|
850
|
|
|
|
|
|
|
=over 4 |
851
|
|
|
|
|
|
|
|
852
|
|
|
|
|
|
|
=item * |
853
|
|
|
|
|
|
|
|
854
|
|
|
|
|
|
|
As noted above the lack of parentheses in the grammar makes some complex |
855
|
|
|
|
|
|
|
constructions impossible. However, introducing parentheses would make a more |
856
|
|
|
|
|
|
|
complex parser necessary. After all user defined types may be used for |
857
|
|
|
|
|
|
|
simulating parentheses. |
858
|
|
|
|
|
|
|
|
859
|
|
|
|
|
|
|
If parentheses, brackets and braces would be added to the grammar, the |
860
|
|
|
|
|
|
|
following changed productions would be probably best: |
861
|
|
|
|
|
|
|
|
862
|
|
|
|
|
|
|
=over |
863
|
|
|
|
|
|
|
|
864
|
|
|
|
|
|
|
=item I := |
865
|
|
|
|
|
|
|
|
866
|
|
|
|
|
|
|
'C<(>' I 'C<)> | I | ... |
867
|
|
|
|
|
|
|
|
868
|
|
|
|
|
|
|
=item I := |
869
|
|
|
|
|
|
|
|
870
|
|
|
|
|
|
|
'C<\>' I | 'C<[>' I 'C<]>' | 'C<{>' I 'C<=>>' I 'C<}>' | I | ... |
871
|
|
|
|
|
|
|
|
872
|
|
|
|
|
|
|
=back |
873
|
|
|
|
|
|
|
|
874
|
|
|
|
|
|
|
=item * |
875
|
|
|
|
|
|
|
|
876
|
|
|
|
|
|
|
Furthermore it would be nice to have |
877
|
|
|
|
|
|
|
|
878
|
|
|
|
|
|
|
=over 4 |
879
|
|
|
|
|
|
|
|
880
|
|
|
|
|
|
|
=item I := |
881
|
|
|
|
|
|
|
|
882
|
|
|
|
|
|
|
I | 'C>' I 'C>' | I 'C<..>' I |
883
|
|
|
|
|
|
|
|
884
|
|
|
|
|
|
|
=item I := |
885
|
|
|
|
|
|
|
|
886
|
|
|
|
|
|
|
I |
887
|
|
|
|
|
|
|
|
888
|
|
|
|
|
|
|
=item I := |
889
|
|
|
|
|
|
|
|
890
|
|
|
|
|
|
|
I^[-+]?\d+(\.\d*)([Ee][-+]\d+)?$/> |
891
|
|
|
|
|
|
|
|
892
|
|
|
|
|
|
|
=back |
893
|
|
|
|
|
|
|
|
894
|
|
|
|
|
|
|
so you can define an anonymous type for a string matching a regex or for a |
895
|
|
|
|
|
|
|
number being inside a range. But given the rich structure of Perl regexes at |
896
|
|
|
|
|
|
|
least the I would require a real parser. |
897
|
|
|
|
|
|
|
|
898
|
|
|
|
|
|
|
=back |
899
|
|
|
|
|
|
|
|
900
|
|
|
|
|
|
|
=head1 SIMILAR MODULES |
901
|
|
|
|
|
|
|
|
902
|
|
|
|
|
|
|
There is the L package which has a similar functionality. However, it |
903
|
|
|
|
|
|
|
dates 1996 and seems not be maintained since then. Unfortunately it is not as |
904
|
|
|
|
|
|
|
flexible as this module and is still a bit buggy. |
905
|
|
|
|
|
|
|
|
906
|
|
|
|
|
|
|
=head1 AUTHOR |
907
|
|
|
|
|
|
|
|
908
|
|
|
|
|
|
|
Stefan Merten |
909
|
|
|
|
|
|
|
|
910
|
|
|
|
|
|
|
The idea for the code implementing the B feature was taken from |
911
|
|
|
|
|
|
|
L by Michael G. Schwern . |
912
|
|
|
|
|
|
|
|
913
|
|
|
|
|
|
|
=head1 SEE ALSO |
914
|
|
|
|
|
|
|
|
915
|
|
|
|
|
|
|
L |
916
|
|
|
|
|
|
|
|
917
|
|
|
|
|
|
|
L |
918
|
|
|
|
|
|
|
|
919
|
|
|
|
|
|
|
=head1 LICENSE |
920
|
|
|
|
|
|
|
|
921
|
|
|
|
|
|
|
This program is licensed under the terms of the GPL. See |
922
|
|
|
|
|
|
|
|
923
|
|
|
|
|
|
|
http://www.gnu.org/licenses/gpl.txt |
924
|
|
|
|
|
|
|
|
925
|
|
|
|
|
|
|
=head1 AVAILABILTY |
926
|
|
|
|
|
|
|
|
927
|
|
|
|
|
|
|
See |
928
|
|
|
|
|
|
|
|
929
|
|
|
|
|
|
|
http://www.merten-home.de/FreeSoftware/Carp_Ensure/ |
930
|
|
|
|
|
|
|
|
931
|
|
|
|
|
|
|
=cut |
932
|
|
|
|
|
|
|
|
933
|
|
|
|
|
|
|
1; |