At 03:05 PM 4/19/01 -0400, Paul D. Smith wrote:
>Is this too advanced a question for [EMAIL PROTECTED]?

Pretty much :-)  I'd try [EMAIL PROTECTED]

More below.

>I'm trying to create a subclass of the DBI class.  My goal is twofold:
>
>  1) Override the connect method so that I can hide many of the details
>     of connecting from the rest of my package, and
>
>  2) Add a few extra "helper" methods.
>
>I want to access my extra helper methods, plus all the standard DBI
>methods.
>
>My subclass has no data of its own, so I just wanted to use the normal
>DBI handle as the object reference.
>
>I did something like this:
>
>   package My::DB;
>
>   use strict;
>   use vars qw(@ISA);
>
>   use DBI;
>
>   @ISA = qw(DBI);
>
>   sub connect {
>     my $self = shift;
>     my $class = ref($self) || $self;
>
>     #... figure connect info $data_source, $username, $password
>
>     return $class->SUPER::connect($data_source, $username, $password);
>   }
>
>
>   sub MyMethod {
>     my $this = shift;
>
>     # ... do some stuff
>   }
>
>Now when I do this:
>
>   $dbh = My::DB->connect();
>
>it works, but this:
>
>   $dbh->MyMethod();
>
>fails with "Can't locate object method "Send" via package My::DB at ...".
>
>Is this telling me that DBI was implemented using only the 1-arg form of
>bless?  Can I not inherit from DBI?

It's using a two-arg form, but it's buried amidst a maze of calls between 
DBI and a driver.  So I think you're best off finding out what the 
'approved' way of subclassing DBI is.  I can't even find anything about it 
in the DBI book.

>So, then I thought I'd try re-blessing it into my class, like this:
>
>   sub connect {
>     my $self = shift;
>     my $class = ref($self) || $self;
>
>     #... figure connect info $data_source, $username, $password
>
>     my $this = $class->SUPER::connect($data_source, $username, $password);
>
>     return bless($this, $class);
>   }
>
>Now I can get to $dbh->MyMethod(), but I can't get to any of the DBI
>methods; for example $dbh->prepare() fails with:
>
>   Can't locate auto/My/DB/prepare.al in @INC ...
>
>I can't even get there using SUPER; this also fails:
>
>   sub MyMethod {
>     my $this = shift;
>
>     # ... do some stuff
>
>     return $this->SUPER::prepare(...);
>   }
>
>Gives me:
>
>   Can't locate auto/My/DB/SUPER/prepare.al in @INC ...
>
>which seems strange to me.
>
>Anyway, can anyone help me make a subclass of DBI that does what I want?
>Do I have to write some kind of AUTOLOAD to do a delegator (or use
>Class::Delegate or something), as described in Camel?  I was hoping that
>the @ISA thing would allow any method that wasn't known in my class to
>be automatically looked for in my parent classes... why doesn't that
>work?
>
>Thanks!
>
>--
>-------------------------------------------------------------------------------
>  Paul D. Smith <[EMAIL PROTECTED]>    HASMAT--HA Software Methods & 
> Tools
>  "Please remain calm...I may be mad, but I am a professional." --Mad 
> Scientist
>-------------------------------------------------------------------------------
>    These are my opinions---Nortel Networks takes no responsibility for them.

--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com

Reply via email to