I think the Slashdot Headline Grabber is a good idea. I only have one question: why not using LWP to get the /. xml page? I think it's better, and using LWP it is possible to set up a proxy (very useful if you are beyond a firewall). I hacked your code a bit, and here are my suggestions:
use LWP;
use HTTP::Request::Common;
#
# ....
#
sub fetch_headlines {
my @D;
my $ua = new LWP::UserAgent;
return 0 unless ($ua);
$ua->proxy('http', 'http://myproxy.mynet.org:8080'); #set up your
+proxy here
my $url = "http://www.slashdot.org/slashdot.xml";
my $res = $ua->request(GET $url);
if ($res->is_success) {
@D = split /\n/, $res->content;
} else {
return 0;
}
my ($title, $url);
for (@D) {
$title = $1 if /\<title\>(.*)\<\/title\>/;
$url = $1 if /\<url\>(.*)\<\/url\>/;
if (/<\/story>/) {
$stories{$url} = $title;
push(@keys, $url);
$title = "";
$url = "";
}
}
return 1;
}
It may also be a good idea to use XML::DOM to parse the XML downloaded from /. but that is probably too much for the Headline Grabber: the for loop is quicker.
Any comment is highly appreciated.
marcos
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.