NAME

Lazy::Bool - Boolean wrapper lazy

SYNOPSIS

  use Lazy::Bool;

  my $result = Lazy::Bool->new(sub{  
  	# complex boolean expression
  });

  ...
  if($result) { # now we evaluate the expression
	
  }

Using this module you can play with lazy booleans. Using expressions &, | and ! you can delay the expression evaluation until necessary.

DESCRIPTION

This is a proof-of-concept for a boolean wrapper using lazy initialization using pure perl.

The expression will be evaluated in boolean context, like

if($lazy_boolean) { }
unless($lazy_boolean) { }

$lazy_boolean && $other  # for a lazy operation use the &
$lazy_boolean || $other  # for a lazy operation use the | 

METHODS

new

The constructor, can receive one expression or a subroutine reference.

use Lazy::Bool;

my $result1 = Lazy::Bool->new( 1 );

my $result2 = Lazy::Bool->new(sub{ 
  $a > $b && $valid 
});

true

Returns a lazy true value

use Lazy::Bool;

my $true = Lazy::Bool::true;

false

Returns a lazy false value

use Lazy::Bool;

my $false = Lazy::Bool::false;

Overloaded Operators

Bit and '&'

Used as a logical and (&&), you can create operations between lazy booleans and scalars (will be changed to lazy).

  use Lazy::Bool;

  my $true = Lazy::Bool::true;
  my $false = Lazy::Bool::false;

  my $result = $true & $false;
	
  print "success" unless $result; # now will be evaluated!
	

Important: There is no shortcut if the first value is "false"

Bit or '|'

Used as a logical or (||), you can create operations between lazy booleans and scalars (will be changed to lazy).

use Lazy::Bool;

my $true = Lazy::Bool::true;
my $false = Lazy::Bool::false;

my $result = $true | $false;

print "success" if $result; # now will be evaluated!

Important: There is no shortcut if the first value is "true"

Negation (!)

Used as a logical negation (not), you can create a lazy negation.

use Lazy::Bool;

my $false = Lazy::Bool::false;

my $result = ! $false;

print "success" if $result; # now will be evaluated!

EXAMPLES

A complex example:

use Lazy::Bool;
use Test::More tests=> 3;
my $a = 6;
my $b = 4;
my $x  = Lazy::Bool->new(sub{ $a > $b });
my $false = Lazy::Bool::false;

my $result = ($x | $false) & ( ! ( $false & ! $false ) );

# now the expressions will be evaluate
ok($result,    "complex expression should be true");
ok(!! $x ,  "double negation of true value should be true");	
ok(!!! $false, "truple negation of false value should be true");	

TODO

There are lots of things to do. Please send me feedback! It is justa a P.O.C. but it can be useful in some situation.

For example, how we can add support to shortcut operations? or support && and || ?

And how about avoid evaluate the same boolean twice?

EXPORT

None

SEE ALSO

Scalar::Lazy and Scalar::Defer

AUTHOR

Tiago Peczenyj, <tiago.peczenyj@gmail.com>

COPYRIGHT AND LICENSE

Copyright (C) 2013 by Tiago Peczenyj

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available.