public
Created

  • Download Gist
reddit.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#!/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;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.