English 中文(简体)
如果没有一揽子方案,便可采用“脱钩”方法。 Perl
原标题:Can t call method "disconnect" without a package..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");

但它没有工作。

错误信息: 不详的路段: Query calls at /var/www/domains/gabordx.tauri.hu/www/main.pl line 7. ->“&Query(从用户中选取”);”

问题是什么?

感谢!

最佳回答

页: 1 学历:

our @EXPORT_OK = qw(Query DBImport DataFill);
our @EXPORT = qw(Query DBImport DataFill);

页: 1 一揽子计划,如此,

sql::Query("SELECT * FROM users");
问题回答

您可以直接称呼该模块的名称,预定名称如下:

sql::Query("SELECT * FROM users");

或可从模块中进口

our @EXPORT_OK = qw(Query DBImport DataFill); # In the module

use sql qw(Query); # In caller code

或你可以不停地从 s头出口。

our @EXPORT = qw(Query DBImport DataFill);

http://perldoc.perl.org/Exporta。

一种方式是出口这一功能:

our @EXPORT = qw(DBImport DataFill Query);

http://perldoc.perl.org/Exporter.html>rel=“nofollow”>perldoc Exporter





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签