Created
October 12, 2011 11:25
-
-
Save kraih/1280965 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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