Re: How to include a connection file in perl



sulata@xxxxxxxxx wrote:
Hello

I am shifting from php to perl and I need some help. I want to make a
database insert page and divide that page into two parts. One is the
connection file and other is the execution file. When all the following
code is in one file, it works well, but when I break it into two files,
this code does not run. Can anyone please help? Maybe I am not using
'require' properly. My code is;

# load module
use DBI;
# connect
my $dbh = DBI->connect("DBI:mysql:database=perl;host=localhost",
"root", "", {'RaiseError' => 1});
$dept_name = time();
# execute INSERT query
my $rows = $dbh->do("INSERT INTO departments (dept_name) VALUES
('".$dept_name."')") or die "Error";
print "Content-type: text/html\n\n";
print "Done: $rows row(s) affected\n";
# clean up
$dbh->disconnect();

foo.pm:
package foo;
sub connect {
DBI->connect("DBI:mysql:database=perl;host=localhost",
"root", "", {'RaiseError' => 1});
}
1;
__END__


file.pl:
use foo;
my $dbh=foo::connect();
# etc.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
.