Bug 5448: Refactor Boolean.pm

Remove unnecesssary export
Use Exporter according to best practices
Use Carp not warn so we know where the error string came from
Don't create warnings manipulating undefined input
Replace package variables by lexicals

Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz>
This commit is contained in:
Colin Campbell 2010-11-26 16:31:35 +00:00 committed by Chris Cormack
parent 78be08c73e
commit f08b33a912

View file

@ -24,26 +24,15 @@ package C4::Boolean;
use strict; use strict;
use warnings; use warnings;
use POSIX; use Carp;
use base qw(Exporter);
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); our $VERSION = 0.03;
our @EXPORT_OK = qw( true_p);
BEGIN {
# set the version for version checking
$VERSION = 0.02;
require Exporter;
@EXPORT = qw(
&INVALID_BOOLEAN_STRING_EXCEPTION
);
@EXPORT_OK = qw(
true_p
);
@ISA = qw(Exporter);
}
=head1 NAME =head1 NAME
C4::Boolean - Convenience functions to handle boolean values C4::Boolean - Convenience function to handle boolean values
in the parameter table in the parameter table
=head1 SYNOPSIS =head1 SYNOPSIS
@ -63,25 +52,23 @@ Boolean values in a consistent way which makes common sense.
=cut =cut
sub INVALID_BOOLEAN_STRING_EXCEPTION () use constant INVALID_BOOLEAN_STRING_EXCEPTION =>
{ 'The given value does not seem to be interpretable as a Boolean value' } q{The given value does not seem to be interpretable as a Boolean value};
use vars qw( %strings ); our %strings = (
'0' => 0, '1' => 1, # C
%strings = ( '-1' => 1, # BASIC
'0' => 0, '1' => 1, # C 'nil' => 0, 't' => 1, # LISP
'-1' => 1, # BASIC 'false' => 0, 'true' => 1, # Pascal
'nil' => 0, 't' => 1, # LISP 'off' => 0, 'on' => 1,
'false' => 0, 'true' => 1, # Pascal 'no' => 0, 'yes' => 1,
'off' => 0, 'on' => 1, 'n' => 0, 'y' => 1,
'no' => 0, 'yes' => 1,
'n' => 0, 'y' => 1,
); );
=item true_p =item true_p
if ( C4::Boolean::true_p(C4::Context->preference("insecure")) ) { if ( C4::Boolean::true_p(C4::Context->preference("insecure")) ) {
... ...
} }
Tries to interpret the passed string as a Boolean value. Returns Tries to interpret the passed string as a Boolean value. Returns
@ -90,27 +77,23 @@ exception is thrown.
=cut =cut
sub true_p ($) { sub true_p {
my($x) = @_; my $x = shift;
my $it; my $it;
if (!defined $x || ref($x) ne '') { if (!defined $x || ref $x ) {
warn INVALID_BOOLEAN_STRING_EXCEPTION; carp INVALID_BOOLEAN_STRING_EXCEPTION;
return;
} }
$x = lc($x); $x = lc $x;
$x =~ s/\s//g; $x =~ s/\s//g;
if (defined $strings{$x}) { if (defined $strings{$x}) {
$it = $strings{$x}; $it = $strings{$x};
} else { } else {
warn INVALID_BOOLEAN_STRING_EXCEPTION; carp INVALID_BOOLEAN_STRING_EXCEPTION;
} }
return $it; return $it;
} }
#---------------------------------
END { } # module clean-up code here (global destructor)
1; 1;
__END__ __END__