File Coverage

blib/lib/Data/Bool.pm
Criterion Covered Total %
statement 29 54 53.7
branch 14 32 43.7
condition 1 5 20.0
subroutine 13 17 76.4
pod 2 2 100.0
total 59 110 53.6


line stmt bran cond sub pod time code
1              
2 23     23   1380922 use 5.005;
  23         261  
3              
4             package Data::Bool;
5             $Data::Bool::VERSION = '2.98014';
6              
7             # ABSTRACT: An interface to booleans as objects for Perl
8              
9             BEGIN {
10              
11             # For historical reasons, alias *Data::Bool::Impl with JSON::PP::Boolean
12 23     23   278 *Data::Bool::Impl:: = *JSON::PP::Boolean::;
13              
14             # JSON/PP/Boolean.pm is redundant
15             $INC{'JSON/PP/Boolean.pm'} ||= __FILE__
16 23 50 50     7925 unless $ENV{DATA_BOOL_NICE};
17             }
18              
19             package #
20             Data::Bool::Impl;
21              
22             BEGIN {
23 23     23   24747 require overload;
24 23 50       20560 if ( $ENV{DATA_BOOL_LOUD} ) {
25 0         0 my @o = grep __PACKAGE__->overload::Method($_), qw(0+ ++ --);
26 0         0 my @s = grep __PACKAGE__->can($_), qw(new);
27 0 0       0 push @s, '$VERSION' if $Data::Bool::VERSION;
28 0 0 0     0 if ( @o || @s ) {
29 0         0 my $p = ref do { bless \( my $dummy ), __PACKAGE__ };
  0         0  
30 0         0 my @f;
31 0 0       0 push @f, join( ', ', @s ) if @s;
32 0 0       0 push @f, 'overloads on ' . join( ', ', @o ) if @o;
33 0         0 warn join( ' and ', @f ), qq{ defined for $p elsewhere};
34             }
35             }
36              
37             overload->import(
38 0     0   0 '0+' => sub { ${ $_[0] } },
  0         0  
39 0     0   0 '++' => sub { $_[0] = ${ $_[0] } + 1 },
  0         0  
40 0     0   0 '--' => sub { $_[0] = ${ $_[0] } - 1 },
  0         0  
41 23 100       165 fallback => 1,
42             ) unless __PACKAGE__->overload::Method('0+');
43              
44 46 100   46   3763 *new = sub { bless \( my $dummy = $_[1] ? 1 : 0 ), $_[0] }
45 23 50       2524 unless __PACKAGE__->can('new');
46              
47 23 50       671 $Data::Bool::Impl::VERSION = '2.98014'
48             unless $Data::Bool::Impl::VERSION;
49             }
50              
51             package Data::Bool;
52              
53 23     23   156 use Scalar::Util ();
  23         40  
  23         669  
54              
55 23     23   119 use constant true => Data::Bool::Impl->new(1);
  23         45  
  23         116  
56 23     23   145 use constant false => Data::Bool::Impl->new(0);
  23         38  
  23         75  
57              
58 23     23   145 use constant BOOL_PACKAGE => ref true;
  23         49  
  23         4317  
59              
60 245 100   245 1 34372 sub is_bool ($) { Scalar::Util::blessed( $_[0] ) and $_[0]->isa(BOOL_PACKAGE) }
61              
62 70 100   70 1 25039 sub to_bool ($) { $_[0] ? true : false }
63              
64             @Data::Bool::EXPORT_OK = qw(true false is_bool to_bool BOOL_PACKAGE);
65              
66             BEGIN {
67 23 50   23   1140 if ( "$]" < 5.008003 ) { # Inherit from Exporter (if needed)
68 0         0 require Exporter;
69 0         0 my $EXPORTER_VERSION = Exporter->VERSION;
70 0         0 $EXPORTER_VERSION =~ tr/_//d;
71 0 0       0 push @Data::Bool::ISA, qw(Exporter) if $EXPORTER_VERSION < 5.57;
72             }
73             }
74              
75             sub import { # Load Exporter only if needed
76 7 50   7   9312 return unless @_ > 1;
77              
78 0           require Exporter;
79              
80 23     23   144 no warnings 'redefine';
  23         39  
  23         2436  
81             *import = sub {
82 0 0   0     return unless @_ > 1;
83 0           goto &Exporter::import;
84 0           };
85 0           goto &Exporter::import;
86             }
87              
88             1;