Lazy::Bool - Boolean wrapper lazy
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.
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 |
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 });
Returns a lazy true value
use Lazy::Bool; my $true = Lazy::Bool::true;
Returns a lazy false value
use Lazy::Bool; my $false = Lazy::Bool::false;
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"
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"
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!
Helper to create an instance.
use Lazy::Bool qw(lzb); my $a = 6; my $b = 4; my $condition = lzb { $a > $b };
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");
This package can export the helper lzbc to easily create a new instance of Lazy::Bool
Scalar::Lazy and Scalar::Defer
Tiago Peczenyj, <tiago.peczenyj@gmail.com>
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.
To install Lazy::Bool, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Lazy::Bool
CPAN shell
perl -MCPAN -e shell install Lazy::Bool
For more information on module installation, please visit the detailed CPAN module installation guide.