为什么回滚方法对于 DBI 句柄不可用?

发布于 2024-07-07 08:53:37 字数 720 浏览 8 评论 0原文

由于某种原因,我在使用 DBI 句柄时遇到了麻烦。 基本上发生的事情是我在 perl 模块中创建了一个特殊的连接函数,并从 do: 切换

do 'foo.pl'

use Foo;

然后我 do

$dbh = Foo->connect;

现在由于某种原因我不断收到错误:

无法通过包“Foo”在 ../Foo.pm 第 171 行找到对象方法“rollback”。

所以奇怪的是 $dbh 绝对不是 Foo,它只是在 foo 中定义。 无论如何,到目前为止我还没有遇到任何麻烦。 有什么想法吗?

编辑:@Axeman:connect在原始版本中不存在。 在我们有一个像这样使用的字符串之前:

do 'foo.pl';
$dbh = DBI->connect($DBConnectString);

所以 connect 是这样的

sub connect {
    my $dbh = DBI->connect('blah');
    return $dbh;
}

For some reason I am having troubles with a DBI handle. Basically what happened was that I made a special connect function in a perl module and switched from doing:

do 'foo.pl'

to

use Foo;

and then I do

$dbh = Foo->connect;

And now for some reason I keep getting the error:

Can't locate object method "rollback" via package "Foo" at ../Foo.pm line 171.

So the weird thing is that $dbh is definitely not a Foo, it's just defined in foo. Anyway, I haven't had any troubles with it up until now. Any ideas what's up?

Edit: @Axeman: connect did not exist in the original. Before we just had a string that we used like this:

do 'foo.pl';
$dbh = DBI->connect($DBConnectString);

and so connect is something like this

sub connect {
    my $dbh = DBI->connect('blah');
    return $dbh;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小鸟爱天空丶 2024-07-14 08:53:37

我们需要查看 Foo 中的实际代码才能回答这个问题。 您可能想阅读文档中的对 DBI 进行子类化,了解如何操作正确地做到这一点。

基本上,您要么需要 Foo 正确地子类化 DBI(同样,您需要阅读文档),要么需要声明一个 connect 函数以正确委托给 DBI::connect 方法。 不过,为 OO 代码编写生产性包装器时要小心。 以这种方式维持状态变得非常困难。

We need to see the actual code in Foo to be able to answer this. You probably want to read Subclassing the DBI from the documentation to see how to do this properly.

Basically, you either need Foo to subclass DBI properly (again, you'll need to read the docs), or you need to declare a connect function to properly delegate to the DBI::connect method. Be careful about writing a producedural wrapper for OO code, though. It gets awfully hard to maintain state that way.

眼睛会笑 2024-07-14 08:53:37

来自 perlfunc

 do 'stat.pl'; 

      就像 

          eval `cat stat.pl`; 
  

因此,当您执行“foo.pl”时,您会在当前上下文中执行代码。 因为我不知道 foo.plFoo.pm 中发生了什么,所以我无法告诉您发生了什么变化。 但是,我可以告诉您,它始终在当前上下文中执行,现在在 Foo:: 命名空间中执行。

您调用此方法的方式是将 'Foo' 作为第一个参数传递给 Foo::connect 或从 Foo->can 返回的 sub ('连接')。 似乎不知何故,它被传递给一些认为它是数据库句柄的代码,并告诉该对象回滚。

From perlfunc:

        do 'stat.pl';

    is just like

        eval `cat stat.pl`;

So when you do 'foo.pl', you execute the code in the current context. Because I don't know what goes on in foo.pl or Foo.pm, I can't tell you what's changed. But, I can tell you that it's always executed in the current context, and now in executes in Foo:: namespace.

The way you're calling this, you are passing 'Foo' as the first parameter to Foo::connect or the returned sub from Foo->can('connect'). It seems that somehow that's being passed to some code that thinks it's a database handle, and that's telling that object to rollback.

银河中√捞星星 2024-07-14 08:53:37

我同意斧头人的观点。 您可能应该使用

use Foo;
...
$dbh = Foo::connect();

而不是 Foo->connect();来调用您的函数。

I agree with Axeman. You should probably be calling your function using

use Foo;
...
$dbh = Foo::connect();

instead of Foo->connect();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文