The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Lingua::Boolean - comprehensively parse boolean response strings

VERSION

version 0.002

SYNOPSIS

    use Lingua::Boolean;

    # Use functional/procedural interface
    print "Do it? ";
    chomp(my $response = <>);
    if ( boolean $response ) {   # YES, y, OK, 1...
        print "OK, doing it.\n";
    }
    else {                      # no, N, 0...
        print "OK, not doing it.\n";
    }

    # Once more, with feeling
    print "Fait-le? ";
    chomp($response = <>);
    if ( boolean $response, 'fr' ) {    # OUI
        print "OK, on le fait.\n";
    }
    else {                              # non
        print "OK, on ne le fait pas.\n";
    }

    # Or, use OO interface
    my $bool = Lingua::Boolean->new('en');
    print "Do it? ";
    chomp($response = <>);
    if ($bool->boolean($response)) {
        print "OK, doing it!\n";
    }
    else {
        print "OK, not doing it.\n";
    }

DESCRIPTION

Does that string look like they said "true" or "false"? To know, you have to check a lot of things. Lingua::Boolean attempts to do that in a single module, and do so for multiple languages.

METHODS

Lingua::Boolean provides both a functional/procedural and object-oriented interfaces. Everything described below is an object method, but can also be called as a function. boolean() is exported by default, and can be called that way - everything else requires the fully-qualified name.

    use Lingua::Boolean;
    my @languages = Lingua::Boolean::languages();
    print boolean('yes') . "\n"; # boolean is exported by default

import

Calling import() will, obviously, import subs into your namespace. By default, Lingua::Boolean imports the sub boolean(). All other subs should be accessed with the object-oriented interface, or use the fully qualified name.

new

new() creates a new Lingua::Boolean object. You can optionally give it the code for the language you'll be working with, and only that language will be loaded. If you do so, you needn't pass the language to every call to boolean():

    use Lingua::Boolean qw();
    my $bool = Lingua::Boolean->new('fr');
    print ($bool->boolean('oui') ? "TRUE\n" : "FALSE\n");

Otherwise, boolean() accept the language code as the second parameter:

    use Lingua::Boolean qw();
    my $bool = Lingua::Boolean->new();
    print ($bool->boolean('oui', 'fr') ? "TRUE\n" : "FALSE\n");

boolean

boolean() tries to determine if the string looks true or looks false, and returns true or false accordingly. If both tests fail, dies. By default, uses en; pass a language code as the second parameter to check another language. Croaks if the language is unknown to Lingua::Boolean (or the Lingua::Boolean object, if used as an object method).

    use Lingua::Boolean qw();
    my $bool = Lingua::Boolean->new();
    print ($bool->boolean('yes') ? "TRUE\n" : "FALSE\n");

If you specify the language in the constructor, you needn't specify it in the call to boolean():

    use Lingua::Boolean qw();
    my $bool = Lingua::Boolean->new('fr');
    print ($bool->boolean('OUI') ? "TRUE\n" : "FALSE\n");

This sub is exported by default, and can be used functionally:

    use Lingua::Boolean;
    print (boolean('yes') ? "TRUE\n" : "FALSE\n");

languages

languages() returns the list of languages that Lingua::Boolean knows about.

    use Lingua::Boolean;
    my @languages = Lingua::Boolean::languages(); # qw(English Français ...)

When called as an object method, returns the languages that that object knows about:

    use Lingua::Boolean qw();
    my $bool = Lingua::Boolean->new('fr');
    my @languages = $bool->languages(); # qw(Français)

langs

langs() returns the list of language codes that Lingua::Boolean knows about.

    use Lingua::Boolean;
    my @lang_codes = Lingua::Boolean::langs(); # qw(en fr ...)

When called as an object method, returns the languages that that object knows about:

    use Lingua::Boolean qw();
    my $bool = Lingua::Boolean->new('fr');
    my @lang_codes = $bool->langs(); # qw(fr)

EXPORTS

By default, Lingua::Boolean exports boolean(). All other methods must be fully qualified - or use the object-oriented interface.

AVAILABILITY

The latest version of this module is available from the Comprehensive Perl Archive Network (CPAN). Visit http://www.perl.com/CPAN/ to find a CPAN site near you, or see http://search.cpan.org/dist/Lingua-Boolean/.

The development version lives at http://github.com/doherty/Lingua-Boolean and may be cloned from git://github.com/doherty/Lingua-Boolean. Instead of sending patches, please fork this project using the standard git and github infrastructure.

BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests through the web interface at http://github.com/doherty/Lingua-Boolean/issues.

AUTHOR

Mike Doherty <doherty@cs.dal.ca>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2010 by Mike Doherty.

This is free software, licensed under:

  The GNU General Public License, Version 3, June 2007