Skip to content

@kraih /reddit.pl
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#!/usr/bin/env perl
use Mojo::Base -strict;
use Mojo::UserAgent;
# Lets start by fetching the current top Perl stories from Reddit
my $reddit =
Mojo::UserAgent->new->get('www.reddit.com/r/perl')->res->dom;
# And extract all titles from the HTML
my $titles =
$reddit->find('p.title > a.title')->map(sub { $_->text });
# How many do we got?
say 'Found ', $titles->size, ' stories.';
# Wonder how many actually contain the word "Perl"
say $titles->grep(sub {/perl/i})->size, ' contain the word Perl.';
# Top story
say 'Current top story is: ', $titles->first;
# Third story
say 'Third story is: ', $titles->[2];
# Random story that contains the word "Perl"
say 'Random Perl story: ', $titles->shuffle->first(sub {/perl/i});
# And now we go a little crazy :)
say 'Stories 2-5 in Base64:';
say $titles->slice(1 .. 4)->join('')->b64_encode;
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.