#!/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') % for my $server ( stash('titles') ) { % }
Host Status Title
<%= $server->{host} %> <%= $server->{status} %> <%= $server->{title} =%>
@@ layouts/titles.html.ep Titles

Titles

<%= content =%>