Bug 2505 - enable Perl warnings in all modules and scripts
[koha.git] / C4 / Boolean.pm
blob02c0014b84ac58ab5e3e2ab3c2d9206288423bc1
1 package C4::Boolean;
3 #package to handle Boolean values in the parameters table
4 # Note: This is just a utility module; it should not be instantiated.
7 # Copyright 2003 Katipo Communications
9 # This file is part of Koha.
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.
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.
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.
24 use strict;
25 use warnings;
27 use Carp;
28 use base qw(Exporter);
30 our $VERSION = 0.03;
31 our @EXPORT_OK = qw( true_p);
33 =head1 NAME
35 C4::Boolean - Convenience function to handle boolean values
36 in the parameter table
38 =head1 SYNOPSIS
40 use C4::Boolean;
42 =head1 DESCRIPTION
44 In the parameter table, there are various Boolean values that
45 variously require a 0/1, no/yes, false/true, or off/on values.
46 This module aims to provide scripts a means to interpret these
47 Boolean values in a consistent way which makes common sense.
49 =head1 FUNCTIONS
51 =over 2
53 =cut
55 use constant INVALID_BOOLEAN_STRING_EXCEPTION =>
56 q{The given value does not seem to be interpretable as a Boolean value};
58 our %strings = (
59 '0' => 0, '1' => 1, # C
60 '-1' => 1, # BASIC
61 'nil' => 0, 't' => 1, # LISP
62 'false' => 0, 'true' => 1, # Pascal
63 'off' => 0, 'on' => 1,
64 'no' => 0, 'yes' => 1,
65 'n' => 0, 'y' => 1,
68 =item true_p
70 if ( C4::Boolean::true_p(C4::Context->preference("insecure")) ) {
71 ...
74 Tries to interpret the passed string as a Boolean value. Returns
75 the value if the string can be interpreted as such; otherwise an
76 exception is thrown.
78 =cut
80 sub true_p {
81 my $x = shift;
82 my $it;
83 if (!defined $x || ref $x ) {
84 carp INVALID_BOOLEAN_STRING_EXCEPTION;
85 return;
87 $x = lc $x;
88 $x =~ s/\s//g;
89 if (defined $strings{$x}) {
90 $it = $strings{$x};
91 } else {
92 carp INVALID_BOOLEAN_STRING_EXCEPTION;
94 return $it;
98 __END__
100 =back
102 =head1 AUTHOR
104 Koha Development Team <http://koha-community.org/>
106 =cut