Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
@@ -108,6 +108,7 @@ to check another language. Croaks if the language is unknown to L<Lingua::Boolea
108 108 sub looks_true {
109 109 my $to_test = shift;
110 110 my $lang = shift || 'en';
  111 + _trim($to_test);
111 112
112 113 croak "I don't know anything about the language '$lang'" unless exists $regexes->{$lang};
113 114 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
125 126 sub looks_false {
126 127 my $to_test = shift;
127 128 my $lang = shift || 'en';
  129 + _trim($to_test);
128 130
129 131 croak "I don't know anything about the language '$lang'" unless exists $regexes->{$lang};
130 132 return true if ($to_test ~~ $regexes->{$lang}->{False});
@@ -143,6 +145,7 @@ is unknown to L<Lingua::Boolean>.
143 145 sub boolean {
144 146 my $to_test = shift;
145 147 my $lang = shift || 'en';
  148 + _trim($to_test);
146 149
147 150 if (looks_true($to_test, $lang)) {
148 151 return true;
@@ -162,6 +165,13 @@ By default, L<Lingua::Boolean> exports C<boolean()>.
162 165
163 166 =cut
164 167
  168 +sub _trim { # http://www.perlmonks.org/?node_id=36684
  169 + @_ = $_ if not @_ and defined wantarray;
  170 + @_ = @_ if defined wantarray;
  171 + for (@_ ? @_ : $_) { s/^\s+|\s+$//g }
  172 + return wantarray ? @_ : $_[0] if defined wantarray;
  173 +}
  174 +
165 175 1;
166 176
167 177 __END__
4 t/lang/en.t
@@ -11,7 +11,7 @@ my @langs = Lingua::Boolean::langs();
11 11 ok($lang ~~ @langs, "$lang is available");
12 12
13 13 subtest 'yes' => sub { #YES
14   - my @yes = ('y', 'yes', 'ok', 'on', 'Y', 'YES', 'OK', 'ON', 'true', 1, 2);
  14 + my @yes = (' y', 'yes ', 'ok', 'on', ' Y', 'YES', 'OK', 'ON', 'true', 1, 2);
15 15 plan tests => scalar @yes;
16 16
17 17 foreach my $word (@yes) {
@@ -20,7 +20,7 @@ subtest 'yes' => sub { #YES
20 20 };
21 21
22 22 subtest 'no' => sub { # NO
23   - my @no = ('n', 'no', 'off', 'not ok', 'N', 'NO', 'OFF', 'NOTOK', 'FAlse', 0);
  23 + my @no = ('n ', ' no', 'off', 'not ok', 'N', 'NO', 'OFF', 'NOTOK', 'FAlse', 0);
24 24 plan tests => scalar @no;
25 25
26 26 foreach my $word (@no) {
4 t/lang/fr.t
@@ -11,7 +11,7 @@ my @langs = Lingua::Boolean::langs();
11 11 ok($lang ~~ @langs, "$lang is available");
12 12
13 13 subtest 'yes' => sub { #YES
14   - my @yes = ('oui', 'ok', 'vrai', 1);
  14 + my @yes = (' oui', 'ok ', 'vrai', 1);
15 15 plan tests => scalar @yes;
16 16
17 17 foreach my $word (@yes) {
@@ -20,7 +20,7 @@ subtest 'yes' => sub { #YES
20 20 };
21 21
22 22 subtest 'no' => sub { # NO
23   - my @no = ('n', 'no', 'non', 'faux', 0);
  23 + my @no = ('n ', ' no', 'non', 'faux', 0);
24 24 plan tests => scalar @no;
25 25
26 26 foreach my $word (@no) {
4 t/lang/none.t
@@ -6,7 +6,7 @@ use Test::More 0.94 tests => 3;
6 6 use Lingua::Boolean;
7 7
8 8 subtest 'yes' => sub { #YES
9   - my @yes = ('y', 'yes', 'ok', 'on', 'Y', 'YES', 'OK', 'ON', 1, 2);
  9 + my @yes = (' y', 'yes ', 'ok', 'on', 'Y', 'YES', 'OK', 'ON', 1, 2);
10 10 plan tests => scalar @yes * 2;
11 11 foreach my $word (@yes) {
12 12 ok(boolean($word), "$word is true");
@@ -15,7 +15,7 @@ subtest 'yes' => sub { #YES
15 15 };
16 16
17 17 subtest 'no' => sub { # NO
18   - my @no = ('n', 'no', 'off', 'not ok', 'N', 'NO', 'OFF', 'NOTOK', 0);
  18 + my @no = ('n ', ' no', 'off', 'not ok', 'N', 'NO', 'OFF', 'NOTOK', 0);
19 19 plan tests => scalar @no * 2;
20 20 foreach my $word (@no) {
21 21 ok(!boolean($word), "$word is false");

0 comments on commit 2b6a750

Please sign in to comment.
Something went wrong with that request. Please try again.