我写了一个 perl 模块:
package sql;
use strict;
use XML::Simple;
use DBI;
use DBD::mysql;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(DBImport DataFill);
our @EXPORT = qw(DBImport DataFill);
our $dbh;
sub DBConnect() {
my $platform = "mysql";
my $database = "databasename";
my $host = "localhost";
my $user = "user";
my $pw = "password";
my $dsn = "dbi:mysql:$database:localhost";
$dbh = DBI->connect($dsn, $user, $pw);
$dbh->do('SET NAMES utf8');
return $dbh;
}
sub Query($) {
my $sth = $dbh->prepare(shift);
$sth->execute;
while (my $ref = $sth->fetchrow_hashref()) {
print $ref->{'email'};
}
$sth->finish();
}
BEGIN{
$dbh = &DBConnect();
}
END{
$dbh->disconnect();
}
1;
我尝试使用它:
#!/usr/bin/perl
use strict;
use CGI qw/:standard/;
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
use sql;
&Query("SELECT * FROM users");
但它不起作用。
错误消息:未定义的子例程 &main::Query 在 /var/www/domains/gabordx.tauri.hu/www/main.pl 第 7 行调用。 -> "&Query("SELECT * FROM users");"
有什么问题?
谢谢!