How to include a connection file in perl

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();
sulata [ Sa, 02 Dezember 2006 21:11 ] [ ID #1555392 ]

Re: How to include a connection file in perl

sulata [at] gmail.com 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
xhoster [ Mo, 04 Dezember 2006 02:38 ] [ ID #1557117 ]
Perl » comp.lang.perl.modules » How to include a connection file in perl

Vorheriges Thema: FDF2XML skript?
Nächstes Thema: Email address syntax check?