Skip to content

@jberger /titles.pl
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Grabbing website titles, non-blocking.
#!/usr/bin/env perl
use Mojolicious::Lite;
app->ua->max_redirects(10);
my @urls = qw/mojolicio.us mojocasts.com/;
helper 'render_dumper' => sub {
my $self = shift;
$self->render( text => $self->dumper( \@_ ) );
};
any '/one' => sub {
my $self = shift;
$self->render_later;
$self->ua->get($urls[0] => sub {
my ($ua, $tx) = @_;
$self->render_dumper($tx->res->dom->at('title')->text);
});
};
any '/two' => sub {
my $self = shift;
$self->render_later;
my @titles;
$self->ua->get($urls[0] => sub {
my ($ua, $tx) = @_;
push @titles, $tx->res->dom->at('title')->text;
$self->ua->get($urls[1] => sub {
my ($ua, $tx) = @_;
push @titles, $tx->res->dom->at('title')->text;
$self->render_dumper(@titles);
});
});
};
any '/all' => sub {
my $self = shift;
$self->render_later;
my $delay = Mojo::IOLoop->delay(sub{
my $delay = shift;
my @titles = map { $_->res->dom->at('title')->text } @_;
$self->render_dumper(@titles);
});
$self->ua->get( $_ => $delay->begin ) for @urls;
};
app->start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.