unable to store data in DBI object

J

Jon

Hiya,

I'm working on adding table prefixes for an application, so that it can be
used with the same database but different sets of data. I thought the best
idea would be to store the prefix as part of the DBI object (eg:
$dbh->{'_prefix'} = $prefix;), however this isn't working.

Here is some code demonstrating the problem.

penguin@restless:~/scripts$ cat sqltest.pl
#!/usr/bin/perl

use lib './lib';

use Data::Dumper;
use sql;
use strict;
use warnings;

my $sql = new sql(
'database' => 'test',
'username' => 'username',
'password' => 'password',
'dbprefix' => 'anything',
);

print Dumper($sql);
penguin@restless:~/scripts$ cat lib/sql.pm
package sql;

use DBI;
use strict;
use warnings;

@sql::ISA = qw/DBI::db/;

sub new($%) {
my $class = shift;
my %opts = @_;

# Make sure we have all required arguments
foreach (qw/username database dbprefix/) {
die("No $_ supplied") unless $opts{$_};
}

# Create a database handle
my $dbh = DBI->connect("DBI:mysql:$opts{database}", $opts{username},
$opts{password});
|| die("Connection to database failed: ".$DBI::errstr);


# Record our table prefix in our DBI object
$dbh->{'_prefix'} = $opts{'dbprefix'};

return bless $dbh, $class;
}

1;
penguin@restless:~/scripts$ ./sqltest.pl
$VAR1 = bless( {}, 'sql' );
penguin@restless:~/scripts$

Thanks,
 
B

Brian McCauley

Jon said:
I'm working on adding table prefixes for an application, so that it can be
used with the same database but different sets of data. I thought the best
idea would be to store the prefix as part of the DBI object (eg:
$dbh->{'_prefix'} = $prefix;), however this isn't working.

In general when you subclass a class in Perl you can't just put the
subclasses' instance data anywhere you like. You have to put it
somewhere where the parent class doesn't mind you putting it.

For details of where DBI::db doesn't mind you putting stuff see the
section of the DBI documentation section "Subclassing the DBI" and in
particular the bit where it says "You can stash private data into DBI
handles.."
package sql;

You should probably use longer package names and ones that don't start
with lowercase.
 
J

Jon

Brian McCauley said:
In general when you subclass a class in Perl you can't just put the
subclasses' instance data anywhere you like. You have to put it
somewhere where the parent class doesn't mind you putting it.

For details of where DBI::db doesn't mind you putting stuff see the
section of the DBI documentation section "Subclassing the DBI" and in
particular the bit where it says "You can stash private data into DBI
handles.."

Thank you very much for your help. I'll have more of a read into the
"Subclassing DBI" stuff when I get time.
You should probably use longer package names and ones that don't start
with lowercase.

Yes and I normally do but got lazy when writing up the experiment. Thanks
again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top