English 中文(简体)
Perl模块“未返回真值”
原标题:Perl module "did not return a true value"

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

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…

致以问候和感谢。

问题回答

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

您也可以在perldoc-perlmod,或者perldoc-f需要

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.

任何模块中的最后一行应为

1;




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签