Search code examples
perlmojoliciousmojolicious-lite

mojolicious - barewords - mojocast 2


Following along with mojocasts episode 2 to learn mojolicious.

I have got the example in

#!/usr/bin/env perl
use Mojolicious::Lite;

get '/:fname/:lname' => sub {
    shift->render('hello');
};

app->start;

__DATA__

@@ hello.html.ep
<!doctype html><html>
    <head><title>Placeholders</title></head>
    <body><i>Hello <%= fname %> <%= $lname %></li></body>
</html>

However when I go to the address http://127.0.0.1:3000/sayth/renshaw I get this error from the server.

[Fri Apr 25 15:59:05 2014] [error] Bareword "fname" not allowed while "strict subs" in use at template hello.html.ep from DATA section line 3, <DATA> line 17.
1: <!doctype html><html>
2:     <head><title>Placeholders</title></head>
3:     <body><i>Hello <%= fname %> <%= $lname %></li></body>
4: </html>

I don't believe I have specified strict subs, how do I fix this?

Edit: I am running the latest version installed by curl with perl 5.16.3 installed.


Solution

  • Mojolicious enables use strict; by default. Be thankful :)

    The error is the same as you would get in perl code:

    Bareword "fname" not allowed while "strict subs" in use at template hello.html.ep
    

    Basically, you're just missing a dollar sign before fname:

    @@ hello.html.ep
    <!doctype html><html>
        <head><title>Placeholders</title></head>
        <body><i>Hello <%= $fname %> <%= $lname %></li></body>
    </html>
    

    Or you can use this format as well:

    @@ hello.html.ep
    <!doctype html><html>
        <head><title>Placeholders</title></head>
        <body><i>Hello <%= param('fname') %> <%= param('lname') %></li></body>
    </html>