本文介绍了无法通过包子类化DBI定位对象方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次尝试使用perl进行子类化,我想知道为什么我会遇到这个简单的错误...
无法通过/home/dblibs/WebDB.pm第19行的包"WebDB :: st"找到对象方法"prepare".".似乎找到了WebDB模块就可以了,但是:::
中没有prepare子例程.首先,这是我的软件包(两个软件包都在一个文件WebDB.pm中).

this is my first foray into subclassing with perl and I am wondering why I am getting this simple error...
"Can't locate object method "prepare" via package "WebDB::st" at /home/dblibs/WebDB.pm line 19.". It seems to find the module WebDB ok, but not the prepare subroutine in ::st
First here's my package (both packages are in one file, WebDB.pm)

package WebDB;
use strict;
use DBI;

sub connect {
    my $dbh = (DBI->connect ("DBI:mysql:test:127.0.0.1", "root","",
                    { PrintError => 1, RaiseError => 0 }));
    return bless $dbh, 'WebDB::st';
}

package WebDB::st;
our @ISA = qw(::st);
sub prepare {
    my ($self, $str, @args) = @_;
    $self->SUPER::prepare("/* userid:$ENV{USER} */ $str", @args);
}


1;

我还尝试将我们的@ISA = qw(;; st)"替换为"use base'WebDB'",并遇到了同样的问题.我认为这可能是我所忽略的非常简单的事情.非常感谢!简

I also tried replacing the "our @ISA = qw(;;st)" with "use base 'WebDB'" and same problem.I'm thinking it's probably something very simple that I'm overlooking. Many thanks! Jane

推荐答案

必须正确完成子类化DBI的工作.仔细阅读为DBI子类化并正确设置RootClass(或在@ISA设置为root的情况下显式调用connect. DBI).确保您有WebDB :: st子类化DBI :: st和WebDB :: db类的子类化DBI :: db(即使没有覆盖任何方法).无需费劲.

Subclassing DBI has to be done just right to work correctly. Read Subclassing the DBI carefully and properly set RootClass (or explicitly call connect on your root class with @ISA set to DBI). Make sure you have WebDB::st subclassing DBI::st and a WebDB::db class subclassing DBI::db (even if there are no methods being overridden). No need to rebless.

避免使用base;它有一些不幸的行为导致了它的过时,特别是当与不在其自己的文件中的类一起使用时.显式设置@ISA或使用较新的parent编译指示:

Avoid using base; it has some unfortunate behavior that has led to its deprecation, particularly when used with classes that are not in a file of their own.Either explicitly set @ISA or use the newer parent pragma:

package WebDB;
use parent 'DBI';
...
package WebDB::db;
use parent -norequire => 'DBI::db';
...
package WebDB::st;
use parent -norequire => 'DBI::st';
...

这篇关于无法通过包子类化DBI定位对象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 09:41