Class-Null
view release on metacpan or search on metacpan
can create the object the usual way, using the "new()" constructor, and
call any method on it, and all methods will do the same - nothing.
(Actually, it always returns the same "Class::Null" singleton object,
enabling method chaining.) It's effectively a catch-all object. We can
use this class with our own object like this:
package MyObject;
use Class::Null;
# some class constructor and accessor declaration here
sub init {
my $self = shift;
...
$self->log(Class::Null->new);
...
}
sub do_it {
my $self = shift;
$self->log->log(level => 'debug', message => 'starting to do it');
...
$self->log->log(level => 'debug', message => 'still doing it');
...
$self->log->log(level => 'debug', message => 'finished doing it');
}
This is only one example of using a null class, but it can be used
whenever you want to make an optional helper object into a mandatory
helper object, thereby avoiding unnecessarily complicated checks and
preserving the transparency of how your objects are related to each
other and how they call each other.
Although "Class::Null" is exceedingly simple it has been made into a
distribution and put on CPAN to avoid further clutter and repetitive
definitions.
METHODS
new
Returns the singleton null object.
Any other method
Returns another singleton null object so method chaining works.
OVERLOADS
Boolean context
In boolean context, a null object always evaluates to false.
Numeric context
When used as a number, a null object always evaluates to 0.
String context
When stringified, a null object always evaluates to the empty
string.
INSTALLATION
See perlmodinstall for information and options on installing Perl
modules.
BUGS AND LIMITATIONS
No bugs have been reported.
Please report any bugs or feature requests through the web interface at
<http://rt.cpan.org/Public/Dist/Display.html?Name=Class-Null>.
AVAILABILITY
The latest version of this module is available from the Comprehensive
Perl Archive Network (CPAN). Visit <http://www.perl.com/CPAN/> to find a
CPAN site near you, or see <http://search.cpan.org/dist/Class-Null/>.
The development version lives at <http://github.com/hanekomu/Class-Null>
and may be cloned from <git://github.com/hanekomu/Class-Null.git>.
Instead of sending patches, please fork this project using the standard
git and github infrastructure.
AUTHOR
Marcel Gruenauer <marcel@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2005 by Marcel Gruenauer.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
( run in 1.297 second using v1.01-cache-2.11-cpan-39bf76dae61 )