Reputation: 11
I am writing Perl code and using the DBI
module in the file with extension .pm
.
When importing the DBI
module, I am getting an error like
syntax error at /etc/perl/Foo.pm line 13, near "DBI:"
Compilation failed in require at join.pl
In the join.pl
file we are calling the module Foo.pm
as
use Foo;
Foo::dbconnect();
and the code in Foo.pm
is like this
#!/usr/bin/perl
package Foo;
use DBI;
sub dbconnect {
my $database = "DB_NAME";
my $user ="user_name";
my $password = "password";
my $dsn = "dbi:mysql:$database:localhost:3306";
my $connect = DBI:connect->($dsn, $user, $password)
or die "can't connect to the db $DBI::errstr\n";
return $connect;
}
1;
I am getting the error in the line
my $connect = DBI:connect->($dsn, $user, $password)
or die "can't connect to the db $DBI::errstr\n";
Please help me to solve this issue.
Upvotes: 0
Views: 965
Reputation: 126772
The syntax you are using for the connect
method DBI:connect->(...)
is wrong.
It would be valid syntax if you used two colons instead of one and removed the arrow but it still wouldn't work.
What you need is a class method call, like this
my $connect = DBI->connect($dsn, $user, $password)
or die "can't connect to the db: $DBI::errstr\n";
Upvotes: 3
Reputation: 54381
Welcome to SO.
First of all, you should always use strict
and use warnings
to help find problems in your code.
There's a syntax error in your connect
line. Try this:
my $connect = DBI->connect($dsn, $user, $password)
or die "can't connect to the db $DBI::errstr\n";
A word of advice: It's a good idea to name your variables after what they represent. I'd suggest $dbh
for database handle instead of $connect
, but it's up to preference of course.
Upvotes: 3