Reputation: 5069
New to Mojolicious. I have two files, test1.pl and test2.pm, got error when trying to run it on my PC (Ubuntu 12.04).
$ morbo test1.pl
Couldn't load application from file "test1.pl": Can't call method "render" on an undefined value at test2.pm line 11.
Any ideas? Thanks.
Here are the two files
#########test1.pluse Mojolicious::Lite;
no warnings;
use test2;
get '/' => test2::sendMainPage;
#########test2.pm
package test2;
use Mojolicious::Lite;
use URI::Escape;
use HTML::Entities;
use Data::Dumper;
use JSON;
use Exporter 'import';
sub sendMainPage {
my $self = shift;
$self->render(text => q|<html><body>
<h1>Welcome to test demo page</h1>
</body></html>|);
}
1;
Upvotes: 1
Views: 182
Reputation: 35208
You need to pass a reference to your sub when setting up a route:
use Mojolicious::Lite;
no warnings;
use test2;
get '/' => \&test2::sendMainPage;
Otherwise, you're actually calling the sub with no parameters, and hence the error.
Also, don't do this no warnings;
. Include use strict;
and use warnings;
at the top of each and every script script you make. There are many reasons why Mojolicious::Lite
turns on those pragmas by default.
If you had done that, you would've gotten this warning which would've alerted you to the problem:
Bareword "test2::sendMainPage" not allowed while "strict subs" in use at test1.pl line 5.
And finally, always capitalize your package names. Test2
instead of 'test2
. From perlstyle
Perl informally reserves lowercase module names for "pragma" modules like integer and strict . Other modules should begin with a capital letter and use mixed case, but probably without underscores due to limitations in primitive file systems' representations of module names as files that must fit into a few sparse bytes.
Final working code:
test1.pl
use strict;
use warnings;
use Mojolicious::Lite;
use Test2;
get '/' => \&Test2::sendMainPage;
app->start;
__DATA__
Test2.pm
package Test2;
use strict;
use warnings;
sub sendMainPage {
my $self = shift;
$self->render(text => q|<html><body>
<h1>Welcome to test demo page</h1>
</body></html>|);
}
1;
__DATA__
Upvotes: 8