Ambrosia
view release on metacpan or search on metacpan
lib/Ambrosia/core/Nil.pm view on Meta::CPAN
package Ambrosia::core::Nil;
use strict;
use warnings;
use overload
'%{}' => sub { {} },
'@{}' => sub { [] },
'${}' => sub { my $e=undef; \$e; },
'&{}' => \&sub_nil,
'*{}' => sub { shift },
'bool'=> sub {0},
'""' => sub {''},
'0+' => sub {0},
'fallback' => 1
;
our $VERSION = 0.010;
our $AUTOLOAD;
{
my $SINGLETON;
sub new
{
my $proto = shift;
return $SINGLETON ||= bless [], ref $proto || $proto;
}
}
sub sub_nil
{
my $obj = shift;
sub { $obj };
}
sub TO_JSON
{
return {};
}
sub AUTOLOAD
{
unless ( defined wantarray ) # don't bother doing more
{
goto \&sub_nil;
}
elsif ( wantarray )
{
return ();
}
return $_[0];
}
1;
__END__
=head1 NAME
Ambrosia::core::Nil - implement pattern NullObject.
=head1 VERSION
version 0.010
=head1 SYNOPSIS
use Ambrosia::core::Nil;
$obj = new Ambrosia::core::Nil(@arg);
$obj->foo(); #It's work and not invoke exeption.
$obj->()->()->foo(); #And it's work too.
@a = $obj->foo(); #return empty array
$b = $obj->foo(); #return object of Ambrosia::core::Nil
#with string concatenation $obj return empty string
$s = "foo" . $obj; #$s eq 'foo'
$i = 10 + $obj; #$i == 10
#%$obj is empty hash
#@$obj is empty array
unless ( $obj )
{
print "The object of type Ambrosia::core::Nil allthase is false.\n";
}
=head1 DESCRIPTION
( run in 1.825 second using v1.01-cache-2.11-cpan-5735350b133 )