← Index
NYTProf Performance Profile   « block view • line view • sub view »
For mentat.storage.mongo.pl
  Run on Tue Jun 24 10:04:38 2014
Reported on Tue Jun 24 10:05:07 2014

Filename/usr/local/share/perl/5.14.2/boolean.pm
StatementsExecuted 68 statements in 1.00ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
1112.01ms2.98msboolean::::BEGIN@31 boolean::BEGIN@31
11124µs24µsMongoDB::Cursor::::BEGIN@1MongoDB::Cursor::BEGIN@1
44421µs21µsboolean::::import boolean::import
202220µs20µsboolean::::false boolean::false
11110µs44µsboolean::::BEGIN@10 boolean::BEGIN@10
1117µs10µsboolean::::BEGIN@3 boolean::BEGIN@3
1117µs52µsboolean::::BEGIN@14 boolean::BEGIN@14
1117µs11µsboolean::::BEGIN@3.10 boolean::BEGIN@3.10
1112µs2µsboolean::::true boolean::true
0000s0sboolean::::TO_JSON boolean::TO_JSON
0000s0sboolean::::__ANON__[:10] boolean::__ANON__[:10]
0000s0sboolean::::__ANON__[:11] boolean::__ANON__[:11]
0000s0sboolean::::boolean boolean::boolean
0000s0sboolean::::isBoolean boolean::isBoolean
0000s0sboolean::::isFalse boolean::isFalse
0000s0sboolean::::isTrue boolean::isTrue
0000s0sboolean::::truth boolean::truth
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1251µs124µs
# spent 24µs within MongoDB::Cursor::BEGIN@1 which was called: # once (24µs+0s) by MongoDB::Cursor::BEGIN@25 at line 1
use 5.005003;
# spent 24µs making 1 call to MongoDB::Cursor::BEGIN@1
2package boolean;
34109µs429µs
# spent 10µs (7+3) within boolean::BEGIN@3 which was called: # once (7µs+3µs) by MongoDB::Cursor::BEGIN@25 at line 3 # spent 11µs (7+5) within boolean::BEGIN@3.10 which was called: # once (7µs+5µs) by MongoDB::Cursor::BEGIN@25 at line 3
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
51600ns$boolean::VERSION = '0.30';
6
71300nsmy ($true, $false);
8
9use 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
'""' => sub { ${$_[0]} },
11 '!' => sub { ${$_[0]} ? $false : $true },
12234µs278µs fallback => 1;
# spent 44µs making 1 call to boolean::BEGIN@10 # spent 34µs making 1 call to overload::import
13
142194µs297µ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
use base 'Exporter';
# spent 52µs making 1 call to boolean::BEGIN@14 # spent 45µs making 1 call to base::import
151900ns@boolean::EXPORT = qw(true false boolean);
161500ns@boolean::EXPORT_OK = qw(isTrue isFalse isBoolean);
1713µ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
sub import {
231633µs my @options = grep $_ ne '-truth', @_;
24 $_[0]->truth if @options != @_;
25 @_ = @options;
264117µs goto &Exporter::import;
# spent 117µs making 4 calls to Exporter::import, avg 29µs/call
27}
28
291100nsmy ($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
BEGIN {
3214147µs my $have_readonly = eval { require Readonly };
33
34 my $t = 1;
35 my $f = 0;
36 $true = do {bless \$t, 'boolean'};
37 $false = do {bless \$f, 'boolean'};
38
39 if ( $have_readonly ) {
40134µs Readonly::Scalar($t => $t);
# spent 34µs making 1 call to Readonly::Scalar
41111µs Readonly::Scalar($f => $f);
# spent 11µs making 1 call to Readonly::Scalar
42 }
43
44129µs $true_val = overload::StrVal($true);
# spent 29µs making 1 call to overload::AddrRef
45111µs $false_val = overload::StrVal($false);
# spent 11µs making 1 call to overload::AddrRef
46 $bool_vals = {$true_val => 1, $false_val => 1};
471388µs12.98ms}
# spent 2.98ms making 1 call to boolean::BEGIN@31
48
4914µ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
sub true() { $true }
502033µ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
sub false() { $false }
51sub 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}
57sub isTrue($) {
58 not(defined $_[0]) ? false :
59 (overload::StrVal($_[0]) eq $true_val) ? true : false;
60}
61sub isFalse($) {
62 not(defined $_[0]) ? false :
63 (overload::StrVal($_[0]) eq $false_val) ? true : false;
64}
65sub isBoolean($) {
66 not(defined $_[0]) ? false :
67 (exists $bool_vals->{overload::StrVal($_[0])}) ? true : false;
68}
69
70sub 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
82sub TO_JSON { ${$_[0]} ? \1 : \0 }
83
8416µs1;
85
86=encoding utf8
87
88=head1 NAME
89
90boolean - 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
101and:
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
110and:
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
119Most programming languages have a native C<Boolean> data type.
120Perl does not.
121
122Perl has a simple and well known Truth System. The following scalar
123values are false:
124
125 $false1 = undef;
126 $false2 = 0;
127 $false3 = 0.0;
128 $false4 = '';
129 $false5 = '0';
130
131Every other scalar value is true.
132
133This module provides basic Boolean support, by defining two special
134objects: C<true> and C<false>.
135
136=head1 RATIONALE
137
138When sharing data between programming languages, it is important to
139support the same group of basic types. In Perlish programming languages,
140these types include: Hash, Array, String, Number, Null and Boolean. Perl
141lacks native Boolean support.
142
143Data interchange modules like YAML and JSON can now C<use boolean> to
144encode/decode/roundtrip Boolean values.
145
146=head1 FUNCTIONS
147
148This module defines the following functions:
149
150=over
151
152=item true
153
154This function returns a scalar value which will evaluate to true. The
155value is a singleton object, meaning there is only one "true" value in a
156Perl 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
161This function returns a scalar value which will evaluate to false. The
162value is a singleton object, meaning there is only one "false" value in
163a Perl process at any time. You can check to see whether the value is
164the "false" object with the isFalse function described below.
165
166=item boolean($scalar)
167
168Casts the scalar value to a boolean value. If C<$scalar> is true, it
169returns C<boolean::true>, otherwise it returns C<boolean::false>.
170
171=item isTrue($scalar)
172
173Returns C<boolean::true> if the scalar passed to it is the
174C<boolean::true> object. Returns C<boolean::false> otherwise.
175
176=item isFalse($scalar)
177
178Returns C<boolean::true> if the scalar passed to it is the
179C<boolean::false> object. Returns C<boolean::false> otherwise.
180
181=item isBoolean($scalar)
182
183Returns C<boolean::true> if the scalar passed to it is the
184C<boolean::true> or C<boolean::false> object. Returns C<boolean::false>
185otherwise.
186
187=back
188
189=head1 METHODS
190
191Since true and false return objects, you can call methods on them.
192
193=over
194
195=item $boolean->isTrue
196
197Same as isTrue($boolean).
198
199=item $boolean->isFalse
200
201Same as isFalse($boolean).
202
203=back
204
205=head1 USE OPTIONS
206
207By default this module exports the C<true>, C<false> and C<boolean> functions.
208
209The module also defines these export tags:
210
211=over
212
213=item :all
214
215Exports C<true>, C<false>, C<boolean>, C<isTrue>, C<isFalse>, C<isBoolean>
216
217=back
218
219=head2 -truth
220
221You can specify the C<-truth> option to override truth operators to return
222C<boolean> values.
223
224 use boolean -truth;
225 print ref("hello" eq "world"), "\n";
226
227Prints:
228
229 boolean
230
231C<-truth> can be used with the other import options.
232
233=head1 JSON SUPPORT
234
235JSON.pm will encode Perl data with boolean.pm values correctly if you use the
236C<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
245Ingy döt Net <ingy@cpan.org>
246
247=head1 COPYRIGHT
248
249Copyright (c) 2007, 2008, 2010, 2011, 2013. Ingy döt Net.
250
251This program is free software; you can redistribute it and/or modify it
252under the same terms as Perl itself.
253
254See http://www.perl.com/perl/misc/Artistic.html
255
256=cut