File Coverage

blib/lib/String/BooleanSimple.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


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