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

Michael G Schwern schwern at pobox.com
Mon Jul 23 23:58:58 PDT 2007

Austin Schutz wrote:
> 	I actually like the
> sub method { my($self) = @_; $self->{bi}->method($self->{isbn}); }
> 	methodology you are using already. It's clear what you are doing
> and the code is easy to follow. The next person isn't likely to go
> 'wtf is going on here' when they are looking at your code like they will if
> they see something only implicitly defined by AUTOLOAD. Also in vi it only
> takes about 3 seconds per additional method to add in new methods after a few
> yypppps:
> 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.

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.

More information about the Pdx-pm-list mailing list