Useful base class for deriving other modules
package My::Funky::Module; use base qw( Class::Base ); # custom initialiser method sub init { my ($self, $config) = @_; # copy various params into $self $self->params($config, qw( FOO BAR BAZ )) || return undef; # to indicate a failure return $self->error('bad constructor!') if $something_bad; # or to indicate general happiness and well-being return $self; } package main; # new() constructor folds args into hash and calls init() my $object = My::Funky::Module->new( foo => 'bar', ... ) || die My::Funky::Module->error(); # error() class/object method to get/set errors $object->error('something has gone wrong'); print $object->error(); # debugging() method (de-)activates the debug() method $object->debugging(1); # debug() prints to STDERR if debugging enabled $object->debug('The ', $animal, ' sat on the ', $place);
Please consider using Badger::Base instead which is the successor of this module.
This module implements a simple base class from which other modules can be derived, thereby inheriting a number of useful methods such as \*(C`new()\*(C', \*(C`init()\*(C', \*(C`params()\*(C', \*(C`clone()\*(C', \*(C`error()\*(C' and \*(C`debug()\*(C'.
For a number of years, I found myself re-writing this module for practically every Perl project of any significant size. Or rather, I would copy the module from the last project and perform a global search and replace to change the names. Each time it got a little more polished and eventually, I decided to Do The Right Thing and release it as a module in it's own right.
It doesn't pretend to be an all-encompassing solution for every kind of object creation problem you might encounter. In fact, it only supports blessed hash references that are created using the popular, but by no means universal convention of calling \*(C`new()\*(C' with a list or reference to a hash array of named parameters. Constructor failure is indicated by returning undef and setting the $ERROR package variable in the module's class to contain a relevant message (which you can also fetch by calling \*(C`error()\*(C' as a class method).
e.g.
my $object = My::Module->new( file => 'myfile.html', msg => 'Hello World' ) || die $My::Module::ERROR;
or:
my $object = My::Module->new({ file => 'myfile.html', msg => 'Hello World', }) || die My::Module->error();
The \*(C`new()\*(C' method handles the conversion of a list of arguments into a hash array and calls the \*(C`init()\*(C' method to perform any initialisation. In many cases, it is therefore sufficient to define a module like so:
package My::Module; use Class::Base; use base qw( Class::Base );
sub init { my ($self, $config) = @_; # copy some config items into $self $self->params($config, qw( FOO BAR )) || return undef; return $self; }
# ...plus other application-specific methods
1;
Then you can go right ahead and use it like this:
use My::Module;
my $object = My::Module->new( FOO => 'the foo value', BAR => 'the bar value' ) || die $My::Module::ERROR;
Despite its limitations, Class::Base can be a surprisingly useful module to have lying around for those times where you just want to create a regular object based on a blessed hash reference and don't want to worry too much about duplicating the same old code to bless a hash, define configuration values, provide an error reporting mechanism, and so on. Simply derive your module from \*(C`Class::Base\*(C' and leave it to worry about most of the detail. And don't forget, you can always redefine your own \*(C`new()\*(C', \*(C`error()\*(C', or other method, if you don't like the way the Class::Base version works.
This module is what object-oriented afficionados would describe as an \*(L"abstract base class\*(R". That means that it's not designed to be used as a stand-alone module, rather as something from which you derive your own modules. Like this:
package My::Funky::Module use base qw( Class::Base );
You can then use it like this:
use My::Funky::Module;
my $module = My::Funky::Module->new();
If you want to apply any per-object initialisation, then simply write an \*(C`init()\*(C' method. This gets called by the \*(C`new()\*(C' method which passes a reference to a hash reference of configuration options.
sub init { my ($self, $config) = @_;
...
return $self; }
When you create new objects using the \*(C`new()\*(C' method you can either pass a hash reference or list of named arguments. The \*(C`new()\*(C' method does the right thing to fold named arguments into a hash reference for passing to the \*(C`init()\*(C' method. Thus, the following are equivalent:
# hash reference my $module = My::Funky::Module->new({ foo => 'bar', wiz => 'waz', });
# list of named arguments (no enclosing '{' ... '}') my $module = My::Funky::Module->new( foo => 'bar', wiz => 'waz' );
Within the \*(C`init()\*(C' method, you can either handle the configuration yourself:
sub init { my ($self, $config) = @_;
$self->{ file } = $config->{ file } || return $self->error('no file specified');
return $self; }
or you can call the \*(C`params()\*(C' method to do it for you:
sub init { my ($self, $config) = @_;
$self->params($config, 'file') || return $self->error('no file specified');
return $self; }
The \*(C`init()\*(C' method should return $self to indicate success or undef to indicate a failure. You can use the \*(C`error()\*(C' method to report an error within the \*(C`init()\*(C' method. The \*(C`error()\*(C' method returns undef, so you can use it like this:
sub init { my ($self, $config) = @_;
# let's make 'foobar' a mandatory argument $self->{ foobar } = $config->{ foobar } || return $self->error("no foobar argument");
return $self; }
When you create objects of this class via \*(C`new()\*(C', you should now check the return value. If undef is returned then the error message can be retrieved by calling \*(C`error()\*(C' as a class method.
my $module = My::Funky::Module->new() || die My::Funky::Module->error();
Alternately, you can inspect the $ERROR package variable which will contain the same error message.
my $module = My::Funky::Module->new() || die $My::Funky::Module::ERROR;
Of course, being a conscientious Perl programmer, you will want to be sure that the $ERROR package variable is correctly defined.
package My::Funky::Module use base qw( Class::Base );
our $ERROR;
You can also call \*(C`error()\*(C' as an object method. If you pass an argument then it will be used to set the internal error message for the object and return undef. Typically this is used within the module methods to report errors.
sub another_method { my $self = shift;
...
# set the object error return $self->error('something bad happened'); }
If you don't pass an argument then the \*(C`error()\*(C' method returns the current error value. Typically this is called from outside the object to determine its status. For example:
my $object = My::Funky::Module->new() || die My::Funky::Module->error();
$object->another_method() || die $object->error();
The module implements two methods to assist in writing debugging code: debug() and debugging(). Debugging can be enabled on a per-object or per-class basis, or as a combination of the two.
When creating an object, you can set the \*(C`DEBUG\*(C' flag (or lower case \*(C`debug\*(C' if you prefer) to enable or disable debugging for that one object.
my $object = My::Funky::Module->new( debug => 1 ) || die My::Funky::Module->error();
my $object = My::Funky::Module->new( DEBUG => 1 ) || die My::Funky::Module->error();
If you don't explicitly specify a debugging flag then it assumes the value of the $DEBUG package variable in your derived class or 0 if that isn't defined.
You can also switch debugging on or off via the \*(C`debugging()\*(C' method.
$object->debugging(0); # debug off $object->debugging(1); # debug on
The \*(C`debug()\*(C' method examines the internal debugging flag (the \*(C`_DEBUG\*(C' member within the $self hash) and if it finds it set to any true value then it prints to \s-1STDERR\s0 all the arguments passed to it. The output is prefixed by a tag containing the class name of the object in square brackets (but see the \*(C`id()\*(C' method below for details on how to change that value).
For example, calling the method as:
$object->debug('foo', 'bar');
prints the following output to \s-1STDERR:\s0
[My::Funky::Module] foobar
When called as class methods, \*(C`debug()\*(C' and \*(C`debugging()\*(C' instead use the $DEBUG package variable in the derived class as a flag to control debugging. This variable also defines the default \*(C`DEBUG\*(C' flag for any objects subsequently created via the new() method.
package My::Funky::Module use base qw( Class::Base );
our $ERROR; our $DEBUG = 0 unless defined $DEBUG;
# some time later, in a module far, far away package main;
# debugging off (by default) my $object1 = My::Funky::Module->new();
# turn debugging on for My::Funky::Module objects $My::Funky::Module::DEBUG = 1;
# alternate syntax My::Funky::Module->debugging(1);
# debugging on (implicitly from $DEBUG package var) my $object2 = My::Funky::Module->new();
# debugging off (explicit override) my $object3 = My::Funky::Module->new(debug => 0);
If you call \*(C`debugging()\*(C' without any arguments then it returns the value of the internal object flag or the package variable accordingly.
print "debugging is turned ", $object->debugging() ? 'on' : 'off';
Class constructor method which expects a reference to a hash array of parameters or a list of \*(C`name => value\*(C' pairs which are automagically folded into a hash reference. The method blesses a hash reference and then calls the \*(C`init()\*(C' method, passing the reference to the hash array of configuration parameters.
Returns a reference to an object on success or undef on error. In the latter case, the \*(C`error()\*(C' method can be called as a class method, or the $ERROR package variable (in the derived class' package) can be inspected to return an appropriate error message.
my $object = My::Class->new( foo => 'bar' ) # params list || die $My::Class::$ERROR; # package var
or
my $object = My::Class->new({ foo => 'bar' }) # params hashref || die My::Class->error; # class method
Object initialiser method which is called by the \*(C`new()\*(C' method, passing a reference to a hash array of configuration parameters. The method may be derived in a subclass to perform any initialisation required. It should return $self on success, or \*(C`undef\*(C' on error, via a call to the \*(C`error()\*(C' method.
package My::Module; use base qw( Class::Base );
sub init { my ($self, $config) = @_;
# let's make 'foobar' a mandatory argument $self->{ foobar } = $config->{ foobar } || return $self->error("no foobar argument");
return $self; } The \*(C`params()\*(C' method accept a reference to a hash array as the first argument containing configuration values such as those passed to the \*(C`init()\*(C' method. The second argument can be a reference to a list of parameter names or a reference to a hash array mapping parameter names to default values. If the second argument is not a reference then all the remaining arguments are taken as parameter names. Thus the method can be called as follows:
sub init { my ($self, $config) = @_;
# either... $self->params($config, qw( foo bar ));
# or... $self->params($config, [ qw( foo bar ) ]);
# or... $self->params($config, { foo => 'default foo value', bar => 'default bar value' } );
return $self; }
The method looks for values in $config corresponding to the keys specified and copies them, if defined, into $self.
Keys can be specified in \s-1UPPER\s0 \s-1CASE\s0 and the method will look for either upper or lower case equivalents in the $config hash. Thus you can call \*(C`params()\*(C' from \*(C`init()\*(C' like so:
sub init { my ($self, $config) = @_; $self->params($config, qw( FOO BAR )) return $self; }
but use either case for parameters passed to \*(C`new()\*(C':
my $object = My::Module->new( FOO => 'the foo value', BAR => 'the bar value' ) || die My::Module->error();
my $object = My::Module->new( foo => 'the foo value', bar => 'the bar value' ) || die My::Module->error();
Note however that the internal key within $self used to store the value will be in the case provided in the call to \*(C`params()\*(C' (upper case in this example). The method doesn't look for upper case equivalents when they are specified in lower case.
When called in list context, the method returns a list of all the values corresponding to the list of keys, some of which may be undefined (allowing you to determine which values were successfully set if you need to). When called in scalar context it returns a reference to the same list.
The \*(C`clone()\*(C' method performs a simple shallow copy of the object hash and creates a new object blessed into the same class. You may want to provide your own \*(C`clone()\*(C' method to perform a more complex cloning operation.
my $clone = $object->clone();
General purpose method for getting and setting error messages. When called as a class method, it returns the value of the $ERROR package variable (in the derived class' package) if called without any arguments, or sets the same variable when called with one or more arguments. Multiple arguments are concatenated together.
# set error My::Module->error('set the error string'); My::Module->error('set ', 'the ', 'error string');
# get error print My::Module->error(); print $My::Module::ERROR;
When called as an object method, it operates on the \*(C`_ERROR\*(C' member of the object, returning it when called without any arguments, or setting it when called with arguments.
# set error $object->error('set the error string');
# get error print $object->error();
The method returns \*(C`undef\*(C' when called with arguments. This allows it to be used within object methods as shown:
sub my_method { my $self = shift;
# set error and return undef in one return $self->error('bad, bad, error') if $something_bad; } Prints all arguments to \s-1STDERR\s0 if the internal \*(C`_DEBUG\*(C' flag (when called as an object method) or $DEBUG package variable (when called as a class method) is set to a true value. Otherwise does nothing. The output is prefixed by a string of the form \*(L"[Class::Name]\*(R" where the name of the class is that returned by the \*(C`id()\*(C' method.
Used to get (no arguments) or set ($flag defined) the value of the internal \*(C`_DEBUG\*(C' flag (when called as an object method) or $DEBUG package variable (when called as a class method).
The \*(C`debug()\*(C' method calls this method to return an identifier for the object for printing in the debugging message. By default it returns the class name of the object (i.e. \*(C`ref $self\*(C'), but you can of course subclass the method to return some other value. When called with an argument it uses that value to set its internal \*(C`_ID\*(C' field which will be returned by subsequent calls to \*(C`id()\*(C'.
Andy Wardley <[email protected]>
This is version 0.04 of Class::Base.
This module began life as the Template::Base module distributed as part of the Template Toolkit.
Thanks to Brian Moseley and Matt Sergeant for suggesting various enhancments, some of which went into version 0.02.
Version 0.04 was uploaded by Gabor Szabo.
Copyright (C) 1996-2012 Andy Wardley. All Rights Reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.