I am creating an object that make queries to database. But how shall I
explain to him what database handler he shall use?
Under mod_perl it is too dangerous to make $dbh global variable.
Well, I can create data field in this object that will store current $dbh,
but it seems too strange too me to have such data field in object:
$person={
name=>'Jack',
age=10,
...,
$dbh=>...
}
$dbh have no relation to information about Jack.
Is there any better solution?
There is some piece of code
-------------------
mainscript.pl
....
my $dsn = "DBI:$config{'driver'}:database=$config{'dbname'};";
my $dbh = DBI->connect($dsn, $config{'user'}, $config{'pass'});
....
some SQL queries
......
$guest=new Person($dbh,$id);
print <<END
Guest name:$guest->name;
Guest age:$guest->age;
END
;
......
------------------
Person.pm
Package Person;
use strict;
sub new
{
my ($that, $dbh, $id) = @_;
my $class =ref($that)||$that;
my $table ='SITE';
my $sth = $dbh->prepare( qq{SELECT * FROM $table where id=?});
if (!$sth) { die "Error:" . $dbh->errstr . "\n"; }
if (!$sth->execute($id)) { die "Error:" . $sth->errstr . "\n";}
if (my $ref = $sth->fetchrow_hashref)
{
my $person->{'name'}=$ref->{'name'};
my $person->{'age'}=$ref->{'age'};
.......
}
bless $person, $that;
return $person;
}
.... others methods
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]