line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package String::BooleanSimple; ## Gets the boolean representative of a string |
2
|
|
|
|
|
|
|
$String::BooleanSimple::VERSION = '0.026'; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
3889
|
use strict; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
83
|
|
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
10
|
use vars qw(@ISA @EXPORT %EXPORT_TAGS $VERSION); |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
184
|
|
9
|
3
|
|
|
3
|
|
10
|
use Exporter; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
270
|
|
10
|
3
|
|
|
3
|
|
1129
|
use boolean qw(true false); |
|
3
|
|
|
|
|
7272
|
|
|
3
|
|
|
|
|
9
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
%EXPORT_TAGS = ( all => [qw( |
16
|
|
|
|
|
|
|
boolean |
17
|
|
|
|
|
|
|
is_true |
18
|
|
|
|
|
|
|
is_false |
19
|
|
|
|
|
|
|
isTrue |
20
|
|
|
|
|
|
|
isFalse |
21
|
|
|
|
|
|
|
)] ); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
Exporter::export_ok_tags('all'); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# Some static functions returning a boolean value (0 or 1) of a string like |
27
|
|
|
|
|
|
|
# 'true', 'false', etc. In the background it returns the value from the module |
28
|
|
|
|
|
|
|
# boolean, which holds a true/false method. What means, if you change that rules, |
29
|
|
|
|
|
|
|
# it would not return a 0 or 1, but whatever you defined. |
30
|
|
|
|
|
|
|
# |
31
|
|
|
|
|
|
|
# SYNOPSIS |
32
|
|
|
|
|
|
|
# ======== |
33
|
|
|
|
|
|
|
# |
34
|
|
|
|
|
|
|
# # imports all functions |
35
|
|
|
|
|
|
|
# use String::BooleanSimple ':all'; |
36
|
|
|
|
|
|
|
# |
37
|
|
|
|
|
|
|
# # imports only is_true() |
38
|
|
|
|
|
|
|
# use String::BooleanSimple qw(is_true); |
39
|
|
|
|
|
|
|
# |
40
|
|
|
|
|
|
|
# |
41
|
|
|
|
|
|
|
# Matches |
42
|
|
|
|
|
|
|
# ======= |
43
|
|
|
|
|
|
|
# Supports these strings: |
44
|
|
|
|
|
|
|
# |
45
|
|
|
|
|
|
|
# |
46
|
|
|
|
|
|
|
# true yes active enabled on y ok positive 1 2 3 4 5 6 7 8 9 |
47
|
|
|
|
|
|
|
# |
48
|
|
|
|
|
|
|
# false no inactive disabled off n not ok negative 0 |
49
|
|
|
|
|
|
|
# |
50
|
|
|
|
|
|
|
# If the string does not match, it causes an error. Whitespace at the beginning or end will be automatically removed. |
51
|
|
|
|
|
|
|
# |
52
|
|
|
|
|
|
|
# Default values |
53
|
|
|
|
|
|
|
# ============== |
54
|
|
|
|
|
|
|
# You may set a default string as second parameter on the functions. It works if the first value can not be indentified |
55
|
|
|
|
|
|
|
# as valid string. An empty string is also a non-valid string and will trigger the default value. |
56
|
|
|
|
|
|
|
# |
57
|
|
|
|
|
|
|
# Example: |
58
|
|
|
|
|
|
|
# |
59
|
|
|
|
|
|
|
# if ( is_true("","false") ){}; |
60
|
|
|
|
|
|
|
# |
61
|
|
|
|
|
|
|
# it will be false. Typical for reading config files, where a value does not allways exist. |
62
|
|
|
|
|
|
|
# |
63
|
|
|
|
|
|
|
# If the default value as well is not a valid string, it dies with an error. |
64
|
|
|
|
|
|
|
# |
65
|
|
|
|
|
|
|
# Module boolean |
66
|
|
|
|
|
|
|
# ============== |
67
|
|
|
|
|
|
|
# If the calling application using the module boolean, you may write it like that: |
68
|
|
|
|
|
|
|
# |
69
|
|
|
|
|
|
|
# use boolean ':all'; |
70
|
|
|
|
|
|
|
# use String::BooleanSimple 'boolean'; |
71
|
|
|
|
|
|
|
# |
72
|
|
|
|
|
|
|
# my $s='positive' |
73
|
|
|
|
|
|
|
# |
74
|
|
|
|
|
|
|
# if ( isTrue( boolean($s) ) ){...}; |
75
|
|
|
|
|
|
|
# |
76
|
|
|
|
|
|
|
# if ( boolean($s) == true ){...}; |
77
|
|
|
|
|
|
|
# |
78
|
|
|
|
|
|
|
# |
79
|
|
|
|
|
|
|
# Please note, here the isTrue() function is part of "use boolean"! |
80
|
|
|
|
|
|
|
# It is not imported with ':all' from String::BooleanSimple because that is a conflict. |
81
|
|
|
|
|
|
|
# |
82
|
|
|
|
|
|
|
# The following example is possible and logical, but looks silly: |
83
|
|
|
|
|
|
|
# |
84
|
|
|
|
|
|
|
# if ( is_true($s) == true ){...}; |
85
|
|
|
|
|
|
|
# |
86
|
|
|
|
|
|
|
# Theoretically you must do like that, if "use boolean", because who knows what is realy "true" and "false"? But |
87
|
|
|
|
|
|
|
# it is not very nice to read it and the simple way "if( is_true() )" worth to risk that maybe false is not '0'. |
88
|
|
|
|
|
|
|
# |
89
|
|
|
|
|
|
|
# In other words, if the calling app is not using perl's "boolean" but somehow in perl's module "boolean" the |
90
|
|
|
|
|
|
|
# value of true/false does not fit anymore to 1/0, it might be, that using is_true/is_false with String::BooleanSimple |
91
|
|
|
|
|
|
|
# will cause wrong behaviour. |
92
|
|
|
|
|
|
|
# |
93
|
|
|
|
|
|
|
# |
94
|
|
|
|
|
|
|
# LICENSE |
95
|
|
|
|
|
|
|
# ======= |
96
|
|
|
|
|
|
|
# You can redistribute it and/or modify it under the conditions of LGPL. |
97
|
|
|
|
|
|
|
# |
98
|
|
|
|
|
|
|
# AUTHOR |
99
|
|
|
|
|
|
|
# ====== |
100
|
|
|
|
|
|
|
# Andreas Hernitscheck ahernit(AT)cpan.org |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# Returns 1 if the string matches to a positive pattern like "true". |
104
|
|
|
|
|
|
|
sub is_true { # $boolean ($string,$defaultstring) |
105
|
156
|
|
|
156
|
1
|
4019
|
my $value = shift; |
106
|
156
|
|
|
|
|
119
|
my $def_value = shift; |
107
|
156
|
|
|
|
|
149
|
my $ret = undef; |
108
|
|
|
|
|
|
|
|
109
|
156
|
|
|
|
|
197
|
$ret = boolean($value,$def_value); |
110
|
|
|
|
|
|
|
|
111
|
156
|
|
|
|
|
494
|
return $ret; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# Returns 1 if the string matches to a negative pattern like "false". |
117
|
|
|
|
|
|
|
sub is_false { # $boolean ($string,$defaultstring) |
118
|
156
|
|
|
156
|
1
|
2326
|
my $value = shift; |
119
|
156
|
|
|
|
|
131
|
my $def_value = shift; |
120
|
156
|
|
|
|
|
131
|
my $ret = undef; |
121
|
|
|
|
|
|
|
|
122
|
156
|
100
|
|
|
|
200
|
$ret = 1 - ( boolean($value,$def_value) == true ? true : 0 ); |
123
|
|
|
|
|
|
|
|
124
|
156
|
100
|
|
|
|
1359
|
$ret ? true : false; |
125
|
|
|
|
|
|
|
|
126
|
156
|
|
|
|
|
588
|
return $ret; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# Returns 1 if the string matches to a postive pattern like "true". |
131
|
|
|
|
|
|
|
# Returns 0 if the string matches to a negative pattern like "false". |
132
|
|
|
|
|
|
|
sub boolean { # $boolean ($string,$defaultstring) |
133
|
624
|
|
|
624
|
1
|
535
|
my $value = shift; |
134
|
624
|
|
|
|
|
431
|
my $def_value = shift; |
135
|
624
|
|
|
|
|
455
|
my $ret = 2; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# trim |
138
|
624
|
|
|
|
|
615
|
$value = _trim( $value ); |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# lower case |
141
|
624
|
|
|
|
|
582
|
$value = lc( $value ); |
142
|
|
|
|
|
|
|
|
143
|
624
|
100
|
|
|
|
1601
|
if ( $value =~ m/^(true|yes|active|enabled|on|y|ok|positive|[1-9])$/ ){ $ret = 1 }; |
|
306
|
|
|
|
|
288
|
|
144
|
|
|
|
|
|
|
|
145
|
624
|
100
|
|
|
|
1091
|
if ( $value =~ m/^(false|no|inactive|disabled|off|n|negative|not ok|[0])$/ ){ $ret = 0 }; |
|
162
|
|
|
|
|
152
|
|
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
|
148
|
624
|
100
|
|
|
|
916
|
if ($ret == 2) { |
149
|
|
|
|
|
|
|
|
150
|
156
|
50
|
|
|
|
231
|
if ( $def_value ne '' ){ |
151
|
156
|
|
|
|
|
206
|
$ret = boolean( $def_value ); |
152
|
|
|
|
|
|
|
}else{ |
153
|
|
|
|
|
|
|
|
154
|
0
|
|
|
|
|
0
|
die "String value \'$value\' does not match to a given true/false pattern."; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
624
|
100
|
|
|
|
1280
|
$ret ? true : false; |
160
|
|
|
|
|
|
|
|
161
|
624
|
|
|
|
|
1442
|
return $ret; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
# trimming the string from whitespace |
166
|
|
|
|
|
|
|
sub _trim { |
167
|
624
|
|
|
624
|
|
538
|
my $value = shift; |
168
|
|
|
|
|
|
|
|
169
|
624
|
|
|
|
|
1672
|
$value =~ s/^\s*//s; |
170
|
624
|
|
|
|
|
1234
|
$value =~ s/\s*$//s; |
171
|
|
|
|
|
|
|
|
172
|
624
|
|
|
|
|
752
|
return $value; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# If you like the java style, you may import that alias |
177
|
|
|
|
|
|
|
sub isTrue{ # $boolean ($string,$defaultstring) |
178
|
130
|
|
|
130
|
1
|
9927
|
return is_true(@_); |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
# If you like the java style, you may import that alias |
182
|
|
|
|
|
|
|
sub isFalse{ # $boolean ($string,$defaultstring) |
183
|
130
|
|
|
130
|
1
|
5727
|
return is_false(@_); |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
1; |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
#################### pod generated by Pod::Autopod - keep this line to make pod updates possible #################### |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 NAME |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
String::BooleanSimple - Gets the boolean representative of a string |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head1 SYNOPSIS |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
# imports all functions |
200
|
|
|
|
|
|
|
use String::BooleanSimple ':all'; |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
# imports only is_true() |
203
|
|
|
|
|
|
|
use String::BooleanSimple qw(is_true); |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=head1 DESCRIPTION |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
Some static functions returning a boolean value (0 or 1) of a string like |
211
|
|
|
|
|
|
|
'true', 'false', etc. In the background it returns the value from the module |
212
|
|
|
|
|
|
|
boolean, which holds a true/false method. What means, if you change that rules, |
213
|
|
|
|
|
|
|
it would not return a 0 or 1, but whatever you defined. |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head1 REQUIRES |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
L |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
L |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=head1 METHODS |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=head2 boolean |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
my $boolean = boolean($string, $defaultstring); |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
Returns 1 if the string matches to a postive pattern like "true". |
231
|
|
|
|
|
|
|
Returns 0 if the string matches to a negative pattern like "false". |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head2 isFalse |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
my $boolean = isFalse($string, $defaultstring); |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
If you like the java style, you may import that alias |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=head2 isTrue |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
my $boolean = isTrue($string, $defaultstring); |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
If you like the java style, you may import that alias |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=head2 is_false |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
my $boolean = is_false($string, $defaultstring); |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
Returns 1 if the string matches to a negative pattern like "false". |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=head2 is_true |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
my $boolean = is_true($string, $defaultstring); |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
Returns 1 if the string matches to a positive pattern like "true". |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=head1 Matches |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
Supports these strings: |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
true yes active enabled on y ok positive 1 2 3 4 5 6 7 8 9 |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
false no inactive disabled off n not ok negative 0 |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
If the string does not match, it causes an error. Whitespace at the beginning or end will be automatically removed. |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=head1 Default values |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
You may set a default string as second parameter on the functions. It works if the first value can not be indentified |
279
|
|
|
|
|
|
|
as valid string. An empty string is also a non-valid string and will trigger the default value. |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
Example: |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
if ( is_true("","false") ){}; |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
it will be false. Typical for reading config files, where a value does not allways exist. |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
If the default value as well is not a valid string, it dies with an error. |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=head1 Module boolean |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
If the calling application using the module boolean, you may write it like that: |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
use boolean ':all'; |
296
|
|
|
|
|
|
|
use String::BooleanSimple 'boolean'; |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
my $s='positive' |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
if ( isTrue( boolean($s) ) ){...}; |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
if ( boolean($s) == true ){...}; |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
Please note, here the isTrue() function is part of "use boolean"! |
306
|
|
|
|
|
|
|
It is not imported with ':all' from String::BooleanSimple because that is a conflict. |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
The following example is possible and logical, but looks silly: |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
if ( is_true($s) == true ){...}; |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
Theoretically you must do like that, if "use boolean", because who knows what is realy "true" and "false"? But |
313
|
|
|
|
|
|
|
it is not very nice to read it and the simple way "if( is_true() )" worth to risk that maybe false is not '0'. |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
In other words, if the calling app is not using perl's "boolean" but somehow in perl's module "boolean" the |
316
|
|
|
|
|
|
|
value of true/false does not fit anymore to 1/0, it might be, that using is_true/is_false with String::BooleanSimple |
317
|
|
|
|
|
|
|
will cause wrong behaviour. |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
=head1 AUTHOR |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
Andreas Hernitscheck ahernit(AT)cpan.org |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
=head1 LICENSE |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
You can redistribute it and/or modify it under the conditions of LGPL. |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
=cut |
334
|
|
|
|
|
|
|
|