File Coverage

blib/lib/Object/Generic/False.pm
Criterion Covered Total %
statement 17 20 85.0
branch n/a
condition 1 2 50.0
subroutine 9 11 81.8
pod 0 3 0.0
total 27 36 75.0


line stmt bran cond sub pod time code
1             #
2             # Object::Generic::False.pm
3             #
4             # A perl object that evaluates to a boolean false value
5             # but which still allows method calls.
6             #
7             # use Object::Generic::False qw( false );
8             # my $n = false; # returns global $Object::Generic::False::_false_
9             # print "n is false" if not $n;
10             # print "n->foo->bar is also false" if not $n->foo->bar;
11             #
12             # See the bottom of this file for the documentation.
13             #
14             # $Id: False.pm 378 2005-06-07 19:00:32Z mahoney $
15             #
16             package Object::Generic::False;
17 3     3   47607 use strict;
  3         6  
  3         933  
18 3     3   21 use warnings;
  3         6  
  3         873  
19             our @ISA = qw(Exporter);
20             our @EXPORT_OK = qw(false);
21             our $_false_ = new Object::Generic::False; # The global returned by false().
22             use overload
23 17     17   82 q("") => sub {return ''}, # Also autogenerates numeric false as 0.
24 4     4   20 q(<=>) => sub {return 0 <=> $_[1]},
25 4     4   22 q(cmp) => sub {return '' cmp $_[1]},
26             # --- arithmetic ---
27 3         49 q(+) => \&false,
28             q(-) => \&false,
29             q(neg) => \&false,
30             q(*) => \&false,
31             q(/) => \&false,
32             q(%) => \&false,
33             q(**) => \&false,
34             # --- strings -------
35             #q(.) => \&false,
36             q(x) => \&false,
37             # --- bits ----------
38             q(&) => \&false,
39             q(|) => \&false,
40             q(~) => \&false, # Should this perhaps return a (true) Object::Generic?
41 3     3   6213 ;
  3         4030  
42             sub new {
43 6     6 0 12 my $class = shift;
44 6   50     697 my $false = shift || 0;
45 6         36 return bless \$false => $class;
46             }
47 0     0   0 sub DESTROY { # Defined here so that AUTOLOAD won't handle it.
48             }
49             sub AUTOLOAD {
50 5     5   30 return shift;
51             }
52             sub false {
53 26     26 0 174 return $_false_;
54             }
55             sub error {
56 0     0 0   my $self = shift;
57 0           return $$self;
58             }
59              
60             1;
61              
62             __END__