Filename | /usr/local/share/perl/5.14.2/boolean.pm |
Statements | Executed 68 statements in 1.00ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 2.01ms | 2.98ms | BEGIN@31 | boolean::
1 | 1 | 1 | 24µs | 24µs | BEGIN@1 | MongoDB::Cursor::
4 | 4 | 4 | 21µs | 21µs | import | boolean::
20 | 2 | 2 | 20µs | 20µs | false | boolean::
1 | 1 | 1 | 10µs | 44µs | BEGIN@10 | boolean::
1 | 1 | 1 | 7µs | 10µs | BEGIN@3 | boolean::
1 | 1 | 1 | 7µs | 52µs | BEGIN@14 | boolean::
1 | 1 | 1 | 7µs | 11µs | BEGIN@3.10 | boolean::
1 | 1 | 1 | 2µs | 2µs | true | boolean::
0 | 0 | 0 | 0s | 0s | TO_JSON | boolean::
0 | 0 | 0 | 0s | 0s | __ANON__[:10] | boolean::
0 | 0 | 0 | 0s | 0s | __ANON__[:11] | boolean::
0 | 0 | 0 | 0s | 0s | boolean | boolean::
0 | 0 | 0 | 0s | 0s | isBoolean | boolean::
0 | 0 | 0 | 0s | 0s | isFalse | boolean::
0 | 0 | 0 | 0s | 0s | isTrue | boolean::
0 | 0 | 0 | 0s | 0s | truth | boolean::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | 2 | 51µs | 1 | 24µs | # spent 24µs within MongoDB::Cursor::BEGIN@1 which was called:
# once (24µs+0s) by MongoDB::Cursor::BEGIN@25 at line 1 # spent 24µs making 1 call to MongoDB::Cursor::BEGIN@1 |
2 | package boolean; | ||||
3 | 4 | 109µs | 4 | 29µs | use strict; use warnings; # spent 11µs making 1 call to boolean::BEGIN@3.10
# spent 10µs making 1 call to boolean::BEGIN@3
# spent 5µs making 1 call to warnings::import
# spent 3µs making 1 call to strict::import |
4 | |||||
5 | 1 | 600ns | $boolean::VERSION = '0.30'; | ||
6 | |||||
7 | 1 | 300ns | my ($true, $false); | ||
8 | |||||
9 | use overload | ||||
10 | # spent 44µs (10+34) within boolean::BEGIN@10 which was called:
# once (10µs+34µs) by MongoDB::Cursor::BEGIN@25 at line 12 | ||||
11 | '!' => sub { ${$_[0]} ? $false : $true }, | ||||
12 | 2 | 34µs | 2 | 78µs | fallback => 1; # spent 44µs making 1 call to boolean::BEGIN@10
# spent 34µs making 1 call to overload::import |
13 | |||||
14 | 2 | 194µs | 2 | 97µs | # spent 52µs (7+45) within boolean::BEGIN@14 which was called:
# once (7µs+45µs) by MongoDB::Cursor::BEGIN@25 at line 14 # spent 52µs making 1 call to boolean::BEGIN@14
# spent 45µs making 1 call to base::import |
15 | 1 | 900ns | @boolean::EXPORT = qw(true false boolean); | ||
16 | 1 | 500ns | @boolean::EXPORT_OK = qw(isTrue isFalse isBoolean); | ||
17 | 1 | 3µs | %boolean::EXPORT_TAGS = ( | ||
18 | all => [@boolean::EXPORT, @boolean::EXPORT_OK], | ||||
19 | test => [qw(isTrue isFalse isBoolean)], | ||||
20 | ); | ||||
21 | |||||
22 | # spent 21µs within boolean::import which was called 4 times, avg 5µs/call:
# once (6µs+0s) by MongoDB::Cursor::BEGIN@25 at line 25 of MongoDB/Cursor.pm
# once (6µs+0s) by MongoDB::Collection::BEGIN@29 at line 29 of MongoDB/Collection.pm
# once (5µs+0s) by MongoDB::MongoClient::BEGIN@33 at line 33 of MongoDB/MongoClient.pm
# once (5µs+0s) by MongoDB::Connection::BEGIN@34 at line 34 of MongoDB/Connection.pm | ||||
23 | 4 | 7µs | my @options = grep $_ ne '-truth', @_; | ||
24 | 4 | 2µs | $_[0]->truth if @options != @_; | ||
25 | 4 | 3µs | @_ = @options; | ||
26 | 4 | 21µs | 4 | 117µs | goto &Exporter::import; # spent 117µs making 4 calls to Exporter::import, avg 29µs/call |
27 | } | ||||
28 | |||||
29 | 1 | 100ns | my ($true_val, $false_val, $bool_vals); | ||
30 | |||||
31 | # spent 2.98ms (2.01+970µs) within boolean::BEGIN@31 which was called:
# once (2.01ms+970µs) by MongoDB::Cursor::BEGIN@25 at line 47 | ||||
32 | 2 | 96µs | my $have_readonly = eval { require Readonly }; | ||
33 | |||||
34 | 1 | 200ns | my $t = 1; | ||
35 | 1 | 100ns | my $f = 0; | ||
36 | 2 | 35µs | $true = do {bless \$t, 'boolean'}; | ||
37 | 2 | 5µs | $false = do {bless \$f, 'boolean'}; | ||
38 | |||||
39 | 1 | 300ns | if ( $have_readonly ) { | ||
40 | 1 | 2µs | 1 | 34µs | Readonly::Scalar($t => $t); # spent 34µs making 1 call to Readonly::Scalar |
41 | 1 | 1µs | 1 | 11µs | Readonly::Scalar($f => $f); # spent 11µs making 1 call to Readonly::Scalar |
42 | } | ||||
43 | |||||
44 | 1 | 3µs | 1 | 29µs | $true_val = overload::StrVal($true); # spent 29µs making 1 call to overload::AddrRef |
45 | 1 | 1µs | 1 | 11µs | $false_val = overload::StrVal($false); # spent 11µs making 1 call to overload::AddrRef |
46 | 1 | 4µs | $bool_vals = {$true_val => 1, $false_val => 1}; | ||
47 | 1 | 388µs | 1 | 2.98ms | } # spent 2.98ms making 1 call to boolean::BEGIN@31 |
48 | |||||
49 | 1 | 4µs | # spent 2µs within boolean::true which was called:
# once (2µs+0s) by MongoDB::Cursor::explain at line 257 of MongoDB/Cursor.pm | ||
50 | 20 | 33µs | # spent 20µs within boolean::false which was called 20 times, avg 980ns/call:
# 19 times (17µs+0s) by MongoDB::Cursor::next at line 259 of MongoDB/Cursor.pm, avg 900ns/call
# once (2µs+0s) by MongoDB::Cursor::next at line 144 of MongoDB/Collection.pm | ||
51 | sub boolean($) { | ||||
52 | die "Not enough arguments for boolean::boolean" if scalar(@_) == 0; | ||||
53 | die "Too many arguments for boolean::boolean" if scalar(@_) > 1; | ||||
54 | return not(defined $_[0]) ? false : | ||||
55 | "$_[0]" ? $true : $false; | ||||
56 | } | ||||
57 | sub isTrue($) { | ||||
58 | not(defined $_[0]) ? false : | ||||
59 | (overload::StrVal($_[0]) eq $true_val) ? true : false; | ||||
60 | } | ||||
61 | sub isFalse($) { | ||||
62 | not(defined $_[0]) ? false : | ||||
63 | (overload::StrVal($_[0]) eq $false_val) ? true : false; | ||||
64 | } | ||||
65 | sub isBoolean($) { | ||||
66 | not(defined $_[0]) ? false : | ||||
67 | (exists $bool_vals->{overload::StrVal($_[0])}) ? true : false; | ||||
68 | } | ||||
69 | |||||
70 | sub truth { | ||||
71 | # enable modifying true and false | ||||
72 | &Internals::SvREADONLY( \ !!0, 0); | ||||
73 | &Internals::SvREADONLY( \ !!1, 0); | ||||
74 | # turn perl internal booleans into blessed booleans: | ||||
75 | ${ \ !!0 } = $false; | ||||
76 | ${ \ !!1 } = $true; | ||||
77 | # make true and false read-only again | ||||
78 | &Internals::SvREADONLY( \ !!0, 1); | ||||
79 | &Internals::SvREADONLY( \ !!1, 1); | ||||
80 | } | ||||
81 | |||||
82 | sub TO_JSON { ${$_[0]} ? \1 : \0 } | ||||
83 | |||||
84 | 1 | 6µs | 1; | ||
85 | |||||
86 | =encoding utf8 | ||||
87 | |||||
88 | =head1 NAME | ||||
89 | |||||
90 | boolean - Boolean support for Perl | ||||
91 | |||||
92 | =head1 SYNOPSIS | ||||
93 | |||||
94 | use boolean; | ||||
95 | |||||
96 | do &always if true; | ||||
97 | do &never if false; | ||||
98 | |||||
99 | do &maybe if boolean($value)->isTrue; | ||||
100 | |||||
101 | and: | ||||
102 | |||||
103 | use boolean ':all'; | ||||
104 | |||||
105 | $guess = int(rand(2)) % 2 ? true : false; | ||||
106 | |||||
107 | do &something if isTrue($guess); | ||||
108 | do &something_else if isFalse($guess); | ||||
109 | |||||
110 | and: | ||||
111 | |||||
112 | use boolean -truth; | ||||
113 | |||||
114 | die unless ref(42 == 42) eq 'boolean'; | ||||
115 | die unless ("foo" =~ /bar/) eq '0'; | ||||
116 | |||||
117 | =head1 DESCRIPTION | ||||
118 | |||||
119 | Most programming languages have a native C<Boolean> data type. | ||||
120 | Perl does not. | ||||
121 | |||||
122 | Perl has a simple and well known Truth System. The following scalar | ||||
123 | values are false: | ||||
124 | |||||
125 | $false1 = undef; | ||||
126 | $false2 = 0; | ||||
127 | $false3 = 0.0; | ||||
128 | $false4 = ''; | ||||
129 | $false5 = '0'; | ||||
130 | |||||
131 | Every other scalar value is true. | ||||
132 | |||||
133 | This module provides basic Boolean support, by defining two special | ||||
134 | objects: C<true> and C<false>. | ||||
135 | |||||
136 | =head1 RATIONALE | ||||
137 | |||||
138 | When sharing data between programming languages, it is important to | ||||
139 | support the same group of basic types. In Perlish programming languages, | ||||
140 | these types include: Hash, Array, String, Number, Null and Boolean. Perl | ||||
141 | lacks native Boolean support. | ||||
142 | |||||
143 | Data interchange modules like YAML and JSON can now C<use boolean> to | ||||
144 | encode/decode/roundtrip Boolean values. | ||||
145 | |||||
146 | =head1 FUNCTIONS | ||||
147 | |||||
148 | This module defines the following functions: | ||||
149 | |||||
150 | =over | ||||
151 | |||||
152 | =item true | ||||
153 | |||||
154 | This function returns a scalar value which will evaluate to true. The | ||||
155 | value is a singleton object, meaning there is only one "true" value in a | ||||
156 | Perl process at any time. You can check to see whether the value is the | ||||
157 | "true" object with the isTrue function described below. | ||||
158 | |||||
159 | =item false | ||||
160 | |||||
161 | This function returns a scalar value which will evaluate to false. The | ||||
162 | value is a singleton object, meaning there is only one "false" value in | ||||
163 | a Perl process at any time. You can check to see whether the value is | ||||
164 | the "false" object with the isFalse function described below. | ||||
165 | |||||
166 | =item boolean($scalar) | ||||
167 | |||||
168 | Casts the scalar value to a boolean value. If C<$scalar> is true, it | ||||
169 | returns C<boolean::true>, otherwise it returns C<boolean::false>. | ||||
170 | |||||
171 | =item isTrue($scalar) | ||||
172 | |||||
173 | Returns C<boolean::true> if the scalar passed to it is the | ||||
174 | C<boolean::true> object. Returns C<boolean::false> otherwise. | ||||
175 | |||||
176 | =item isFalse($scalar) | ||||
177 | |||||
178 | Returns C<boolean::true> if the scalar passed to it is the | ||||
179 | C<boolean::false> object. Returns C<boolean::false> otherwise. | ||||
180 | |||||
181 | =item isBoolean($scalar) | ||||
182 | |||||
183 | Returns C<boolean::true> if the scalar passed to it is the | ||||
184 | C<boolean::true> or C<boolean::false> object. Returns C<boolean::false> | ||||
185 | otherwise. | ||||
186 | |||||
187 | =back | ||||
188 | |||||
189 | =head1 METHODS | ||||
190 | |||||
191 | Since true and false return objects, you can call methods on them. | ||||
192 | |||||
193 | =over | ||||
194 | |||||
195 | =item $boolean->isTrue | ||||
196 | |||||
197 | Same as isTrue($boolean). | ||||
198 | |||||
199 | =item $boolean->isFalse | ||||
200 | |||||
201 | Same as isFalse($boolean). | ||||
202 | |||||
203 | =back | ||||
204 | |||||
205 | =head1 USE OPTIONS | ||||
206 | |||||
207 | By default this module exports the C<true>, C<false> and C<boolean> functions. | ||||
208 | |||||
209 | The module also defines these export tags: | ||||
210 | |||||
211 | =over | ||||
212 | |||||
213 | =item :all | ||||
214 | |||||
215 | Exports C<true>, C<false>, C<boolean>, C<isTrue>, C<isFalse>, C<isBoolean> | ||||
216 | |||||
217 | =back | ||||
218 | |||||
219 | =head2 -truth | ||||
220 | |||||
221 | You can specify the C<-truth> option to override truth operators to return | ||||
222 | C<boolean> values. | ||||
223 | |||||
224 | use boolean -truth; | ||||
225 | print ref("hello" eq "world"), "\n"; | ||||
226 | |||||
227 | Prints: | ||||
228 | |||||
229 | boolean | ||||
230 | |||||
231 | C<-truth> can be used with the other import options. | ||||
232 | |||||
233 | =head1 JSON SUPPORT | ||||
234 | |||||
235 | JSON.pm will encode Perl data with boolean.pm values correctly if you use the | ||||
236 | C<convert_blessed> option: | ||||
237 | |||||
238 | use JSON; | ||||
239 | use boolean -truth; | ||||
240 | my $json = JSON->new->convert_blessed; | ||||
241 | say $json->encode({false => (0 == 1)}); # Says: '{"false":false}', | ||||
242 | |||||
243 | =head1 AUTHOR | ||||
244 | |||||
245 | Ingy döt Net <ingy@cpan.org> | ||||
246 | |||||
247 | =head1 COPYRIGHT | ||||
248 | |||||
249 | Copyright (c) 2007, 2008, 2010, 2011, 2013. Ingy döt Net. | ||||
250 | |||||
251 | This program is free software; you can redistribute it and/or modify it | ||||
252 | under the same terms as Perl itself. | ||||
253 | |||||
254 | See http://www.perl.com/perl/misc/Artistic.html | ||||
255 | |||||
256 | =cut |