Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a trim function to ensure we're not thrown off by whitespace

Calling code should trim, but let's help them out a bit. Thanks
to japhy for code: http://www.perlmonks.org/?node_id=36684
  • Loading branch information...
commit 2b6a75070d525737b1275910e8de85c6f4b7bac4 1 parent 09a9abc
Mike Doherty authored
10 lib/Lingua/Boolean.pm
View
@@ -108,6 +108,7 @@ to check another language. Croaks if the language is unknown to L<Lingua::Boolea
sub looks_true {
my $to_test = shift;
my $lang = shift || 'en';
+ _trim($to_test);
croak "I don't know anything about the language '$lang'" unless exists $regexes->{$lang};
return true if ($to_test ~~ $regexes->{$lang}->{True});
@@ -125,6 +126,7 @@ to check another language. Croaks if the language is unknown to L<Lingua::Boolea
sub looks_false {
my $to_test = shift;
my $lang = shift || 'en';
+ _trim($to_test);
croak "I don't know anything about the language '$lang'" unless exists $regexes->{$lang};
return true if ($to_test ~~ $regexes->{$lang}->{False});
@@ -143,6 +145,7 @@ is unknown to L<Lingua::Boolean>.
sub boolean {
my $to_test = shift;
my $lang = shift || 'en';
+ _trim($to_test);
if (looks_true($to_test, $lang)) {
return true;
@@ -162,6 +165,13 @@ By default, L<Lingua::Boolean> exports C<boolean()>.
=cut
+sub _trim { # http://www.perlmonks.org/?node_id=36684
+ @_ = $_ if not @_ and defined wantarray;
+ @_ = @_ if defined wantarray;
+ for (@_ ? @_ : $_) { s/^\s+|\s+$//g }
+ return wantarray ? @_ : $_[0] if defined wantarray;
+}
+
1;
__END__
4 t/lang/en.t
View
@@ -11,7 +11,7 @@ my @langs = Lingua::Boolean::langs();
ok($lang ~~ @langs, "$lang is available");
subtest 'yes' => sub { #YES
- my @yes = ('y', 'yes', 'ok', 'on', 'Y', 'YES', 'OK', 'ON', 'true', 1, 2);
+ my @yes = (' y', 'yes ', 'ok', 'on', ' Y', 'YES', 'OK', 'ON', 'true', 1, 2);
plan tests => scalar @yes;
foreach my $word (@yes) {
@@ -20,7 +20,7 @@ subtest 'yes' => sub { #YES
};
subtest 'no' => sub { # NO
- my @no = ('n', 'no', 'off', 'not ok', 'N', 'NO', 'OFF', 'NOTOK', 'FAlse', 0);
+ my @no = ('n ', ' no', 'off', 'not ok', 'N', 'NO', 'OFF', 'NOTOK', 'FAlse', 0);
plan tests => scalar @no;
foreach my $word (@no) {
4 t/lang/fr.t
View
@@ -11,7 +11,7 @@ my @langs = Lingua::Boolean::langs();
ok($lang ~~ @langs, "$lang is available");
subtest 'yes' => sub { #YES
- my @yes = ('oui', 'ok', 'vrai', 1);
+ my @yes = (' oui', 'ok ', 'vrai', 1);
plan tests => scalar @yes;
foreach my $word (@yes) {
@@ -20,7 +20,7 @@ subtest 'yes' => sub { #YES
};
subtest 'no' => sub { # NO
- my @no = ('n', 'no', 'non', 'faux', 0);
+ my @no = ('n ', ' no', 'non', 'faux', 0);
plan tests => scalar @no;
foreach my $word (@no) {
4 t/lang/none.t
View
@@ -6,7 +6,7 @@ use Test::More 0.94 tests => 3;
use Lingua::Boolean;
subtest 'yes' => sub { #YES
- my @yes = ('y', 'yes', 'ok', 'on', 'Y', 'YES', 'OK', 'ON', 1, 2);
+ my @yes = (' y', 'yes ', 'ok', 'on', 'Y', 'YES', 'OK', 'ON', 1, 2);
plan tests => scalar @yes * 2;
foreach my $word (@yes) {
ok(boolean($word), "$word is true");
@@ -15,7 +15,7 @@ subtest 'yes' => sub { #YES
};
subtest 'no' => sub { # NO
- my @no = ('n', 'no', 'off', 'not ok', 'N', 'NO', 'OFF', 'NOTOK', 0);
+ my @no = ('n ', ' no', 'off', 'not ok', 'N', 'NO', 'OFF', 'NOTOK', 0);
plan tests => scalar @no * 2;
foreach my $word (@no) {
ok(!boolean($word), "$word is false");
Please sign in to comment.
Something went wrong with that request. Please try again.