我也有办法得到 dbConnect not defined 错误。我似乎找不到问题所在。dbConnect 是带有密码的硬编码。
#! perl
use strict;
use warnings;
use diagnostics;
use DBR qw(dbConnect query);
my $dbhead = dbConnect();
my $sql = "select * from mailing";
my @returned = query($dbhead,$sql);
该模块保存在名为 DB.pm 的保存目录中。我无法更改名称或内容。
package GUI::DB;
use strict;
use DBI;
use warnings;
use vars qw(@ISA @EXPORT);
use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(dbConnect query);
#
# dbConnect - connect to the database, get the database handle
#
sub dbConnect {
# Read database settings from config file:
my $dsn = "DBI:mysql:database=test";
my $dbh = DBI->connect( $dsn,
'abc',
'password',
{ RaiseError => 1 }
);
return $dbh;
}
#
# query - execute a query with parameters
# query($dbh, $sql, @bindValues)
#
sub query {
my $dbh = shift;
my $sql = shift;
my @bindValues = @_; # 0 or several parameters
my @returnData = ();
# issue query
my $sth = $dbh->prepare($sql);
if ( @bindValues ) {
$sth->execute(@bindValues);
} else {
$sth->execute();
}
if ( $sql =~ m/^select/i ) {
while ( my $row = $sth->fetchrow_hashref ) {
push @returnData, $row;
}
}
# finish the sql statement
$sth->finish();
return @returnData;
}
__END__
我希望有人能看到我做错了什么。我知道模块中的子程序可以正常工作,因为如果我只是在模块底部编写代码并运行它,它就可以工作。