File Coverage

blib/lib/JSON/DWIW/Boolean.pm
Criterion Covered Total %
statement 24 32 75.0
branch 2 6 33.3
condition 1 3 33.3
subroutine 8 10 80.0
pod 3 4 75.0
total 38 55 69.0


line stmt bran cond sub pod time code
1             # Creation date: 2007-05-10 20:29:02
2             # Authors: don
3             #
4             # Copyright (c) 2007-2010 Don Owens . 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 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 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     36   314 use strict;
  36         67  
  36         1295  
51 36     36   178 use warnings;
  36         74  
  36         868  
52              
53 36     36   561 use 5.006_00;
  36         119  
  36         5246  
54              
55             package JSON::DWIW::Boolean;
56              
57             use overload
58 12 100   12   435 bool => sub { my $self = shift; my $val = $$self; return $val ? 1 : 0; },
  12         20  
  12         66  
59 36 0   36   69192 '0+' => sub { my $self = shift; my $val = $$self; return $val ? 1 : 0; };
  36     0   50233  
  36         440  
  0         0  
  0         0  
  0         0  
60            
61              
62             our $VERSION = sprintf("%d.%02d",(q$Revision: 1.4 $ =~ /\d+/g));
63              
64              
65             =pod
66              
67             =head1 METHODS
68              
69             =head2 C
70              
71             Returns an object initialized with $val as its boolean value.
72              
73             =cut
74              
75             sub new {
76 10     10 1 11 my $proto = shift;
77 10         13 my $val = shift;
78              
79 10         12 my $obj = $val;
80            
81 10   33     58 my $self = bless \$obj, ref($proto) || $proto;
82            
83 10         101 return $self;
84             }
85              
86             =pod
87              
88             =head2 C
89              
90             Class method that returns a new object initialized to a true value.
91              
92             =cut
93              
94             sub true {
95 5     5 1 1399 my $proto = shift;
96 5         15 return $proto->new(1);
97             }
98              
99             =pod
100              
101             =head2 C
102              
103             Class method that returns a new object initialized to a false value.
104              
105             =cut
106              
107             sub false {
108 5     5 1 2142 my $proto = shift;
109 5         19 return $proto->new(0);
110             }
111              
112             sub as_bool {
113 0     0 0   my $self = shift;
114 0           my $val = $$self;
115              
116 0 0         if ($val) {
117 0           return 1;
118             }
119 0           return;
120             }
121              
122              
123             =pod
124              
125             =head1 AUTHOR
126              
127             Don Owens
128              
129             =head1 LICENSE AND COPYRIGHT
130              
131             Copyright (c) 2007-2010 Don Owens . 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             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: