SPUG: Class::Multimethod Question

Matt Tucker tuck at whistlingfish.net
Fri Jul 7 13:40:10 CDT 2000


-- Asim Jalis <ajalis at cobaltgroup.com> spake thusly:

>> But in this case, as your examples show, there is enough information
>> when calling your constructor to distinguish between ObjectA and
>> ObjectB without using multimethods.  The multimethod technique would
>> make more sense if you were passing additional parameters to the
>> constructor, and wanted to choose ObjectA or ObjectB based on the
>> types of those parameters.
>
> The multimethods technique can potentially be quite useful with
> constructors.  It is common to provide one constructor which takes
> no parameters and then provide other constructors which over-ride the
> defaults.

A current possible workaround is:

package Foo;
sub new {
	my $class = shift;
	my $self = bless {}, $class;
	$self->init(@_);
	return $self;
}

multimethod init => (Foo) => sub {
	...
}

multimethod init => (Foo HASH) => sub {
	...
}
...etc.

However, it would be nice to have Multimethods actually do the right 
thing for new without requiring this workaround.

- Matt


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
     Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For full traffic, use spug-list for LIST ; otherwise use spug-list-digest





More information about the spug-list mailing list