Skip to content

Instantly share code, notes, and snippets.

@motemen
Created January 14, 2009 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motemen/46733 to your computer and use it in GitHub Desktop.
Save motemen/46733 to your computer and use it in GitHub Desktop.
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