motemen (owner)

Revisions

gist: 46733 Download_button fork
public
Public Clone URL: git://gist.github.com/46733.git
Embed All Files: show embed
Object/Nil.pm #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package Object::Nil;
use strict;
use warnings;
use overload
    '0+' => sub { 0 },
    '""' => sub { '' },
    'eq' => sub { ref $_[1] && $_[1]->isa(__PACKAGE__) },
    '==' => sub { ref $_[1] && $_[1]->isa(__PACKAGE__) },
    'ne' => sub { not $_[0] eq $_[1] },
    '!=' => sub { not $_[0] == $_[1] },
    fallback => 1;
 
our $Instance;
 
sub new {
    $Instance = bless { }, shift unless defined $Instance;
    $Instance;
}
 
1;
 
__END__
 
=head1 NAME
 
Object::Nil - The nil object
 
=head1 SYNOPSIS
 
use Object::Nil;
my $nil = Object::Nil->new;
"$nil"; # => ''
$nil eq ''; # => undef
 
=head1 DESCRIPTION
 
This module provides the nil object which evaluats to an empty string
in string context and 0 in numeric context, but returns false when
compared with them.
 
=head1 COPYRIGHT
 
Unpokopo
 
=cut