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

Austin Schutz tex at off.org
Mon Jul 23 23:29:50 PDT 2007

On Mon, Jul 23, 2007 at 05:29:14PM -0700, benh wrote:
> ok heres the issue. I have two modules, one that is a book info look
> up module(BookInfo), and one that is more of a basic model for a
> single book(SingleBook). SingleBook holds an instance of BookInfo so
> that it can make many of it's lookups (BookInfo->{bi}).
> All of the
> methods for BookInfo take an isbn as the param, SingleBook takes an
> isbn as a param for new, and holds it.
> The problem that I can't cleanly subclass BookInfo, because I already
> know my isbn, so
> I'm running in to is I keep haveing stubs that just pass thru to
> BookInfo in SingleBook. About 2/3's of SingleBook is basicly
> sub do_something {
>    my ( $self ) = @_;
>    return $self->{bi}->do_something($self->{isbn});
> }
> It works, but it seems needlessly messy. What I would like to do is to
> have some way to specify that all calls made to SingleBook that dont
> exits should then get passed to BookInfo with the know isbn.

	What sticks out to me is that this makes it sound like BookInfo
should be initialized with an isbn. One way to accomplish this:

	Add set_isbn/get_isbn methods to BookInfo. It needs to be initialized
before use and reset between methods which will use differing isbns. This is
less clean but simple. You can actually leave existing API callers alone
because you can do:

package BookInfo;

# $bi->method();
# or
# $bi->method($isbn);
sub method {
  my($self, $isbn) = @_;
  $isbn = $self->{isbn} unless defined($isbn);

	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

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.


More information about the Pdx-pm-list mailing list