The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Class::Modular -- Modular class generation superclass

SYNOPSIS

     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;

DESCRIPTION

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.

FUNCTIONS

new

Usage

     $obj = Foo::Bar->new();

Function

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.

_init

Usage

     $self->_init(@args);

Function

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.

load

Usage

     $db->load('FOO::Subclass');

Function

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.

_addmethods

Usage

     $self->_addmethods()

Function

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.

override

Function

     $obj->override('methodname', $code_ref)

Returns

TRUE on success, FALSE on failure.

Function

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.

clone

Usage

     my $clone  = $obj->clone

Function

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.

can

Usage

     $obj->can('METHOD');
     Class::Modular->can('METHOD');

Function

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.

Returns

A coderef to the method if the method is supported, undef otherwise.

Args

Scalar Method Name

handledby

Usage

     $obj->handledby('methodname');
     $obj->handledby('Class::Method::methodname');

Function

Returns the subclass that handles this method.

Returns

SCALAR subclass name

Args

SCALAR method name

DESTROY

Usage

Called by perl.

Function

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.

AUTOLOAD

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;

BUGS

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.

COPYRIGHT

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>