Bug 17189: Add the ability to define several memcached namespaces - replace existing...
[koha.git] / C4 / Boolean.pm
1 package C4::Boolean;
2
3 #package to handle Boolean values in the parameters table
4 # Note: This is just a utility module; it should not be instantiated.
5
6
7 # Copyright 2003 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23
24 use strict;
25 use warnings;
26
27 use Carp;
28 use base qw(Exporter);
29
30 our    @EXPORT_OK = qw( true_p);
31
32 =head1 NAME
33
34 C4::Boolean - Convenience function to handle boolean values
35 in the parameter table
36
37 =head1 SYNOPSIS
38
39   use C4::Boolean;
40
41 =head1 DESCRIPTION
42
43 In the parameter table, there are various Boolean values that
44 variously require a 0/1, no/yes, false/true, or off/on values.
45 This module aims to provide scripts a means to interpret these
46 Boolean values in a consistent way which makes common sense.
47
48 =head1 FUNCTIONS
49
50 =over 2
51
52 =cut
53
54 use constant INVALID_BOOLEAN_STRING_EXCEPTION =>
55   q{The given value does not seem to be interpretable as a Boolean value};
56
57 our %strings = (
58    '0'     => 0,    '1'     => 1,    # C
59                     '-1'    => 1,    # BASIC
60    'nil'   => 0,    't'     => 1,    # LISP
61    'false' => 0,    'true'  => 1,    # Pascal
62    'off'   => 0,    'on'    => 1,
63    'no'    => 0,    'yes'   => 1,
64    'n'     => 0,    'y'     => 1,
65 );
66
67 =item true_p
68
69     if ( C4::Boolean::true_p(C4::Context->preference("IndependentBranches")) ) {
70     ...
71     }
72
73 Tries to interpret the passed string as a Boolean value. Returns
74 the value if the string can be interpreted as such; otherwise an
75 exception is thrown.
76
77 =cut
78
79 sub true_p  {
80     my $x = shift;
81     my $it;
82     if (!defined $x || ref $x ) {
83         carp INVALID_BOOLEAN_STRING_EXCEPTION;
84         return;
85     }
86     $x = lc $x;
87     $x =~ s/\s//g;
88     if (defined $strings{$x}) {
89         $it = $strings{$x};
90     } else {
91         carp INVALID_BOOLEAN_STRING_EXCEPTION;
92     }
93     return $it;
94 }
95
96 1;
97 __END__
98
99 =back
100
101 =head1 AUTHOR
102
103 Koha Development Team <http://koha-community.org/>
104
105 =cut