Fix FSF address in directory C4/
[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 under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along
21 # with Koha; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
24 use strict;
25 use warnings;
26
27 use POSIX;
28
29 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
30
31 BEGIN {
32         # set the version for version checking
33         $VERSION = 0.02;
34         require Exporter;
35         @EXPORT = qw(
36                 &INVALID_BOOLEAN_STRING_EXCEPTION
37     );
38         @EXPORT_OK = qw(
39                 true_p
40     );
41         @ISA = qw(Exporter);
42 }
43
44 =head1 NAME
45
46 C4::Boolean - Convenience functions to handle boolean values
47 in the parameter table
48
49 =head1 SYNOPSIS
50
51   use C4::Boolean;
52
53 =head1 DESCRIPTION
54
55 In the parameter table, there are various Boolean values that
56 variously require a 0/1, no/yes, false/true, or off/on values.
57 This module aims to provide scripts a means to interpret these
58 Boolean values in a consistent way which makes common sense.
59
60 =head1 FUNCTIONS
61
62 =over 2
63
64 =cut
65
66 sub INVALID_BOOLEAN_STRING_EXCEPTION ()
67     { 'The given value does not seem to be interpretable as a Boolean value' }
68
69 use vars qw( %strings );
70
71 %strings = (
72    '0'     => 0,        '1'     => 1,   # C
73                         '-1'    => 1,   # BASIC
74    'nil'   => 0,        't'     => 1,   # LISP
75    'false' => 0,        'true'  => 1,   # Pascal
76    'off'   => 0,        'on'    => 1,
77    'no'    => 0,        'yes'   => 1,
78    'n'     => 0,        'y'     => 1,
79 );
80
81 =item true_p
82
83     if ( C4::Boolean::true_p(C4::Context->preference("insecure")) ) {
84         ...
85     }
86
87 Tries to interpret the passed string as a Boolean value. Returns
88 the value if the string can be interpreted as such; otherwise an
89 exception is thrown.
90
91 =cut
92
93 sub true_p ($) {
94     my($x) = @_;
95     my $it;
96     if (!defined $x || ref($x) ne '') {
97         warn INVALID_BOOLEAN_STRING_EXCEPTION;
98     }
99     $x = lc($x);
100     $x =~ s/\s//g;
101     if (defined $strings{$x}) {
102         $it = $strings{$x};
103     } else {
104         warn INVALID_BOOLEAN_STRING_EXCEPTION;
105     }
106     return $it;
107 }
108
109
110 #---------------------------------
111
112 END { }       # module clean-up code here (global destructor)
113
114 1;
115 __END__
116
117 =back
118
119 =head1 AUTHOR
120
121 Koha Developement team <info@koha.org>
122
123 =cut