为什么回滚方法对于 DBI 句柄不可用?
由于某种原因,我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们需要查看 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.
来自 perlfunc:
因此,当您
执行“foo.pl”
时,您会在当前上下文中执行代码。 因为我不知道foo.pl
或Foo.pm
中发生了什么,所以我无法告诉您发生了什么变化。 但是,我可以告诉您,它始终在当前上下文中执行,现在在Foo::
命名空间中执行。您调用此方法的方式是将
'Foo'
作为第一个参数传递给Foo::connect
或从Foo->can 返回的 sub ('连接')
。 似乎不知何故,它被传递给一些认为它是数据库句柄的代码,并告诉该对象回滚。From perlfunc:
So when you
do 'foo.pl'
, you execute the code in the current context. Because I don't know what goes on infoo.pl
orFoo.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 inFoo::
namespace.The way you're calling this, you are passing
'Foo'
as the first parameter toFoo::connect
or the returned sub fromFoo->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 torollback
.我同意斧头人的观点。 您可能应该使用
而不是 Foo->connect();来调用您的函数。
I agree with Axeman. You should probably be calling your function using
instead of Foo->connect();