1 # This file is part of Class::Modular and is released under the terms
2 # of the GPL version 2, or any later version at your option. See the
3 # file README and COPYING for more information.
4 # Copyright 2003, 2004 by Don Armstrong <don@donarmstrong.com>.
7 package Class::Modular;
11 Class::Modular -- Modular class generation superclass
17 use base qw(Class::Modular);
21 Class::Modular is a superclass for generating modular classes, where
22 methods can be added into the class from the perspective of the
23 object, rather than the perspective of the class.
25 That is, you can create a class which has a set of generic common
26 functions. Less generic functions can be included or overridden
27 without modifying the base classes. This allows for code to be more
28 modular, and reduces code duplication.
30 It fills the middle ground between traditional class based OOP and
31 classless OOP. L<Class::Mutator> and L<Sex> are similar to
32 Class::Modular but less manic.
39 use vars qw($VERSION $DEBUG $REVISION);
43 use Storable qw(dclone); # Used for deep copying objects
47 ($REVISION) = q$LastChangedRevision$ =~ /\$LastChangedRevision:\s+([^\s+])/;
48 $DEBUG = 0 unless defined $DEBUG;
57 $db->load('FOO::Subclass');
61 Loads the named subclass into this object if the named subclass has
64 The options scalar is passed to $subclass::_methods when determining
65 which methods should be added using _addmethods.
67 The subclasses _init method is called right after methods are loaded.
69 If debugging is enabled, will warn about loading already loaded
76 my ($self,$subclass,$options) = @_;
80 # check to see if the subclass has already been loaded.
82 if (not defined $self->{_subclasses}->{$subclass}){
85 eval "require $subclass" or die $@;
86 # Use subclass::METHODS if it exists [use constants METHODS => qw(foo)]
87 $self->_addmethods($subclass,&{"${subclass}::_methods"}($self,$options));
88 &{"${subclass}::_init"}($self);
91 $self->{_subclasses}->{$subclass} = {};
94 carp "Not reloading subclass $subclass" if $DEBUG;
102 $obj->override('methodname', $code_ref)
106 Allows you to override utility functions that are called internally to
107 provide a different default function.
109 It's superficially similar to _addmethods, which is called by load,
110 but it deals with code references, and requires the method name to be
113 Methods overridden here are _NOT_ overrideable in _addmethods. This
114 may need to be changed.
119 my ($self, $method_name, $function_reference) = @_;
121 $self->{_methodhash}->{$method_name}->{reference} = $function_reference;
122 $self->{_methodhash}->{$method_name}->{overridden} = 1;
129 my $clone = $obj->clone
133 Produces a clone of the object with duplicates of all data and/or new
134 connections as appropriate.
136 Calls _clone on all loaded subclasses.
138 Warns if debugging is on for classes which don't have a _clone method.
139 Dies on other errors.
147 bless $clone, ref($self);
149 # copy data structures at this level
150 $clone->{_methodhash} = dclone($self->{_methodhash});
151 $clone->{_subclasses} = dclone($self->{_subclasses});
153 foreach my $subclass (keys %{$self->{_subclasses}}) {
154 # use eval to try and call the subclasses _clone method.
155 # Ignore no such function errors, but trap other types of
158 # XXX Switch to can instead.
161 &$subclass::_clone($self,$clone);
164 # Die unless we've hit an undefined subroutine.
165 die $@ unless $@ =~ /Undefined\s*subroutine\s*\&.*\:\:\_clone/;
166 warn "$subclass doesn't have a _clone method\n$@" if $DEBUG;
178 Class::Modular->can('METHOD');
182 Replaces UNIVERSAL's can method so that handled methods are reported
183 correctly. Calls UNIVERSAL::can in the places where we don't know
188 A coderef to the method if the method is supported, undef otherwise.
197 my ($self,$method,$vars) = @_;
199 if (ref $self and exists $self->{_methodhash}->{$method}) {
200 # If the method is defined, return a reference to the
202 return $self->{_methodhash}->{$method}->{reference};
205 # Otherwise, let UNIVERSAL::can deal with the method
207 return UNIVERSAL::can($self,$method);
215 $obj->handledby('methodname');
216 $obj->handledby('Class::Method::methodname');
220 Returns the subclass that handles this method.
233 my ($self,$method_name) = @_;
235 $method_name =~ s/.*\://;
237 if (exists $self->{_methodhash}->{$method_name}) {
238 return $self->{_methodhash}->{$method_name}->{subclass};
243 =head1 INTERNAL FUNCTIONS
245 The functions below are functions internal to Class::Modular. The
246 first two, new and _init should probably be overriden in any class
247 that inherits from Class::Modular, but they are provided just in case
248 you don't want to write a new and/or _init.
256 $obj = Foo::Bar->new();
260 Creates a new C<Foo::Bar> object.
262 Aditional arguments can be passed to this creator, and they are stored
263 in C<$self->{_creation_args}>. You can also override the new method in
264 your subclass. It's just provided here for completeness.
269 my ($class,@args) = @_;
271 # We shouldn't be called $me->new, but just in case
272 $class = ref($class) || $class;
291 Stores the arguments used at new so modules that are loaded later can
294 This function is called by default from new. Classes may only wish to
300 my ($self,@creation_args) = @_;
302 $self->{creation_args} = [@_];
314 Calls all subclass _destroy methods.
316 Subclasses need only implement a _destroy method if they have
317 references that need to be uncircularized, or things that should be
318 disconnected or closed.
326 $self->_addmethods($subclass,&{"${subclass}::_methods"}($self,$options));
330 Given an array of methods, adds the methods into the _methodhash
333 Methods that have previously been overridden by override are _NOT_
334 overridden again. This may need to be adjusted in load.
338 sub _addmethods($@) {
339 my ($self,$subclass,@methods) = @_;
341 # stick the method into the table
342 # DLA: Make with the munchies!
344 foreach my $method (@methods) {
345 if (not $method =~ /^$subclass/) {
346 $method = $subclass.'::'.$method;
348 my ($method_name) = $method =~ /\:*([^\:]+)\s*$/;
349 if (exists $self->{_methodhash}->{$method_name}) {
350 if ($self->{_methodhash}->{$method_name}->{overridden}) {
351 carp "Not overriding already overriden method $method_name\n" if $DEBUG;
354 carp "Overriding $method_name $self->{_methodhash}->{$method_name}->{reference} with $method\n";
356 $self->{_methodhash}->{$method_name}->{reference} = $method;
357 $self->{_methodhash}->{$method_name}->{subclass} = $subclass;
366 foreach my $subclass (keys %{$self->{_subclasses}}) {
367 # use eval to try and call the subclasses _destroy method.
368 # Ignore no such function errors, but trap other types of
372 &$subclass::_destroy($self);
375 # Die unless we've hit an undefined subroutine.
376 die $@ unless $@ =~ /Undefined\s*subroutine\s*\&.*\:\:\_clone/;
377 warn "$subclass doesn't have a _clone method\n$@" if $DEBUG;
387 The AUTOLOAD function is responsible for calling child methods which
388 have been installed into the current Class::Modular handle.
390 Subclasses that have a new function as well as an AUTOLOAD function
391 must call Class::Modular::AUTOLOAD and set $Class::Modular::AUTOLOAD
393 $Class::Modular::AUTOLOAD = $AUTOLOAD;
394 goto &Class::Modular::AUTOLOAD;
399 my $method = $AUTOLOAD;
405 if (not ref($self)) {
406 carp "Not a reference in AUTOLOAD.";
410 if (exists $self->{_methodhash}->{$method} and
411 defined $self->{_methodhash}->{$method}->{reference}) {
414 goto &{$self->{_methodhash}{$method}{reference}};
418 croak "Undefined function $AUTOLOAD";