I used trim(), this needs to be imported from Mojo::Util.

-Dan

On Thu, Feb 7, 2019 at 2:24 PM Ron Bergin <[email protected]> wrote:

> Dan, thank you for your input.  I haven't looked at promises much but I
> was under the impression (based on some of Joel Berger's examples as well
> as one or two other key mojolicious developers) that delays were supposed
> to be cleaner and more desirable.
>
> I knew about the issue with the for loop initialization.  Using the
> correct loop gave me a 500 error so I temporarily "broke" that while
> troubleshooting the passing of the data via the dumper helper.  I corrected
> that loop and swapped in your code but it is giving a "Premature connection
> close" error.
> $ morbo ./titles_promise.pl
> Server available at http://127.0.0.1:3000
>
> [Thu Feb  7 11:02:35 2019] [debug] GET "/titles" (604f6be1)
> [Thu Feb  7 11:02:35 2019] [debug] Routing to a callback
> [Thu Feb  7 11:02:35 2019] [error] Premature connection close
> [Thu Feb  7 11:02:35 2019] [debug] Template
> "exception.development.html.ep" not found
> [Thu Feb  7 11:02:35 2019] [debug] Template "exception.html.ep" not found
> [Thu Feb  7 11:02:35 2019] [debug] Rendering template "mojo/debug.html.ep"
> [Thu Feb  7 11:02:35 2019] [debug] Rendering template
> "mojo/menubar.html.ep"
> [Thu Feb  7 11:02:35 2019] [debug] Your secret passphrase needs to be
> changed
> [Thu Feb  7 11:02:35 2019] [debug] 500 Internal Server Error (0.028391s,
> 35.222/s)
>
>
> Thinking that it might be due to a scoping issue with $ua, I moved that
> out to have file scope, but then it produced this error:
> Use of inherited AUTOLOAD for non-method Mojo::Server::Sandbox::
> b205866ab9c46b3f8105929c8a269a25::trim() is deprecated. This will be
> fatal in Perl 5.28 at /home/rkb/test/titles_promise.pl line 23.
> [Thu Feb  7 11:19:23 2019] [error] Undefined subroutine &Mojo::Server::
> Sandbox::b205866ab9c46b3f8105929c8a269a25::trim called at /usr/local/share
> /perl/5.26.1/Mojo/Promise.pm line 123.
>
>
>
> On Thursday, February 7, 2019 at 9:48:20 AM UTC-8, Ron Bergin wrote:
>>
>> I'm brand new to Mojolicious and working on a lite script that goes to 75
>> urls and retrieves text in an h3 tag. I've worked up a working test script
>> that outputs to console and now I'm working on adjusting it to be a web app
>> but I can't seem to get the correct syntax to store/retrieve the data.
>> What am I doing wrong?
>>
>> #!/usr/bin/perl
>>
>> use Mojolicious::Lite;
>>
>> my @urls  = (
>>     'https://mojolicious.org',
>>     'https://metacpan.org',
>>     'https://perlmonks.org',
>>     'http://www.google.com',
>> );
>>
>> get '/titles' => sub {
>>     my $ua    = Mojo::UserAgent->new;
>>     my $async = Mojo::IOLoop::Delay->new;
>>
>>     $async->steps(
>>         sub {
>>             my $self = shift;
>>             $ua->get($_, $self->begin) for @urls;
>>         },
>>         sub {
>>             my ($self, @tx) = @_;
>>             my $titles      = [];
>>
>>             foreach (@tx) {
>>                 my $title  = '';
>>                 my $status = $_->res->is_success ? 'Connected' : $_->
>> error->{message};
>>
>>                 if ( $_->res->is_success ) {
>>                     $title = $_->res->dom->at('title')->text;
>>                     $title =~ s/^\s+|\s+$//g; # couldn't get Mojo::Util
>> trim function to work
>>                 }
>>
>>                 push @$titles, {host => $_->req->url->host, status =>
>> $status, title => $title};
>>             }
>>             $self->stash(titles => $titles);
>>         }
>>     );
>> } => 'titles';
>>
>> app->start;
>>
>> __DATA__
>>
>> @@ titles.html.ep
>> % layout 'titles';
>>
>> %= dumper stash('titles')
>>
>> <table border=1>
>> <tr>
>>   <th>Host</th>
>>   <th>Status</th>
>>   <th>Title</th>
>> </tr>
>>
>> <!-- % for my $server ( @{ stash('titles') } ) { -->
>> % for my $server ( stash('titles') ) {
>>   <tr>
>>     <td><%= $server->{host} %></td>
>>     <td><%= $server->{status} %></td>
>>     <td><%= $server->{title} =%></td>
>>   </tr>
>> % }
>> </table>
>>
>> @@ layouts/titles.html.ep
>> <!doctype html>
>>
>> <html>
>>
>> <head>
>>   <title>Titles</title>
>> </head>
>>
>> <body>
>> <h1>Titles</h1>
>> <%= content =%>
>> </body>
>>
>> </html>
>>
>>
>> Here's the output:
>>
>>> $ ./titles_web.pl get /titles
>>> [Thu Feb  7 09:45:54 2019] [debug] GET "/titles" (fcf21ce8)
>>> [Thu Feb  7 09:45:54 2019] [debug] Routing to a callback
>>> [Thu Feb  7 09:45:54 2019] [debug] Rendering template "titles.html.ep"
>>> from DATA section
>>> [Thu Feb  7 09:45:54 2019] [debug] Rendering template
>>> "layouts/titles.html.ep" from DATA section
>>> [Thu Feb  7 09:45:54 2019] [debug] 200 OK (0.00267s, 374.532/s)
>>> <!doctype html>
>>>
>>> <html>
>>>
>>> <head>
>>>   <title>Titles</title>
>>> </head>
>>>
>>> <body>
>>> <h1>Titles</h1>
>>>
>>> undef
>>>
>>>
>>> <table border=1>
>>> <tr>
>>>   <th>Host</th>
>>>   <th>Status</th>
>>>   <th>Title</th>
>>> </tr>
>>>
>>> <!-- % for my $server ( @{ stash('titles') } ) { -->
>>>   <tr>
>>>     <td></td>
>>>     <td></td>
>>>     <td></td>
>>> </tr>
>>> </table>
>>> </body>
>>>
>>> </html>
>>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Mojolicious" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/mojolicious.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to