File Coverage

blib/lib/Scalar/Boolean.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1 2     2   25114 use strict;
  2         5  
  2         82  
2 2     2   12 use warnings;
  2         4  
  2         106  
3              
4             # ABSTRACT: Makes scalar variables store Boolean values only
5             package Scalar::Boolean;
6              
7 2     2   20 use base qw( Exporter );
  2         5  
  2         673  
8             our @EXPORT = qw(
9             boolean
10             bool booleanise booleanize
11             unbool unbooleanise unbooleanize
12             );
13              
14             my $use_variable_magic = 1;
15              
16             eval { require Variable::Magic };
17             if ($@) {
18             $use_variable_magic = 0;
19             }
20              
21             if ($use_variable_magic) {
22             require Scalar::Boolean::VM;
23              
24             *booleanise = *Scalar::Boolean::VM::booleanise;
25             *unbooleanise = *Scalar::Boolean::VM::unbooleanise;
26             }
27             else {
28             require Scalar::Boolean::Tie;
29              
30             *booleanise = *Scalar::Boolean::Tie::booleanise;
31             *unbooleanise = *Scalar::Boolean::Tie::unbooleanise;
32             }
33              
34             *bool = *booleanize = *booleanise;
35             *unbool = *unbooleanize = *unbooleanise;
36             *boolean = *Scalar::Boolean::Value::boolean;
37              
38             1;
39              
40              
41              
42             =pod
43              
44             =head1 NAME
45              
46             Scalar::Boolean - Makes scalar variables store Boolean values only
47              
48             =head1 VERSION
49              
50             version 1.02
51              
52             =head1 SYNOPSIS
53              
54             use Scalar::Boolean;
55              
56             bool my $value;
57             $value = []; # $value gets set to 1
58             $value = 'Perl'; # $value gets set to 1
59             $value = ''; # $value gets set to 0
60             $value = '0'; # $value gets set to 0
61             $value = undef; # $value gets set to 0
62             $value = (); # $value gets set to 0
63              
64             unbool $value;
65             $value = 'foo'; # $value gets set to 'foo'
66              
67             boolean []; # returns 1
68             boolean undef; # returns 0
69              
70             =head1 METHODS
71              
72             =head2 C or C or C
73              
74             Accepts scalar variables which will be Cd. Once Cd,
75             the variable will convert all values that are assigned to it to their
76             corresponding Boolean values. No effect on already Cd variables.
77              
78             =head2 C or C or C
79              
80             Accepts scalar variables which will be Cd if already
81             Cd. No effect on not already Cd variables.
82              
83             =head2 C
84              
85             Accepts a single value and returns its boolean value without affecting its
86             original value.
87              
88             =head1 PERFORMANCE
89              
90             For performance reasons, Scalar::Boolean prefers L if it is
91             installed, else uses L for magic.
92              
93             =head1 ACKNOWLEDGEMENT
94              
95             Many thanks to B (B) for suggesting several improvements, for
96             valuable suggestions and also for sending sample code. Thank you Eric! :-)
97              
98             =head1 AUTHOR
99              
100             Alan Haggai Alavi
101              
102             =head1 COPYRIGHT AND LICENSE
103              
104             This software is copyright (c) 2012 by Alan Haggai Alavi.
105              
106             This is free software; you can redistribute it and/or modify it under
107             the same terms as the Perl 5 programming language system itself.
108              
109             =cut
110              
111              
112             __END__