Pass array to stash in mojolicious

438 views
Skip to first unread message

Luis Diaz

unread,
Aug 17, 2017, 3:16:21 AM8/17/17
to Mojolicious
Hi!

I'm trying to create a dynamic page that shows the content of a file in a webpage using perl and mojolicious. For future reference, I need to display de data and be able to manipulate it and save the file again. Maybe some of you know a better way to handle this than mojolicious, but, meanwhile... 

I have been reading several blogs and posts (mostly this) to know how to pass the value of an array to a html layout to display the data, but I think I don't understand how this works. I think that the bit that I need it's the last one of that page but I'm struggling to get the array at the site and I don't know how to move on.

The reading file part it's not there because I prefer dealing with just one thing at a time to isolate issues and make the development easier. My code looks like this right now:

#!/usr/bin/env perl

use Mojolicious::Lite;
my @ref_pets = ();

for(my $i=0; $i<=10;$i++){
    # $ref_pets = $ref_pets.',[n='.$i.']';
    push @ref_pets, $i;

}
# for(my $i=0; $i<=10;$i++){
#     say @ref_pets[$i];
# }

for my $loop(@ref_pets){
    say $loop;
}

get '/' => sub {
    my $self = shift;
    # $ref_pets = $ref_pets.',['isolde','cat'],['oscar','dog']';
    $self->stash(
        ref_pets_stash => @ref_pets
    );
} => 'index';

app->start('daemon', '-l', 'http://*:8080');

__DATA__

@@ index.html.ep
%layout 'default';
<p>Welcome to arratrefs</p>

<p>
    <ul>
        %for my $loop (@{@ref_pets_stash}){
            <li>Name of the pet: <%= $loop-> %></li>
        %};
    </ul>
</p>

@@ layouts/default.html.ep
<!doctype html><html>
    <head>
        <title>Default</title>
    </head>
    <body>
        <%== content%>
    </body>
</html>


And when running the code, the terminal output it's this:

$ perl .\Data-to-stash-TEST.pl
0
1
2
3
4
5
6
7
8
9
10
[Thu Aug 17 09:12:33 2017] [info] Listening at "http://*:8080"
Server available at http://127.0.0.1:8080


and after enter the url of the server:

[Thu Aug 17 09:14:23 2017] [debug] Inactivity timeout
[Thu Aug 17 09:14:53 2017] [debug] GET "/"
[Thu Aug 17 09:14:53 2017] [debug] Routing to a callback
[Thu Aug 17 09:14:53 2017] [debug] Rendering template "index.html.ep" from DATA section
[Thu Aug 17 09:14:53 2017] [error] Can't use global $1 in "my" at template index.html.ep from DATA section line 1, near ",$1"
Global symbol "$cb" requires explicit package name (did you forget to declare "my $cb"?) at template index.html.ep from DATA section line 1.
Global symbol "@ref_pets_stash" requires explicit package name (did you forget to declare "my @ref_pets_stash"?) at template index.html.ep from DATA section line 6.
syntax error at template index.html.ep from DATA section line 7, at EOF
1: %layout 'default';
2: <p>Welcome to arratrefs</p>
3:
4: <p>
5:     <ul>
6:         %for my $loop (@{@ref_pets_stash}){
 
[Thu Aug 17 09:14:53 2017] [debug] Template "exception.development.html.ep" not found
[Thu Aug 17 09:14:53 2017] [debug] Template "exception.html.ep" not found
[Thu Aug 17 09:14:53 2017] [debug] Rendering template "mojo/debug.html.ep"
Mojo::Reactor::Poll: I/O watcher failed: Can't use global $3 in "my" at template mojo/debug.html.ep line 1, near ",$3"
Global symbol "$format" requires explicit package name (did you forget to declare "my $format"?) at template mojo/debug.html.ep line 1.
Global symbol "$cb" requires explicit package name (did you forget to declare "my $cb"?) at template mojo/debug.html.ep line 1.
Global symbol "$snapshot" requires explicit package name (did you forget to declare "my $snapshot"?) at template mojo/debug.html.ep line 1.
Global symbol "$handler" requires explicit package name (did you forget to declare "my $handler"?) at template mojo/debug.html.ep line 1.
Global symbol "$exception" requires explicit package name (did you forget to declare "my $exception"?) at template mojo/debug.html.ep line 1.
Global symbol "$ref_pets_stash" requires explicit package name (did you forget to declare "my $ref_pets_stash"?) at template mojo/debug.html.ep line 1.
Global symbol "$snapshot" requires explicit package name (did you forget to declare "my $snapshot"?) at template mojo/debug.html.ep line 279.
1: <!DOCTYPE html>
2: <html>
3:   <head>
4:     % my $title = stash('exception') ? 'Server error' : 'Page not found';
5:     <title><%= $title %> (<%= app->mode %> mode)</title>
6:     <meta http-equiv="Pragma" content="no-cache">

I'm really really lost and I'll really apreciate your help.

Thanks and greetings.

Luis Diaz

unread,
Aug 17, 2017, 4:45:29 AM8/17/17
to Mojolicious
Hi!
 
I've been able to solve it with this post. maybe someone in the same situation as me will find it useful.

Greetings!
Reply all
Reply to author
Forward
0 new messages