[Pdx-pm] [build in defaults to method calls from the outside] need some help thinking thru this one

Austin Schutz tex at off.org
Wed Jul 25 21:45:56 PDT 2007


	I've probably thought about this too much but...

On Mon, Jul 23, 2007 at 11:58:58PM -0700, Michael G Schwern wrote:
> > sub method1 { my($self) = @_; $self->{bi}->method1($self->{isbn}); }
> > sub method2 { my($self) = @_; $self->{bi}->method2($self->{isbn}); }
> > sub method3 { my($self) = @_; $self->{bi}->method3($self->{isbn}); }
> > sub method4 { my($self) = @_; $self->{bi}->method4($self->{isbn}); }
> > 
> > 	*shrug*. Seems clean to me unless you have a bajillion methods. Ymmv.
> 
> The above code is a maint nightmare for all the reasons cut & code is a
> nightmare.  If you want to change how it works across the board you need to
> edit it in every spot.  The duplication means the individual methods will tend
> to drift apart and each get changed slightly differently and each get their
> own class of bugs.
> 
> Also every time BookInfo adds a new method you have to add a new method,
> tightly binding the two classes together.

	This is true if the API changes. But I like the explicitness of it
only working with specific methods. I like my bonds tight. :-)
	Come to think of it you could have AUTOLOAD only work on specific
methods too, I guess that amounts to about the same thing without the cut
and paste effect.

> 
> The AUTOLOAD wtf is obviated with a simple comment:
> # delegate to $self->{bi}->$method($self->{isbn}, @args);
> 
> Method generation is powerful and maintainable.  Embrace it.
> 

	If you have a class which exists really as a subclass it sounds
reasonable because having new methods pop in automatically is expected
behavior. It's also useful for set/get subs.

	For aggregate objects like in this example it just seems unclean.
In particular because things get weird when you have another class you want
to do the same thing with- you can have name collisions and other bits of
nastiness.

	One hybrid technique might be something like:


BEGIN {
  $Autoloads = {
    'method1' => 'Class1',
    'method2' => 'Class1',
    'method3' => 'Class2',
    ...
  };
}

	..then have AUTOLOAD call the method for the correct class, barf
otherwise.

	Austin


More information about the Pdx-pm-list mailing list