Tiago Peczenyj > Lazy-Bool-0.06 > Lazy::Bool

Download:
Lazy-Bool-0.06.tar.gz

Dependencies

Annotate this POD

Website

View/Report Bugs
Module Version: 0.06   Source  

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: Will shortcut the boolean evaluation 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: Will shortcut the boolean evaluation 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!

Functions

lzb

Helper to create an instance.

  use Lazy::Bool qw(lzb);

  my $a = 6;
  my $b = 4;
  my $condition = lzb { $a > $b };

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");      

EXPORT ^

This package can export the helper lzbc to easily create a new instance of Lazy::Bool

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.

syntax highlighting: