11

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.

John Siracusa
  • 14,971
  • 7
  • 42
  • 54
Weiyan
  • 1,116
  • 3
  • 14
  • 25
  • @radkrish, I know that you were trying to help by editing, however I think that you have changed some of the context of the question. It is now no longer obvious where one file ends and the next begins, this is important since, as seen in my answer, if this were all one file, the OP would not be getting this error. Could you please carefully check this edit – Joel Berger Feb 16 '11 at 05:19
  • @Joel, code format revoked. Regret for the context change – R K Feb 16 '11 at 05:26

2 Answers2

23

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.

Joel Berger
  • 20,180
  • 5
  • 49
  • 104
  • Just follow the example of Rose::DB . Eding package with 1; is normal package practice. In this case, the package My::DB:Object's last line init_db(My::DB->new) is supposed to excute at initialization and return true value. It seems this command failed to run. – Weiyan Feb 16 '11 at 06:10
17

The last line in any module should be

1;
gergi
  • 469
  • 2
  • 5
  • First of all, I already posted a VERY SIMILAR answer. Second this is not true. A `use` or `require` needs a true return so that diagnostics may be done on loading. Yes its true MOST modules end in `1;` but not true for all, including the OP, see his/her comment to me. – Joel Berger Feb 16 '11 at 16:18
  • The 1; in last line is to ensure returning true. If append 1; in this case, at raise another error, not behave as the tutor suggested. – Weiyan Feb 17 '11 at 00:36
  • @JoelBerger have a point. But if you are running out of time in the middle of the night this is all you need xaxa. – Ivan Kaloyanov Mar 27 '19 at 16:07