File Coverage

File:C4/Boolean.pm
Coverage:100.0%

linestmtbrancondsubtimecode
1package 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
118
118
118
50242
2501
5189
use strict;
25
118
118
118
2065
1179
6040
use warnings;
26
27
118
118
118
1378
845
8400
use Carp;
28
118
118
118
1142
706
14880
use base qw(Exporter);
29
30our $VERSION = 0.03;
31our @EXPORT_OK = qw( true_p);
32
33 - 53
=head1 NAME

C4::Boolean - Convenience function to handle boolean values
in the parameter table

=head1 SYNOPSIS

  use C4::Boolean;

=head1 DESCRIPTION

In the parameter table, there are various Boolean values that
variously require a 0/1, no/yes, false/true, or off/on values.
This module aims to provide scripts a means to interpret these
Boolean values in a consistent way which makes common sense.

=head1 FUNCTIONS

=over 2

=cut
54
55
118
2245
use constant INVALID_BOOLEAN_STRING_EXCEPTION =>
56
118
118
37933
137991
  q{The given value does not seem to be interpretable as a Boolean value};
57
58our %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,
66);
67
68 - 78
=item true_p

    if ( C4::Boolean::true_p(C4::Context->preference("insecure")) ) {
    ...
    }

Tries to interpret the passed string as a Boolean value. Returns
the value if the string can be interpreted as such; otherwise an
exception is thrown.

=cut
79
80sub true_p {
81
24
374138
    my $x = shift;
82
24
134
    my $it;
83
24
254
    if (!defined $x || ref $x ) {
84
4
511
        carp INVALID_BOOLEAN_STRING_EXCEPTION;
85
4
9397
        return;
86    }
87
20
132
    $x = lc $x;
88
20
182
    $x =~ s/\s//g;
89
20
140
    if (defined $strings{$x}) {
90
18
112
        $it = $strings{$x};
91    } else {
92
2
20
        carp INVALID_BOOLEAN_STRING_EXCEPTION;
93    }
94
20
637
    return $it;
95}
96
971;