Created
January 14, 2009 01:37
-
-
Save motemen/46733 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment