1 # This module is part of DA, Don Armstrong's Modules, and is released
2 # under the terms of the GPL version 2, or any later version. See the
3 # file README and COPYING for more information. Copyright 2003 by Don
4 # Armstrong <don@donarmstrong.com>.
5 # $Id: Modular.pm,v 1.7 2003/11/05 04:55:14 don Exp $
7 package Class::Modular;
11 Class::Modular -- Modular class generation superclass
17 @ISA = qw(Class::Modular);
22 Class::Modular is a superclass for generating modular classes, where
23 methods can be added into the class from the perspective of the
24 object, rather than the perspective of the class.
26 That is, you can create a class which has a set of generic common
27 functions. Less generic functions can be included or overridden
28 without modifying the base classes. This allows for code to be more
29 modular, and reduces code duplication.
35 new is responsible for blessing and creating a new database superclass.
39 load is responsible for loading database plugins
49 use vars qw($VERSION $DEBUG);
53 use Data::Copy qw(deep_copy); # Used for deep copying objects
56 ($VERSION) = q$Revision: 1.7 $ =~ /\$Revision:\s+([^\s+])/;
57 $DEBUG = 0 unless defined $DEBUG;
65 Usage : $obj = Foo::Bar->new();
66 Function: Creates a new Foo::Bar object
67 Returns : A new Foo::Bar object
70 Aditional arguments can be passed to this creator, and they are
71 stored in $self->{_creation_args}. You can also override the new
72 method in your subclass. It's just provided here for completeness.
77 my ($class,@args) = @_;
79 # We shouldn't be called $me->new, but just in case
80 $class = ref($class) || $class;
85 $self->{_creation_args} = [@args];
94 Usage : $db->load('FOO::Subclass');
95 Function: loads a Class::Modular subclass
97 Args : SCALAR subclass SCALAR options
99 Loads the named subclass into this object if the named subclass has
102 The options scalar is passed to $subclass::_methods when determining
103 which methods should be added using _addmethods.
105 The subclasses _init method is called right after methods are loaded.
107 If debugging is enabled, will warn about loading already loaded
114 my ($self,$subclass,$options) = @_;
118 # check to see if the subclass has already been loaded.
120 if (not defined $self->{_subclasses}->{$subclass}){
123 $self->_addmethods($subclass,&{"${subclass}::_methods"}($self,$options));
124 &{"${subclass}::_init"}($self);
127 $self->{_subclasses}->{$subclass} = {};
130 carp "Not reloading subclass $subclass" if $DEBUG;
137 Usage : $self->_addmethods()
138 Function: Adds the passed methods into the function table
140 Args : ARRAY of methods
142 Given an array of methods, adds the methods into the _methodhash
145 Methods that have previously been overridden by override are _NOT_
146 overridden again. This may need to be adjusted in load.
150 sub _addmethods($@) {
151 my ($self,$subclass,@methods) = @_;
153 # stick the method into the table
154 # DLA: Make with the munchies!
156 foreach my $method (@methods) {
157 if (not $method =~ /^$subclass/) {
158 $method = $subclass.$method;
160 my ($method_name) = $method =~ /\:*([^\:]+)\s*$/;
161 if (exists $self->{_methodhash}->{$method_name}) {
162 if ($self->{_methodhash}->{$method_name}->{overridden}) {
163 carp "Not overriding already overriden method $method_name\n" if $DEBUG;
166 carp "Overriding $method_name $self->{_methodhash}->{$method_name}->{reference} with $method\n";
168 $self->{_methodhash}->{$method_name}->{reference} = $method;
169 $self->{_methodhash}->{$method_name}->{subclass} = $subclass;
177 Usage : $obj->override('methodname', $code_ref)
178 Function: Overrides the method methodname and calls $code_ref instead.
179 Returns : TRUE on success, FALSE on failure.
180 Args : SCALAR method name
181 CODEREF function reference
183 Allows you to override utility functions that are called internally
184 to provide a different default function.
186 It's superficially similar to _addmethods, which is called by load,
187 but it deals with code references, and requires the method name to be
190 Methods overridden here are _NOT_ overrideable in _addmethods. This
191 may need to be changed.
196 my ($self, $method_name, $function_reference) = @_;
198 $self->{_methodhash}->{$method_name}->{reference} = $function_reference;
199 $self->{_methodhash}->{$method_name}->{overridden} = 1;
205 Usage : my $clone = $obj->clone
206 Function: Produces a clone of the Class::Modular object
210 Produces a clone of the object with duplicates of all data and/or new
211 connections as appropriate.
213 Calls _clone on all loaded subclasses.
215 Warns if debugging is on for classes which don't have a _clone
216 method. Dies on other errors.
224 bless $clone, ref($self);
226 # copy data structures at this level
227 $clone->{_methodhash} = deep_copy($self->{_methodhash});
228 $clone->{_subclasses} = deep_copy($self->{_subclasses});
230 foreach my $subclass (keys %{$self->{_subclasses}}) {
231 # use eval to try and call the subclasses _clone method.
232 # Ignore no such function errors, but trap other types of
235 # XXX Switch to can instead.
238 &$subclass::_clone($self,$clone);
241 # Die unless we've hit an undefined subroutine.
242 die $@ unless $@ =~ /Undefined\s*subroutine\s*\&.*\:\:\_clone/;
243 warn "$subclass doesn't have a _clone method\n$@" if $DEBUG;
255 Class::Modular->can('METHOD');
259 Replaces UNIVERSAL's can method so that handled methods are reported
260 correctly. Calls UNIVERSAL::can in the places where we don't know
265 A coderef to the method if the method is supported, undef otherwise.
274 my ($self,$method,$vars) = @_;
276 if (ref $self and exists $self->{_methodhash}->{$method}) {
277 # If the method is defined, return a reference to the
279 return $self->{_methodhash}->{$method}->{reference};
282 # Otherwise, let UNIVERSAL::can deal with the method
284 return UNIVERSAL::can($self,$method);
292 $obj->handledby('methodname');
293 $obj->handledby('Class::Method::methodname');
297 Returns the subclass that handles this method.
310 my ($self,$method_name) = @_;
312 $method_name =~ s/.*\://;
314 if (exists $self->{_methodhash}->{$method_name}) {
315 return $self->{_methodhash}->{$method_name}->{subclass};
329 Calls all subclass _destroy methods.
331 Subclasses need only implement a _destroy method if they have
332 references that need to be uncircularized, or things that should be
333 disconnected or closed.
339 foreach my $subclass (keys %{$self->{_subclasses}}) {
340 # use eval to try and call the subclasses _destroy method.
341 # Ignore no such function errors, but trap other types of
345 &$subclass::_destroy($self);
348 # Die unless we've hit an undefined subroutine.
349 die $@ unless $@ =~ /Undefined\s*subroutine\s*\&.*\:\:\_clone/;
350 warn "$subclass doesn't have a _clone method\n$@" if $DEBUG;
359 Usage : Called by perl
360 Function: Calls child methods which have been installed into this handle
364 The AUTOLOAD function is responsible for calling child methods which
365 have been installed into the current Class::Modular handle.
367 Subclasses that have a new function as well as an AUTOLOAD function
368 must call Class::Modular::AUTOLOAD and set $Class::Modular::AUTOLOAD
370 $Class::Modular::AUTOLOAD = $AUTOLOAD;
371 goto &Class::Modular::AUTOLOAD;
376 my $method = $AUTOLOAD;
382 if (not ref($self)) {
383 carp "Not a reference in AUTOLOAD.";
387 if (defined $self->{_methodhash}->{$method}->{reference}) {
390 goto &$self->{_methodhash}->{$method}->{reference};