我收到这个错误
不能在没有包或对象引用的情况下调用方法“prepare”... [在以我的 $execute 开头的 createSqlTable 行中]。
代码类似于以下(片段):
use othermodule1;
use othermodule2;
my $dbh = othermodule1::connectToDatabase();
if ( defined $databaseHandler )
{
print "\ndatabaseHandler is defined here after calling connectToDatabase\n";
}
othermodule2::setDatabaseHandler( $databaseHandler );
othermodule2::createSqlTable();
其他模块1:
my $databaseHandler = unset;
sub connectToDatabase
{
$databaseHandler = DBI->connect('DBI:mysql:db','db','pwd') or die "Could not connect to database: ";
}
其他模块2:
my $dbhandler = unset;
sub setDatabaseHandler
{
$dbhandler = @_;
}
sub createSqlTable()
{
my $query = "CREATE TABLE atable ( a CHAR(30) NULL, b CHAR(30) NULL )"; # etc...
my $execute = $dbhandler ->prepare($myquery) or die "Couldn't prepare statement: " . $dbhandler->errstr;
$execute->execute or die "Couldn't execute statement: " . $dbhandler->errstr;
}