SPUG: Class::Multimethod Question

Asim Jalis ajalis at cobaltgroup.com
Thu Jul 6 13:54:50 CDT 2000


I am trying to use Class::multimethods to write object constructors
and running into some problems. Here is a code sample:

-----Code Begins-----
#!/usr/bin/perl
 
package ObjectA;
use Class::Multimethods; 
multimethod new => ('$') => sub { return bless ({}, 'ObjectA'); };

package ObjectB;
use Class::Multimethods; 
multimethod new => ('$') => sub { return bless ({}, 'ObjectB'); };

package main;

my $x = new ObjectA;
print "ref of x = [", ref($x), "]\n";

my $x = ObjectA->new;
print "ref of x = [", ref($x), "]\n";

my $x = new ObjectB;
print "ref of x = [", ref($x), "]\n";

my $x = ObjectB->new;
print "ref of x = [", ref($x), "]\n";

-----Code Ends-----

Here is the output I get:

ref of x = [ObjectB]
ref of x = [ObjectB]
ref of x = [ObjectB]
ref of x = [ObjectB]

This is broken. The output should be ObjectA on the first two lines
instead of ObjectB.

It's clear that ObjectA->new is actually calling ObjectB::new("ObjectA")
instead of ObjectA::new("ObjectA").  The problem goes away if I replace
multimethods and use plain old Perl subs instead.

Ideas?

Asim

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     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