Perl 模块“未返回真值”

发布于 2024-10-17 08:47:23 字数 673 浏览 2 评论 0原文

我遵循了 CPAN 上的 Rose::DB::Object 教程并设置三个包。

package My::DB::Object;
use My::DB;
use base qw(Rose::DB::Object);
sub init_db { My::DB->new }

package My::DB;
use base qw(Rose::DB);
...

package Motorcycle;
use base 'My::DB::Object';

__PACKAGE__->meta->setup
(
  ...
);

__PACKAGE__->meta->make_manager_class('motorcycles');

在应用程序中:

package main;

use Motorcycle;
use Mojolicious::Lite;

无法编译并出现此错误:

My/DB/Object did not return a true value <eval 2> line 2…

问候和感谢。

I followed the Rose::DB::Object tutorial on CPAN and set up three packages.

package My::DB::Object;
use My::DB;
use base qw(Rose::DB::Object);
sub init_db { My::DB->new }

package My::DB;
use base qw(Rose::DB);
...

package Motorcycle;
use base 'My::DB::Object';

__PACKAGE__->meta->setup
(
  ...
);

__PACKAGE__->meta->make_manager_class('motorcycles');

In the application:

package main;

use Motorcycle;
use Mojolicious::Lite;

This failed to compile with this error:

My/DB/Object did not return a true value <eval 2> line 2…

Regards and thanks.

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

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

发布评论

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

评论(2

爱*していゐ 2024-10-24 08:47:23

虽然我不能说我完全理解您想要完成的任务,但您看到的错误是一个相当常见的错误。 userequire 中包含的任何文件/模块都必须返回“true”值。这通常是通过以 1; 行结束该文件来完成的,也就是说,只是一个 true 的命令(而不是 0 为 false)。查看系统上以 .pm 结尾的任何其他文件,它很可能会以这种方式结束。

还可以在perldoc perlmod中阅读更多内容,或者有这样的说法来自 perldoc -f require

文件必须返回 true 作为最后一个
表示成功的语句
执行任何初始化代码,
所以习惯上结束这样的文件
与“1;”除非你确定它会
否则返回 true。但它是
最好只放“1;”,以防万一
您添加更多语句。

While I can't say I fully understand what it is you are trying to accomplish, the error you are seeing is a fairly common one. Any file/module that is included with a use or require must return a "true" value. This is usually accomplished by ending that file with the line 1;, that is to say simply a command that is true (as opposed to 0 being false). Look at any other file ending in .pm on your system and it is likely to end this way.

You can also read more in perldoc perlmod, or there is this statement from perldoc -f require:

The file must return true as the last
statement to indicate successful
execution of any initialization code,
so it's customary to end such a file
with "1;" unless you're sure it'll
return true otherwise. But it's
better just to put the "1;", in case
you add more statements.

北陌 2024-10-24 08:47:23

任何模块中的最后一行应该是

1;

The last line in any module should be

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