SPUG: Class::Multimethod Question

Andy Jacobs andyj at microsoft.com
Thu Jul 6 14:15:28 CDT 2000


You seem to be declaring both "new" functions with the same parameters
(i.e., '$').  From memory of the talk last night, I think the syntax should
be more like:
	multimethod new => (ObjectA) => sub [...]

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.

 - Andy Jacobs

> -----Original Message-----
> From: owner-spug-list at pm.org 
> [mailto:owner-spug-list at pm.org]On Behalf Of
> Asim Jalis
> Sent: Thursday, July 06, 2000 11:55 AM
> To: damian at conway.org
> Cc: spug-list at pm.org
> Subject: SPUG: Class::Multimethod Question
> 
> 
> 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
> 
> 
> 

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