I seem to have coded myself into a design corner and I'm trying to figure the best way out. I have a base class that I'll call Foo::PersistentObject. This class is not to be instantiated, but instead, objects will inherit some common methods from it:

The intent is to make all objects that use this class persistent in the database without the application programmer needing to worry about it. The applications in the poop group didn't seem applicable for various reasons (need gradual refactoring, database limitations, etc.).

The problem is that the get_list method returns a list of objects, but the constructor has been overloaded and it's causing some problems as a result.

Here's how a subclass might use it:

package Foo::People; use Foo::PersistentObject; @ISA = 'Foo::PersistentObject'; use strict; my %ARGS = ( _id => 'people_id', _table => 'people', _mapping => { id => 'people_id', first_name => 'first', last_name => 'last' } ); sub new { my ($class,$id) = @_; $ARGS{_open_this_id} = $id if $id; my $self = $class->SUPER::new( %ARGS ); } sub get_list { my $class = shift; $class->SUPER::get_list( %ARGS ); } 1;

Note: The intent of the %mapping hash is to provide external method names that map to internal field names so that I can change field names in the database without the programmer needing to worry about the interface.

The programmer could use this class as follows:

my $person = Foo::People->new; $person->open( $id ); my $person = Foo::People->new( $id ); # same thing as above my $people = Foo::People->get_list; # get a list of all people (as obj +ects)

The problem lies in the fact that I chose to overload the constructor. See that line with the _open_this_id line? Well, if I call get_list, the super class method looks like this:

sub get_list { my ($class, %args) = @_; my ($id_name,$table) = @args{qw(_id _table)}; my $sql = "SELECT $id_name FROM $table"; # the database object is deliberately unavailable to the # programmers my @objects; my $ids = $DBO->_arrayref( $sql ); foreach my $id ( @$ids ) { push @objects => $class->new(%args,open_id => $id); } return \@objects; }

You can see that I am creating a list of objects and as I am now passing an argument list to new, it thinks that there is an id to open, but this id winds up being one of the keys of the %args hash. This causes my id to be incorrect and the constructors fail. I got around this by doing the following:

sub new { my ($class,$id) = @_; $ARGS{open_id} = $id if $id; $class->_new( %ARGS ); } sub _new { my $class = shift; $class->SUPER::new( @_ ); }

Then, in the Foo::PersistentObject::get_list method, I call the _new class method instead of new. It seems to solve the problem, yet it also seems a bit hackish at the same time. Having to simulate overloading based on method signatures is making this code considerably more ugly.

Can anyone offer advice on a better strategy here? Also, I'm curious to know if the whole idea might be unsound. I think that it's reasonable, but I know there are those who assert that inheritance should be avoided in Perl.

Cheers,
Ovid

New address of my CGI Course.
Silence is Evil (feel free to copy and distribute widely - note copyright text)


In reply to Overloading inherited class methods by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.