为什么我的 Perl 对象找不到它的 Skip() 方法,即使我可以将它作为子例程调用?

发布于 2024-08-02 03:24:19 字数 714 浏览 6 评论 0 原文

我正在开发 Perl 模块,每当我调用 skip() 方法时,我都会按以下方式编写:

$cursor->skip(4);

我得到:

Undefined subroutine &MyModule::Cursor::skip called at t/tester.pl line 24.
        (in cleanup) invalid object at t/tester.pl line 24.

当我这样调用它时:

MyModule::Cursor::skip($cursor, 4);

Perl 找到它!

奇怪的是,如果我将“skip”命名为其他任何内容(“skipper”、“hello”),则此语法有效:

$cursor->skipper(4);

我想也许skip() 是一个“秘密”保留关键字或其他东西,但我还有名为 sort()next() 的方法(其中我知道是保留的),而且这些工作得很好。

我真的很想将此方法命名为“skip”。 有谁知道为什么 Perl 找不到它?

I'm working on a Perl module and whenever I call the skip() method I wrote in the following way:

$cursor->skip(4);

I get:

Undefined subroutine &MyModule::Cursor::skip called at t/tester.pl line 24.
        (in cleanup) invalid object at t/tester.pl line 24.

When I call it like:

MyModule::Cursor::skip($cursor, 4);

Perl finds it!

Oddly, if I name "skip" anything else ("skipper", "hello"), this syntax works:

$cursor->skipper(4);

I thought maybe skip() was a "secret" reserved key word or something, but I've also got methods named sort() and next() (which I know are reserved), and those work fine.

I'd really like to name this method "skip." Does anyone know why Perl can't find it?

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

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

发布评论

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

评论(2

旧瑾黎汐 2024-08-09 03:24:19

skip() 是从 Test::More< 导出的/code>,您可能已加载该文件,因为您的可执行文件名为 t/tester.pl

ref($cursor) 会给你带来什么? 它应该是一个受祝福的 MyModule::Cursor 对象,但“无效对象”错误可能表明该对象构造不正确。

编辑: perldiag 给出了另一条线索:“in cleanup”表示对象的析构函数遇到问题。 假设您的对象中还没有析构函数,请创建一个 MyModule::Cursor::DESTROY 方法来 Data::Dump 存储该对象以查看其外观就像这个时候。

展示此行为的简洁示例代码片段将非常有帮助。

skip() is exported from Test::More, which you might have loaded since your executable is named t/tester.pl.

What does ref($cursor) yield you? It should be a blessed MyModule::Cursor object, but the "invalid object" error might be suggesting the object was not constructed properly.

EDIT: perldiag gives another clue: "in cleanup" signifies that a problem was encountered by the object's destructor. Assuming you don't already have a destructor in the object, create a MyModule::Cursor::DESTROY method that Data::Dumps the object to see what it looks like at this time.

A concise snippet of example code that exhibits this behaviour would be very helpful.

笨笨の傻瓜 2024-08-09 03:24:19

如果没有实际的代码,就很难调试它。

您是否在测试代码中使用 MyModule::Cursor ? 当您将 skip 替换为 skipper 时,您是否以与测试模块完全相同的方式调用它? 您可以在一次性脚本(单行脚本或非常短的脚本)中使用跳过吗?

我要寻找的是测试代码中的错误,而不是模块中的错误。

更新:您没有在两个不同的文件中执行诸如在 MyModule::Cursor 上声明方法之类的操作,是吗? 您收到的错误消息告诉我它有一个对 MyModule::Cursor 类型的对象的祝福引用,因此它知道该类; 但随后它找不到skip的定义。 您是否碰巧在一个文件中声明了 MyModule::Cursor 的一部分,并在另一个文件中声明了 skip ,并且您的测试模块不包含第二个文件? 或者您的 skip 定义周围是否存在语法错误——缺少分号或不成对的大括号? (但话又说回来,为什么 MyModule::Cursor::skip 可以工作,而 $cursor->skip 却不能呢?)

Without actual code, it's difficult to debug this.

Do you use MyModule::Cursor in your test code? When you replaced skip with skipper, were you calling it in exactly the same way from your test module? Are you able to use skip from a throw-away (one-liner or very short script)?

Where I'm going with this is looking for an error in your test code, rather than the module.

UPDATE: You're not doing something like declaring methods on MyModule::Cursor in two different files, are you? The error message you're getting tells me it has a blessed reference to an object of type MyModule::Cursor, so it knows about the class; but then it can't find the definition of skip. Do you happen to declare part of MyModule::Cursor in one file, and skip in another, and your test module isn't including the second file? Or do you have a syntax error somewhere around your definition of skip -- a missing semi-colon or unpaired curly brace? (But then again, why would MyModule::Cursor::skip work where $cursor->skip does not?)

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