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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|