Subclassing Curiosity

Schuyler D. Erle schuyler at tridity.org
Tue May 7 17:40:50 CDT 2002


I hate to say this, but, speaking from experience, subclassing DBI is *very* difficult to get right. I managed to get the inheritance straightened out, but then for reasons not clear to me, the driver I hacked up dumped core when I tried writing to the database. I'd have debugged it but I don't have the time ATM.

The short answer is, and I'm sure you're not going to like this, avoid subclassing DBI if there's another practical solution to your problem. The DBI is really complicated, and IMHO it's not worth the headache. If you're trying to solve a problem that you really feel can't be solved without subclassing DBI, I recommend describing it in greater detail to the list to see what we come up with...

SDE


On Tue, 07 May 2002 15:13:48 -0700, "Kevin Bingham" <kevin at oreilly.com> wrote:

> Hey Sebastian!
> 
> Sorry, I don't know the answer to this particular one, though somebody 
> kicking around might. Though considering our meager mailing list, who knows 
> when you'll get a response. I suggest posting your question to Perl Monks, 
> http://www.perlmonks.org/, which is highly touted by our very own Tom 
> Anderson (http://tomacorp.com/, We're not a corporation). On Perl Monks you 
> should get an answer right quick.
> 
> Kudos!
> -Kevin
> 
> At 04:48 PM 5/6/2002 -0700, Seb wrote:
> >I have a curiosity with regard to subclassing DBI. I'm just not sure I
> >get the principles involved. I've got a module that is a DBI subclass.
> >In order to subclass DBI as "package Mine", one must also provide
> >packages Mine::db and Mine::st, because the database and statement
> >handles used in DBI are their own classes.
> >
> >My confusion comes in when I put a constructor in my main package, and
> >try to use DBI methods on its returned object. (DBI has no constructor
> >method with which to reference a DBI object, per se)
> >
> >So I have something like this:
> >
> >#!/usr/bin/perl
> >package MyDBI;
> >use DBI;
> >@ISA = qw(DBI);
> >
> >sub new {
> >   my $class = shift;
> >   my $self = {};
> >   bless $self, $class;
> >   return $self;
> >}
> >
> >package MyDBI::db;
> >@ISA = qw(DBI::db);
> >
> >package MyDBI::st;
> >@ISA = qw(DBI::st);
> >
> ># now our main program
> >
> >package main;
> >
> >my $obj = new MyDBI;
> >
> ># $obj is a MyDBI object, with DBI base methods
> >print '$obj is a: '.(scalar ref $obj)."\n";
> >
> >my $dbh = $obj->connect('DBI:Pg:dbname=somedb', undef, undef);
> >
> ># This generates a warning
> ># $dbh is a DBI::db object, NOT a MyDBI::db object
> >print '$dbh is a: '.(scalar ref $dbh)."\n";
> >
> ># On the other hand, if I use the module name, not my object...
> >
> >my $dbh2 = MyDBI->connect('DBI:Pg:dbname=somedb', undef, undef);
> >
> ># No error, and $dbh2 is a proper MyDBI::db object
> >print '$dbh2 is a: '.(scalar ref $dbh2)."\n";
> >
> >__END__
> >
> >The output of the above code looks like this:
> >$obj is a: MyDBI
> >DBI subclass 'MyDBI=HASH(0x813c1a0)::db' isn't setup, ignored at MyDBI.pl 
> >line 26
> >$dbh is a: DBI::db
> >$dbh2 is a: MyDBI::db
> ><disconnect errors snipped>
> >
> >Why does the first case ($obj->connect) not do what I expect it to,
> >while the second case (MyDBI->connect) works fine?
> >
> >Is this just some basic scoping or inheritance thing that I'm not
> >understanding?
> >
> >TIA
> >Sebastian



More information about the Santa-rosa-pm mailing list