blib/lib/JSON/DWIW/Boolean.pm

filter: Statementstmt 75.7% 25 / 33 Branchbran 33.3% 2 / 6 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.0% 8 / 10 Podpod 75.0% 3 / 4 totaltotal 67.2% 39 / 58 CC 5 SCAR 8.3
CC
5
Cov
67%
as_bool
6.0
__ANON__
3.0
new
2.1
CRAP 2.3
« blib/lib/JSON/DWIW.pm ↑ Summary blib/lib/JSON/DWIW/Changes.pm »
1 # Creation date: 2007-05-10 20:29:02
2 # Authors: don
3 #
4 # Copyright (c) 2007-2010 Don Owens <don@regexguy.com>. All rights reserved.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the Perl Artistic license. You should have received a copy of the
8 # Artistic license with this distribution, in the file named
9 # "Artistic". You may also obtain a copy from
10 # http://regexguy.com/license/Artistic
11 #
12 # This program is distributed in the hope that it will be
13 # useful, but WITHOUT ANY WARRANTY; without even the implied
14 # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 # PURPOSE.
16  
17 =pod
18  
19 =head1 NAME
20  
21 JSON::DWIW::Boolean - Return a true or false value when evaluated
22 in boolean context -- to be used with JSON::DWIW->encode() to
23 explicitly specify a boolean value.`
24  
25 =head1 SYNOPSIS
26  
27 use JSON::DWIW;
28 my $val1 = JSON::DWIW->true;
29 my $val2 = JSON::DWIW->false;
30
31 # or
32
33 use JSON::DWIW::Boolean;
34 my $val1 = JSON::DWIW::Boolean->new(1); # true value
35 my $val2 = JSON::DWIW::Boolean->new(0); # false value
36  
37 =head1 DESCRIPTION
38  
39 This module is not intended to be used directly. It is intended
40 to be used as part of L<JSON::DWIW> to specify that a true or false
41 value should be output when converting to JSON, since Perl does
42 not have explicit values for true and false.
43  
44 Overloading is used, so if a L<JSON::DWIW::Boolean> object is
45 evaluated in boolean context, it will evaluate to 1 or 0,
46 depending on whether the object was initialized to true or false.
47  
48 =cut
49  
50 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 36 ▶use 5.006_00;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 package JSON::DWIW::Boolean;
56  
57 use overload
58 12 ▶ bool => sub { my $self = shift; my $val = $$self; return $val ? 1 : 0; },
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 7 5 12
59 0 ▶ '0+' => sub { my $self = shift; my $val = $$self; return $val ? 1 : 0; };
Subroutine __ANON__: not called CC=2 SCAR=10.9
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 0 0 0
60
61  
62 36 our $VERSION = sprintf("%d.%02d",(q$Revision: 1.4 $ =~ /\d+/g));
63  
64  
65 =pod
66  
67 =head1 METHODS
68  
69 =head2 C<new($val)>
70  
71 Returns an object initialized with $val as its boolean value.
72  
73 =cut
74  
75 10 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
76 10 my $proto = shift;
77 10 my $val = shift;
78  
79 10 my $obj = $val;
80
81 10 ▶ my $self = bless \$obj, ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0100
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
82
83 10 return $self;
84 }
85  
86 =pod
87  
88 =head2 C<true()>
89  
90 Class method that returns a new object initialized to a true value.
91  
92 =cut
93  
94 5 ▶sub true {
Subroutine true: called CC=1 SCAR=0.0
Pod documented
95 5 my $proto = shift;
96 5 return $proto->new(1);
97 }
98  
99 =pod
100  
101 =head2 C<false()>
102  
103 Class method that returns a new object initialized to a false value.
104  
105 =cut
106  
107 5 ▶sub false {
Subroutine false: called CC=1 SCAR=0.0
Pod documented
108 5 my $proto = shift;
109 5 return $proto->new(0);
110 }
111  
112 0 ▶sub as_bool {
Subroutine as_bool: not called CC=2 SCAR=17.9
Pod undocumented
113 0 my $self = shift;
114 0 my $val = $$self;
115  
116 0 ▶ if ($val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val) 0 0 0
117 0 return 1;
118 }
119 0 return;
120 }
121  
122  
123 =pod
124  
125 =head1 AUTHOR
126  
127 Don Owens <don@regexguy.com>
128  
129 =head1 LICENSE AND COPYRIGHT
130  
131 Copyright (c) 2007-2010 Don Owens <don@regexguy.com>. All rights reserved.
132  
133 This is free software; you can redistribute it and/or modify it
134 under the same terms as Perl itself. See perlartistic.
135  
136 This program is distributed in the hope that it will be
137 useful, but WITHOUT ANY WARRANTY; without even the implied
138 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
139 PURPOSE.
140  
141 =cut
142  
143 36 1;
144  
145 # Local Variables: #
146 # mode: perl #
147 # tab-width: 4 #
148 # indent-tabs-mode: nil #
149 # cperl-indent-level: 4 #
150 # perl-indent-level: 4 #
151 # End: #
152 # vim:set ai si et sta ts=4 sw=4 sts=4:
« blib/lib/JSON/DWIW.pm ↑ Summary blib/lib/JSON/DWIW/Changes.pm »