The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Math::BooleanEval - Boolean expression parser

SYNOPSIS

 use Math::BooleanEval;
 my $bool = Math::BooleanEval->new('yes|no');

 # evaluate each defined item in the expression to 1 or 0
 foreach my $item (@{$bool->{'arr'}}){
    next unless defined $item;
    $item = ($item =~ m/^no|off|false|null$/i) ? 0 : 1;
 }

 # evaluate the expression
 print $bool->eval();

DESCRIPTION

BooleanEval parses a boolean expression and creates an array of elements in the expression. By setting each element to 1 or 0 you can evaluate the expression. The expression is parsed on the standard boolean delimiters:

 & | () ! ? :

Using BooleanEval involves three steps: instantiate an object, loop through the elements of the expression setting each to 1 or 0, then call eval();

To create a new object, call new(), passing the expression as the single argument:

 $bool = BooleanEval->new('yes|no');

Generally the easiest way to set each element is to use a foreach loop:

 foreach my $item (@{$bool->{'arr'}}){
    next unless defined $item;
        $item = ($item =~ m/^no|off|false|null$/i) ? 1 : 0;
 }

Notice that the first thing to do at the top of the loop is to check if the item is defined. If it is not defined leave it as it is. Otherwise, use the item for whatever checks you like. In the example above we test if the item is one of the standard English words for false. Set the item to 1 or 0, nothing else.

Finally, get the evaluation of the expression with the eval() method:

 print $bool->eval();

PUBLIC INTERFACE

Math::BooleanEval->new(expression)

Instantiates a BooleanEval object.

$bool->reset()

Resets the object for another round of setting and evaluating. The object automatically resets after eval() unless you set the auto_reset property to false (by default it's true), so it's not usually necessary to reset after calling eval.

$bool->eval()

Evaluates the expression. By the time you call this method you should have set all elements in the {'arr'} array to 1, 0, or left them undefined if that's how they were to begin with. See examples above.

syntaxcheck()

Returns true if the expression is syntacally valid, false if not. For example, "Me & You" would return true but "Me & | You" would return false.

syntaxcheck can be called as either an object method:

  if ($bool->syntaxcheck)

or as a static sub:

  if (Math::BooleanEval::syntaxcheck($expr))

TERMS AND CONDITIONS

Copyright (c) 2000 by Miko O'Sullivan. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. This software comes with NO WARRANTY of any kind.

AUTHOR

Miko O'Sullivan miko@idocs.com

Created: Sometime around the end of the twentieth century. I'm kind of vague on the exact date.

VERSION

  Version 0.9    December 18, 2000
  Original release.
  
  Version 0.91   Oct 15, 2001
  Same as 0.9, but hopefully more properly packaged for CPAN.  We'll see.
  
  Version 1.00   July 11, 2002
  Added reset method.
  Cleaned up documentation.