lib/String/BooleanSimple.pm

filter: Statementstmt 97.9% 48 / 49 Branchbran 92.8% 13 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 97.4% 76 / 78 CC 8 SCAR 11.4
CC
8
Cov
97%
boolean
6.0
is_false
3.0
BEGIN
1.0
CRAP 3.1
↑ Summary
1 package String::BooleanSimple; ## Gets the boolean representative of a string
2 3 $String::BooleanSimple::VERSION = '0.026';
3  
4  
5  
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use vars qw(@ISA @EXPORT %EXPORT_TAGS $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use boolean qw(true false);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 3 @ISA = qw(Exporter);
14  
15 3 %EXPORT_TAGS = ( all => [qw(
16 boolean
17 is_true
18 is_false
19 isTrue
20 isFalse
21 )] );
22  
23 3 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 156 ▶sub is_true { # $boolean ($string,$defaultstring)
Subroutine is_true: called CC=1 SCAR=0.0
Pod documented
105 156 my $value = shift;
106 156 my $def_value = shift;
107 156 my $ret = undef;
108  
109 156 $ret = boolean($value,$def_value);
110  
111 156 return $ret;
112 }
113  
114  
115  
116 # Returns 1 if the string matches to a negative pattern like "false".
117 156 ▶sub is_false { # $boolean ($string,$defaultstring)
Subroutine is_false: called CC=3 SCAR=11.0
Pod documented
118 156 my $value = shift;
119 156 my $def_value = shift;
120 156 my $ret = undef;
121  
122 156 ▶ $ret = 1 - ( boolean($value,$def_value) == true ? true : 0 );
Branch: 1 branch
BranchTrueFalseTotal
boolean($value, $def_value) == &true() ? : 102 54 156
123  
124 156 ▶ $ret ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
$ret ? : 54 102 156
125  
126 156 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 624 ▶sub boolean { # $boolean ($string,$defaultstring)
Subroutine boolean: called CC=6 SCAR=17.9
Pod documented
133 624 my $value = shift;
134 624 my $def_value = shift;
135 624 my $ret = 2;
136  
137 # trim
138 624 $value = _trim( $value );
139  
140 # lower case
141 624 $value = lc( $value );
142  
143 624 ▶ if ( $value =~ m/^(true|yes|active|enabled|on|y|ok|positive|[1-9])$/ ){ $ret = 1 };
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(true|yes|active|enabled|on|y|ok|positive|[1-9])$/) 306 318 624
144  
145 624 ▶ if ( $value =~ m/^(false|no|inactive|disabled|off|n|negative|not ok|[0])$/ ){ $ret = 0 };
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(false|no|inactive|disabled|off|n|negative|not ok|[0])$/) 162 462 624
146  
147  
148 624 ▶ if ($ret == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret == 2) 156 468 624
149  
150 156 ▶ if ( $def_value ne '' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($def_value ne '') { } 156 0 156
151 156 $ret = boolean( $def_value );
152 }else{
153  
154 0 die "String value \'$value\' does not match to a given true/false pattern.";
155  
156 }
157 }
158  
159 624 ▶ $ret ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
$ret ? : 408 216 624
160  
161 624 return $ret;
162 }
163  
164  
165 # trimming the string from whitespace
166 624 ▶sub _trim {
Subroutine _trim: called CC=1 SCAR=0.0
167 624 my $value = shift;
168  
169 624 $value =~ s/^\s*//s;
170 624 $value =~ s/\s*$//s;
171
172 624 return $value;
173 }
174  
175  
176 # If you like the java style, you may import that alias
177 130 ▶sub isTrue{ # $boolean ($string,$defaultstring)
Subroutine isTrue: called CC=1 SCAR=0.0
Pod documented
178 130 return is_true(@_);
179 }
180  
181 # If you like the java style, you may import that alias
182 130 ▶sub isFalse{ # $boolean ($string,$defaultstring)
Subroutine isFalse: called CC=1 SCAR=0.0
Pod documented
183 130 return is_false(@_);
184 }
185  
186  
187 3 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<boolean>
220  
221 L<Exporter>
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  
↑ Summary