File Coverage

blib/lib/Lazy/Bool.pm
Criterion Covered Total %
statement 40 40 100.0
branch 6 6 100.0
condition 2 3 66.6
subroutine 19 19 100.0
pod 4 4 100.0
total 71 72 98.6


line stmt bran cond sub pod time code
1             package Lazy::Bool;
2              
3 3     3   43357 use 5.010000;
  3         10  
  3         104  
4 3     3   16 use strict;
  3         4  
  3         81  
5 3     3   12 use warnings;
  3         6  
  3         83  
6 3     3   15 use Exporter 'import';
  3         4  
  3         967  
7              
8             our $VERSION = '0.06';
9             our @EXPORT_OK = qw(lzb);
10              
11             sub lzb(&) {
12 3     3 1 2205 my $code = shift;
13 3         15 __PACKAGE__->new($code);
14             }
15              
16             sub new {
17 109     109 1 18166 my ( $type, $code ) = @_;
18 109   66     299 my $klass = ref($type) || $type;
19 109 100   2   241 my $ref = ( ref($code) eq 'CODE' ) ? $code : sub { $code };
  2         6  
20              
21 109         426 bless $ref => $klass;
22             }
23              
24             sub true {
25 4     4 1 12 shift->new( sub { 1 } );
  4     4   3257  
26             }
27              
28             sub false {
29 7     7 1 21 shift->new( sub { 0 } );
  6     6   39  
30             }
31              
32             use overload
33 3         34 'bool' => \&_to_bool,
34             '&' => \&_lazy_and,
35             '|' => \&_lazy_or,
36 3     3   5458 '!' => \&_lazy_neg;
  3         3547  
37              
38             sub _to_bool {
39 101     101   743 shift->();
40             }
41              
42             sub _lazy_and {
43 44     44   793 my ( $a, $b ) = @_;
44              
45             $a->new(
46             sub {
47 36     36   80 my $real = $a->_to_bool;
48              
49 36 100       165 return $real unless $real;
50              
51 18         52 $real & $b;
52             }
53 44         208 );
54             }
55              
56             sub _lazy_or {
57 36     36   96 my ( $a, $b ) = @_;
58              
59             $a->new(
60             sub {
61 30     30   66 my $real = $a->_to_bool;
62              
63 30 100       133 return $real if $real;
64              
65 12         42 $real | $b;
66             }
67 36         181 );
68             }
69              
70             sub _lazy_neg {
71 66     66   884 my $a = shift;
72             $a->new(
73             sub {
74 48     48   102 !$a->_to_bool;
75             }
76 66         270 );
77             }
78              
79             1;
80             __END__