SPUG: Class::Multimethod Question

Tim Maher/CONSULTIX tim at consultix-inc.com
Thu Jul 6 15:17:07 CDT 2000


Asim,

I've got a fellow with a funny accent next to me to whom I'm
forwarding your question; expect an answer later this afternoon.

-Tim

On Thu, Jul 06, 2000 at 11:54:50AM -0700, Asim Jalis wrote:
> 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

-- 
*========================================================================*
| Dr. Tim Maher, CEO, Consultix       (206) 781-UNIX/8649;  ask for FAX# | 
| Email: tim at consultix-inc.com        Web: http://www.consultix-inc.com  |
| CLASSES:  8/14: UNIX Fund.  8/21: Shell & Utilities  9/11: Perl + Mods |
| DAMIAN CONWAY Seminars;  Adv. OO Perl: 7/6,  Parsing with Modules: 7/7 |
*========================================================================*

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