Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:Tomcat42
perl-Class-Null
perl-Class-Null.spec
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File perl-Class-Null.spec of Package perl-Class-Null
# # spec file for package perl-Class-Null # # Copyright (c) 2024 SUSE LLC # # All modifications and additions to the file contributed by third parties # remain the property of their copyright owners, unless otherwise agreed # upon. The license for this file, and modifications and additions to the # file, is the same license as for the pristine package itself (unless the # license for the pristine package is not an Open Source License, in which # case the license is the MIT License). An "Open Source License" is a # license that conforms to the Open Source Definition (Version 1.9) # published by the Open Source Initiative. # Please submit bugfixes or comments via https://bugs.opensuse.org/ # %define cpan_name Class-Null Name: perl-Class-Null Version: 2.110.730 Release: 0 %define cpan_version 2.110730 License: Artistic-1.0 OR GPL-1.0-or-later Summary: Implements the Null Class design pattern URL: https://metacpan.org/release/%{cpan_name} Source0: https://cpan.metacpan.org/authors/id/M/MA/MARCEL/%{cpan_name}-%{cpan_version}.tar.gz BuildArch: noarch BuildRequires: perl BuildRequires: perl-macros BuildRequires: perl(ExtUtils::MakeMaker) >= 6.31 BuildRequires: perl(Test::More) >= 0.94 Provides: perl(Class::Null) = %{version} %undefine __perllib_provides %{perl_requires} %description This class implements the Null Class design pattern. Suppose that methods in your object want to write log messages to a log object. The log object is possibly stored in a slot in your object and can be accessed using an accessor method: package MyObject; use base 'Class::Accessor'; __PACKAGE__->mk_accessors(qw(log)); 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'); } The log object simply needs to have a 'log()' method that accepts two named parameters. Any class defining such a method will do, and 'Log::Dispatch' fulfills that requirement while providing a lot of flexibility and reusability in handling the logged messages. You might want to log messages to a file: use Log::Dispatch; my $dispatcher = Log::Dispatch->new; $dispatcher->add(Log::Dispatch::File->new( name => 'file1', min_level => 'debug', filename => 'logfile')); my $obj = MyObject->new(log => $dispatcher); $obj->do_it; But what happens if we don't define a log object? Your object's methods would have to check whether a log object is defined before calling the 'log()' method. This leads to lots of unwieldy code like sub do_it { my $self = shift; if (defined (my $log = $self->log)) { $log->log(level => 'debug', message => 'starting to do it'); } ... if (defined (my $log = $self->log)) { $log->log(level => 'debug', message => 'still doing it'); } ... if (defined (my $log = $self->log)) { $log->log(level => 'debug', message => 'finished doing it'); } } The proliferation of if-statements really distracts from the actual call to 'log()' and also distracts from the rest of the method code. There is a better way. We can ensure that there is always a log object that we can call 'log()' on, even if it doesn't do very much (or in fact, anything at all). This object with null functionality is what is called a null object. We 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. %prep %autosetup -n %{cpan_name}-%{cpan_version} %build perl Makefile.PL INSTALLDIRS=vendor %make_build %check make test %install %perl_make_install %perl_process_packlist %perl_gen_filelist %files -f %{name}.files %doc Changes README %license LICENSE %changelog
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor