Boolean.pm

filter: Statementstmt 100% 70 / 70 Branchbran 88.8% 16 / 18 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 24 / 24 Podpod 100% 1 / 1 totaltotal 98.3% 116 / 118 CC 11 SCAR 6.0
CC
11
Cov
98%
new
4.0
_build_true_or_false
3.0
_str
2.0
CRAP 1.8
↑ Summary Boolean/YesNo.pm »
1 package Object::Boolean;
2  
3 4 ▶use base 'Class::Data::Inheritable';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Sub::Exporter -setup =>
Subroutine BEGIN: called CC=1 SCAR=0.0
5 { exports => { True => \&_build_true_or_false,
6 4 False => \&_build_true_or_false } };
7 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $VERSION = '0.02';
11  
12 # overrideable class data
13 4 __PACKAGE__->mk_classdata( strTrue => 'true' );
14 4 __PACKAGE__->mk_classdata( strFalse => 'false' );
15 4 __PACKAGE__->mk_classdata( numTrue => 1 );
16 4 __PACKAGE__->mk_classdata( numFalse => 0 );
17  
18 # boolean behavior
19 4 ▶use overload 'bool' => \&_is_true;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # String behavior
22 4 ▶use overload '""' => \&_str;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use overload 'eq' => \&_eq;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use overload 'ne' => \&_not_eq;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use overload 'not' => \&_not;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # Numeric behavior
28 4 ▶use overload '0+' => \&_num;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 4 ▶use overload '==' => \&_num_eq;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 ▶use overload '!=' => \&_num_not_eq;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 ▶use overload '!' => \&_not;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # constructor
34 29 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
35 29 my ( $class, $value ) = @_;
36 29 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 29 29
37 29 ▶ bless \(
Branch: 1 branch
BranchTrueFalseTotal
!$value || $value eq $class->strFalse ? : 13 16 29
Condition100%3/3
not $value or $value eq $class->strFalse
l!l&&r!l&&!r
9416
MC/DC100%2/2
not $value or $value eq $class->strFalse
not $value $value eq $class->strFalse
Truth table100%3/3
not(A) or B
A= not $valueB= $value eq $class->strFalse
ABresult
1X1
011
000
38 my $state = ( !$value || $value eq $class->strFalse ? 0 : 1 )
39 ), $class;
40 }
41 # _is_true() and new() depend on $state being a scalar ref. Nothing else does.
42 83 ▶sub _is_true { my($s)=@_; $$s }
Subroutine _is_true: called CC=1 SCAR=0.0
43  
44 # build exportable constants
45 4 ▶sub _build_true_or_false {
Subroutine _build_true_or_false: called CC=3 SCAR=11.0
46 4 my $class = shift;
47 4 my $name = shift;
48 4 ▶ my $method =
Branch: 2 branches
BranchTrueFalseTotal
$name eq 'True' ? : 2 2 4
$name eq 'False' ? : 2 0 2
49 ( $name eq 'True' ? 'numTrue'
50 : $name eq 'False' ? 'numFalse'
51 : die("bad export param : $name") );
52 4 ▶ return sub { $class->new($class->$method) };
Subroutine __ANON__: called CC=1 SCAR=0.0
53 }
54  
55 # functions used in overloading
56 44 ▶sub _str { my($s)=@_; $s->_is_true ? $s->strTrue : $s->strFalse; }
Subroutine _str: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$s->_is_true ? : 16 28 44
57 14 ▶sub _num { my($s)=@_; $s->_is_true ? $s->numTrue : $s->numFalse; }
Subroutine _num: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$s->_is_true ? : 8 6 14
58  
59 25 ▶sub _eq { my($s,$t)=@_; $s->_str eq (ref $t eq ref $s ? $t->_str : $t); }
Subroutine _eq: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $t eq ref $s ? : 11 14 25
60 8 ▶sub _num_eq { my($s,$t)=@_; $s->_num == (ref $t eq ref $s ? $t->_num : $t); }
Subroutine _num_eq: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $t eq ref $s ? : 6 2 8
61  
62 2 ▶sub _not_eq { my($s,$t)=@_; !$s->_eq($t); }
Subroutine _not_eq: called CC=1 SCAR=0.0
63 2 ▶sub _num_not_eq { my($s,$t)=@_; !$s->_num_eq($t);}
Subroutine _num_not_eq: called CC=1 SCAR=0.0
64  
65 7 ▶sub _not {
Subroutine _not: called CC=2 SCAR=6.9
66 7 my ($s) = @_;
67 7 my $class = ref $s;
68 7 ▶ $s->_is_true ? $class->new( $class->numFalse ) : $class->new( $class->numTrue );
Branch: 1 branch
BranchTrueFalseTotal
$s->_is_true ? : 3 4 7
69 }
70  
71 =head1 NAME
72  
73 Object::Boolean - Represent boolean values as objects
74  
75 =head1 SYNOPSIS
76  
77 use Object::Boolean;
78 use Object::Boolean qw/True False/; # ..or export some constants
79 use Object::Boolean # ..or rename those constants
80 True => { -as => 'TRUE' },
81 False => { -as => 'FALSE' };
82  
83 # Create a "false" object by calling new() with a Perl
84 # false value or the word 'false'.
85 my $f = Object::Boolean->new(0);
86 my $f = Object::Boolean->new('');
87 my $f = Object::Boolean->new(2+2==3);
88 my $f = Object::Boolean->new('false');
89  
90 # Create a "true" object by calling new() with anything else
91 my $t = Object::Boolean->new(1);
92 my $t = Object::Boolean->new(2+2==4);
93 my $t = Object::Boolean->new('true');
94 my $t = Object::Boolean->new('elephant');
95  
96 # In boolean context, it behaves like a boolean value.
97 if ($f) { print "it's true" }
98 print "\$f is false" unless $f;
99 print "1+1==2 and it's true" if 1+1==2 && $t;
100  
101 # In string context, it becomes "true" or "false"
102 print "It was a $f alarm.";
103 print "Those are $f teeth.";
104  
105 # Boolean negation produces a new boolean object.
106 print (!$f)." love is hard to find.";
107  
108 # Checking for numeric or string equality with other boolean
109 # objects compares them as though they were in a boolean context.
110 if ($t!=$f) { print "They are not the same." } # like xor
111 if ($t==$t) { print "They are both true or both false." }
112 if (Object::Boolean->new(1) eq Object::Boolean->new(2)) {
113 # this will be true
114 }
115  
116 # Comparison to non-boolean objects treats booleans as strings
117 # for string equality or the numbers 0 or 1 for numeric equality
118 my $true = Object::Boolean->new('true');
119 print "true" if $true eq 'true'; # true
120 print 'true' if $true == 1; # also true
121 print 'true' if $true == 27; # no, not true
122 print 'true' if $true eq 'True'; # not true
123  
124 =head1 DESCRIPTION
125  
126 Package for representing booleans as objects which stringify to true/false
127 and numerify to 0/1. Derived classes can easily stringify/numerify to other
128 values.
129  
130 =head1 FUNCTIONS
131  
132 =over
133  
134 =item new
135  
136 Create a new Object::Boolean object.
137  
138 =back
139  
140 =head1 SEE ALSO
141  
142 Object::Boolean::YesNo -- to stringify to 'Yes' and 'No' instead of 'true' and 'false'.
143  
144 =head1 VERSION
145  
146 Version 0.02
147  
148 =head1 AUTHOR
149  
150 Brian Duggan, C<< <bduggan at matatu.org> >>
151  
152 =head1 LICENSE
153  
154 Copyright 2008 Brian Duggan, all rights reserved.
155  
156 This program is free software; you can redistribute it and/or modify it
157 under the same terms as Perl itself.
158  
159 =cut
160  
161 4 1;
162  
↑ Summary Boolean/YesNo.pm »