Class::Modular -- Modular class generation superclass
package Foo; use base qw(Class::Modular); sub new { my $class = shift; my $self = bless {}, ref($class) || $class; $self->SUPER::_init(@_); return $self; } [...] package Bar; sub method_that_bar_provides{ print qq(Hello World!\n); } sub _methods($$){ return qw(method_that_bar_provides); } [...] use Foo; $foo = new Foo; $foo->load('Bar'); $foo->method_that_bar_provides;
Class::Modular is a superclass for generating modular classes, where methods can be added into the class from the perspective of the object, rather than the perspective of the class.
That is, you can create a class which has a set of generic common functions. Less generic functions can be included or overridden without modifying the base classes. This allows for code to be more modular, and reduces code duplication.
This module attempts to fill the middle ground between Class::Mutator and true classless OOP, like Class::Classless.
$obj = Foo::Bar->new();
Creates a new Foo::Bar object
Aditional arguments can be passed to this creator, and they are stored in $self->{creation_args} (and $self->{$cm}{creation_args} by _init. You can also override the new method in your subclass. It's just provided here for completeness.
$self->_init(@args);
Stores the arguments used at new so modules that are loaded later can read them from creation_args
You can also override this method, but if you do so, you should call Class::Modular::_init($self,@_) if you don't set creation_args.
$db->load('FOO::Subclass');
Loads the named subclass into this object if the named subclass has not been loaded.
The options scalar is passed to $subclass::_methods when determining which methods should be added using _addmethods.
The subclasses _init method is called right after methods are loaded.
If debugging is enabled, will warn about loading already loaded subclasses.
$self->_addmethods()
Given an array of methods, adds the methods into the _methodhash calling table.
Methods that have previously been overridden by override are _NOT_ overridden again. This may need to be adjusted in load.
$obj->override('methodname', $code_ref)
TRUE on success, FALSE on failure.
Allows you to override utility functions that are called internally to provide a different default function.
It's superficially similar to _addmethods, which is called by load, but it deals with code references, and requires the method name to be known.
Methods overridden here are _NOT_ overrideable in _addmethods. This may need to be changed.
my $clone = $obj->clone
Produces a clone of the object with duplicates of all data and/or new connections as appropriate.
Calls _clone on all loaded subclasses.
Warns if debugging is on for classes which don't have a _clone method. Dies on other errors.
$obj->can('METHOD'); Class::Modular->can('METHOD');
Replaces UNIVERSAL's can method so that handled methods are reported correctly. Calls UNIVERSAL::can in the places where we don't know anything it doesn't.
A coderef to the method if the method is supported, undef otherwise.
Scalar Method Name
$obj->handledby('methodname'); $obj->handledby('Class::Method::methodname');
Returns the subclass that handles this method.
SCALAR subclass name
SCALAR method name
Called by perl.
Calls all subclass _destroy methods.
Subclasses need only implement a _destroy method if they have references that need to be uncircularized, or things that should be disconnected or closed.
The AUTOLOAD function is responsible for calling child methods which have been installed into the current Class::Modular handle.
Subclasses that have a new function as well as an AUTOLOAD function must call Class::Modular::AUTOLOAD and set $Class::Modular::AUTOLOAD
$Class::Modular::AUTOLOAD = $AUTOLOAD; goto &Class::Modular::AUTOLOAD;
Because this module works through AUTOLOAD, utilities that use can($object) instead of $object->can() will fail to see routines that are actually there. Params::Validate, an excellent module, is currently one of these offenders.
This module is part of DA, Don Armstrong's Modules, and is released under the terms of the GPL version 2, or any later version. See the file README and COPYING for more information.
Copyright 2003, 2004 by Don Armstrong <don@donarmstrong.com>
To install Class::Modular, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::Modular
CPAN shell
perl -MCPAN -e shell install Class::Modular
For more information on module installation, please visit the detailed CPAN module installation guide.