Wierd Subclassing Issue
- From: "Andrew McFarlane" <WelkinConsulting@xxxxxxxxx>
- Date: 9 Oct 2006 14:43:57 -0700
I successfully created and tested a class called TestDBI which overrode
only the connect() method.
I then decided that I hated the class name, so I tried to create the
class CompanyName::DBI. Just as I had done before creating the TestDBI
class, I used module-starter to create all of the infrastructure to
build the new class. I then copied the code inside of TestDBI.pm to
CompanyName/DBI.pm . Inside of the later file, I changed the
occurrences of the string "TestDBI" with "CompanyName::DBI" where
necessary.
I started to build the new module--I'm on Windows XP--the normal way
(perl Makefile.PL; nmake; nmake test...). When I executed "nmake
test", I got the error message:
Can't locate object method "install_driver" via package
"CompanyName/DBI" (perhapsyou forgot to load "CompanyName/DBI"?) at
C:/Perl/site/lib/DBI.pm line 592.
I rechecked--did I really only substitute TestDBI with CompanyName::DBI
in the new class? YES! Did "nmake test" really work for class
TestDBI? Yes.
So, is this because I'm allowed to create a Name1 class, but not a
Name2::Name3 class?
Again, I'm on XP. I'm using Perl 5.8.8, DBI 1.52, and DBD::Oracle
1.15.
Here is an abridged version of the class:
####
package CompanyName::DBI;
use base 'DBI';
use version; $version::VERSION = qv('0.0.1');
use warnings;
use strict;
use CLASS;
use Carp;
use Config::IniFiles;
use List::MoreUtils 'any';
our %_INFO;
_initialize();
our @_VALID_APPLICATIONS = _get_valid_applications();
our $_VALID_APPLICATION_STRING = join ', ', @_VALID_APPLICATIONS;
our @_VALID_ENVIRONMENTS = _get_valid_environments();
our $_VALID_ENVIRONMENT_STRING = join ', ', @_VALID_ENVIRONMENTS;
our $errstr = undef;
our $err = 0;
our $DEBUG = 0;
sub connect {
my ($class, $arg_href) = @_;
# Check the arguments.
my $usage = "USAGE: $CLASS->connect(
application => <$_VALID_APPLICATION_STRING>,
environment => <$_VALID_ENVIRONMENT_STRING>,
);";
unless( my $refType = ref($arg_href) eq 'HASH' ) {
croak "The second parameter must be a HASH reference--you passed in a
$refType reference: $usage";
}
unless( exists $arg_href->{application} ) {
croak qq(The hash reference must have the key "application": $usage);
}
unless( any { $arg_href->{application} eq $_ } @_VALID_APPLICATIONS )
{
croak "The application that you specified ($arg_href->{application})
is invalid: $usage";
}
if( $arg_href->{application} ne 'CLOWNFISH' ) {
unless( exists $arg_href->{environment} ) {
croak qq(The hash reference must have the key "environment":
$usage);
}
unless( any { $arg_href->{environment} eq $_ } @_VALID_ENVIRONMENTS )
{
croak "The environment that you specified ($arg_href->{environment})
is invalid: $usage";
}
}
else {
$arg_href->{environment} = 'ALL_ENVS';
}
if( $DEBUG ) {
warn "DEBUG: ", (caller(0))[3], ": Here are the parameters passed
in:\n";
foreach my $key ( keys %$arg_href ) {
warn qq(\t$key = "$arg_href->{$key}"\n);
}
}
my ( $dbh, $dsn, $user, $password ) = _db_connect_info_lookup(
$arg_href );
$user = $arg_href->{user}
if exists $arg_href->{user};
$password = $arg_href->{password}
if exists $arg_href->{password};
if( $DEBUG ) {
if( $dbh ) {
warn "DEBUG: ", (caller(0))[3], ": Re-using the existing database
handle for $arg_href->{application} $arg_href->{environment}.\n";
}
else {
warn "DEBUG: ", (caller(0))[3], ": About to connect to the
$arg_href->{application} $arg_href->{environment} database as
$user/$password ($dsn).\n";
}
}
$dbh = $class->SUPER::connect( $dsn, $user, $password, { RaiseError =>
0, PrintError => 0, AutoCommit => 0 } )
unless $dbh;
$errstr = $DBI::errstr;
$err = $DBI::err;
$_INFO{$arg_href->{application}}{$arg_href->{environment}}{HANDLE} =
$dbh;
if( $DEBUG and $errstr ) {
warn "DEBUG: ", (caller(0))[3], ": errstr: $errstr; err: $err\n";
}
return $dbh;
}
<SNIP>
package CompanyName::DBI::db;
use base 'DBI::db';
package CompanyName::DBI::st;
use base 'DBI::st';
1;
####
Thanks.
Andrew McFarlane
.
- Prev by Date: RE: Creating XML from an Oracle DB
- Next by Date: Re: DBD:Oracle 1.07 & ref cursor
- Previous by thread: DBD:Oracle 1.07 & ref cursor
- Next by thread: trigger script
- Index(es):