| 1 | package Fortune::WWW::Postillion; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use WWW::Mechanize; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use HTML::Parser; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use base 'Exporter::Tiny'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | our @EXPORT_OK = qw/cookie/; | |||||||||
| 10 | 1 | ▶ | use Mojo::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | Fortune::WWW::Postillion - Get fortune cookies from http://www.der-postillion.com! | ||||||||||
| 15 | |||||||||||
| 16 | =head1 VERSION | ||||||||||
| 17 | |||||||||||
| 18 | =cut | ||||||||||
| 19 | |||||||||||
| 20 | 1 | our $VERSION = '0.03'; | |||||||||
| 21 | |||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||
| 23 | |||||||||||
| 24 | Fortune::WWW::Postillion gives you fortune cookies from the newsticker archive | ||||||||||
| 25 | of the webside C<http://www.der-postillion.com>. | ||||||||||
| 26 | |||||||||||
| 27 | use Fortune::WWW::Postillion 'cookie'; | ||||||||||
| 28 | |||||||||||
| 29 | |||||||||||
| 30 | my $random_cookie = cookie(); | ||||||||||
| 31 | my $third_cookie = cookie(3); | ||||||||||
| 32 | |||||||||||
| 33 | =head1 EXPORT | ||||||||||
| 34 | |||||||||||
| 35 | |||||||||||
| 36 | =cut | ||||||||||
| 37 | |||||||||||
| 38 | 1 | our @text; | |||||||||
| 39 | |||||||||||
| 40 | |||||||||||
| 41 | sub text_handler | ||||||||||
| 42 | 0 | ▶ | { | ||||||||
|
Subroutine
text_handler: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 43 | 0 | my ($self, $text) = @_; | |||||||||
| 44 | 0 | ▶ | if ($text =~ m/[+]{4}\s*(.+?)\s*[+]{4}/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 0 | push @text, $1; | |||||||||
| 46 | } | ||||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | =head1 SUBROUTINES | ||||||||||
| 50 | |||||||||||
| 51 | =head2 cookie | ||||||||||
| 52 | |||||||||||
| 53 | Get a new fortune cookie. You can either request a random cookie or give a | ||||||||||
| 54 | number n and the module will try to return the nth element of the cookie | ||||||||||
| 55 | list. The number is used modulo the number of elements in the cookie jar. So | ||||||||||
| 56 | if you ask for the 5th cookie in a 4 element jar, you will get the first one. | ||||||||||
| 57 | |||||||||||
| 58 | @param int - number of element in news list | ||||||||||
| 59 | |||||||||||
| 60 | @return success - string | ||||||||||
| 61 | @return error - exception | ||||||||||
| 62 | |||||||||||
| 63 | @throws die() | ||||||||||
| 64 | |||||||||||
| 65 | =cut | ||||||||||
| 66 | |||||||||||
| 67 | sub cookie | ||||||||||
| 68 | 0 | ▶ | { | ||||||||
|
Subroutine
cookie: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 69 | 0 | my ($num_cookie) = @_; | |||||||||
| 70 | 0 | my $url = 'http://www.der-postillon.com/search/label/Newsticker'; | |||||||||
| 71 | |||||||||||
| 72 | 0 | my $ua = Mojo::UserAgent->new; | |||||||||
| 73 | 0 | my $body = $ua->get($url)->res->body; | |||||||||
| 74 | # Create parser object | ||||||||||
| 75 | 0 | my $parser = HTML::Parser->new(); | |||||||||
| 76 | 0 | $parser->handler(text => \&text_handler); | |||||||||
| 77 | |||||||||||
| 78 | 0 | $parser->parse($body); | |||||||||
| 79 | 0 | $parser->eof; | |||||||||
| 80 | 0 | ▶ | if (not $num_cookie) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 0 | $num_cookie = int(rand(@text)) + 1; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 0 | return $text[($num_cookie-1) % int @text]; | |||||||||
| 85 | |||||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | =head1 AUTHOR | ||||||||||
| 89 | |||||||||||
| 90 | Maik Hentsche, C<< <Caldrin at cpan dot org> >> | ||||||||||
| 91 | |||||||||||
| 92 | =head1 BUGS | ||||||||||
| 93 | |||||||||||
| 94 | Please report any bugs or feature requests to C<bug-fortune-www-postillion at rt.cpan.org>, or through | ||||||||||
| 95 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Fortune-WWW-Postillion>. I will be notified, and then you'll | ||||||||||
| 96 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 97 | |||||||||||
| 98 | |||||||||||
| 99 | |||||||||||
| 100 | |||||||||||
| 101 | =head1 SUPPORT | ||||||||||
| 102 | |||||||||||
| 103 | You can find documentation for this module with the perldoc command. | ||||||||||
| 104 | |||||||||||
| 105 | perldoc Fortune::WWW::Postillion | ||||||||||
| 106 | |||||||||||
| 107 | |||||||||||
| 108 | You can also look for information at: | ||||||||||
| 109 | |||||||||||
| 110 | =over 4 | ||||||||||
| 111 | |||||||||||
| 112 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||
| 113 | |||||||||||
| 114 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Fortune-WWW-Postillion> | ||||||||||
| 115 | |||||||||||
| 116 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 117 | |||||||||||
| 118 | L<http://annocpan.org/dist/Fortune-WWW-Postillion> | ||||||||||
| 119 | |||||||||||
| 120 | =item * CPAN Ratings | ||||||||||
| 121 | |||||||||||
| 122 | L<http://cpanratings.perl.org/d/Fortune-WWW-Postillion> | ||||||||||
| 123 | |||||||||||
| 124 | =item * Search CPAN | ||||||||||
| 125 | |||||||||||
| 126 | L<http://search.cpan.org/dist/Fortune-WWW-Postillion/> | ||||||||||
| 127 | |||||||||||
| 128 | =back | ||||||||||
| 129 | |||||||||||
| 130 | |||||||||||
| 131 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 132 | |||||||||||
| 133 | |||||||||||
| 134 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 135 | |||||||||||
| 136 | Copyright 2012 Maik Hentsche. | ||||||||||
| 137 | |||||||||||
| 138 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 139 | under the terms of either: the GNU General Public License as published | ||||||||||
| 140 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 141 | |||||||||||
| 142 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 143 | |||||||||||
| 144 | |||||||||||
| 145 | =cut | ||||||||||
| 146 | |||||||||||
| 147 | 1 | 1; # End of Fortune::WWW::Postillion | |||||||||