File Coverage

blib/lib/Boolean/String.pm
Criterion Covered Total %
statement 25 25 100.0
branch n/a
condition n/a
subroutine 11 11 100.0
pod 2 2 100.0
total 38 38 100.0


line stmt bran cond sub pod time code
1 4     4   146589 use strict;
  4         10  
  4         156  
2 4     4   22 use warnings;
  4         8  
  4         279  
3              
4             package Boolean::String;
5              
6             # ABSTRACT: Strings with boolean values independent of perl's assumptions
7              
8              
9 4         55 use Sub::Exporter -setup => {
10             exports => [ qw( true false ) ],
11             groups => { default => [ qw( true false ) ] },
12 4     4   5031 };
  4         68569  
13              
14              
15             sub true {
16 5     5 1 648 my $string = shift;
17              
18 5         47 return bless \"$string", 'Boolean::String::True';
19             }
20              
21              
22             sub false {
23 5     5 1 34 my $string = shift;
24              
25 5         47 return bless \"$string", 'Boolean::String::False';
26             }
27              
28              
29             package # hide from PAUSE
30             Boolean::String::True;
31              
32             use overload
33 2     2   43 bool => sub { return 1 },
34 3     3   259 '""' => sub { return ${ shift() } },
  3         24  
35 4         53 fallback => 1,
36 4     4   2233 ;
  4         7  
37              
38              
39             package # hide from PAUSE
40             Boolean::String::False;
41              
42             use overload
43 2     2   13 bool => sub { return 0 },
44 3     3   1879 '""' => sub { return ${ shift() } },
  3         24  
45 4         37 fallback => 1,
46 4     4   577 ;
  4         8  
47              
48             1;
49              
50              
51              
52             =pod
53              
54             =head1 NAME
55              
56             Boolean::String - Strings with boolean values independent of perl's assumptions
57              
58             =head1 VERSION
59              
60             version 0.01
61              
62             =head1 SYNOPSIS
63              
64             use Boolean::String;
65              
66             $message = false 'Record not found';
67              
68             $message = true 'Record found';
69              
70             =head1 DESCRIPTION
71              
72             Boolean::String allows you to overload a string with a value in boolean context. Normally, perl considers all strings except the empty string to be true. Boolean::String allows you to change this assumption.
73              
74             =head1 FUNCTIONS
75              
76             =head2 true
77              
78             This expects a single string, and returns an object that is true in boolean context and the passed in string in string context.
79              
80             $true_string = true '...';
81              
82             =head2 false
83              
84             This expects a single string, and returns an object that is false in boolean context and the passed in string in string context.
85              
86             $false_string = false '...';
87              
88             =head1 IMPORTING
89              
90             The functions are exported by default. Boolean::String uses L for its import/export business. This makes it easy to change the names of the imported functions, like so:
91              
92             # import 'true_because' and 'false_because'
93             use Boolean::String -all => { -suffix => '_because' };
94              
95             # import 'success' and 'failure'
96             use Boolean::String true => { -as => 'success' }, false => { -as => 'failure' };
97              
98             There's a whole slew of flexibility that Sub::Exporter brings to the table, so check it out if your importing needs are more involved than this.
99              
100             =head1 SEE ALSO
101              
102             =head2 L
103              
104             dualvar allows you to have different values for numeric and string contexts. Unfortunately, Boolean::String's functionality cannot be implemented with this (simply setting the numeric value to 0/1), because perl derives a variable's value in boolean context from its value in string context, not numeric context.
105              
106             =head2 L
107              
108             Sub::Exporter handles the importing of the functions, so if you want to do something fancy, that's where you can find out how.
109              
110             =head1 AUTHOR
111              
112             everybody
113              
114             =head1 COPYRIGHT AND LICENSE
115              
116             This software is copyright (c) 2012 by everybody.
117              
118             This is free software; you can redistribute it and/or modify it under
119             the same terms as the Perl 5 programming language system itself.
120              
121             =cut
122              
123              
124             __END__