Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks.

I found this code on the web this morning and I need help with a few lines of code:
#!/usr/bin/perl -w # rss2html - converts an RSS file to HTML # It take one argument, either a file on the local system, # or an HTTP URL like http://slashdot.org/slashdot.rdf # by Jonathan Eisenzopf. v1.0 19990901 # Copyright (c) 1999 Jupitermedia Corp. All Rights Reserved. # See http://www.webreference.com/perl for more information # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # INCLUDES use strict; use XML::RSS; use LWP::Simple; # Declare variables my $content; my $file; # MAIN # check for command-line argument die "Usage: rss2html.pl (<RSS file> | <URL>)\n" unless @ARGV == 1; # get the command-line argument my $arg = shift; # create new instance of XML::RSS my $rss = new XML::RSS; # argument is a URL if ($arg=~ /http:/i) { $content = get($arg); die "Could not retrieve $arg" unless $content; # parse the RSS content $rss->parse($content); # argument is a file } else { $file = $arg; die "File \"$file\" does't exist.\n" unless -e $file; # parse the RSS file $rss->parsefile($file); } # print the HTML channel &print_html($rss); # SUBROUTINES sub print_html { my $rss = shift; print <<HTML; <table bgcolor="#000000" border="0" width="200"><tr><td> <TABLE CELLSPACING="1" CELLPADDING="4" BGCOLOR="#FFFFFF" BORDER=0 widt +h="100%"> <tr> <td valign="middle" align="center" bgcolor="#EEEEEE"><font color="#0 +00000" face="Arial,Helvetica"><B><a href="$rss->{'channel'}->{'link'} +">$rss->{'channel'}->{'title'}</a></B></font></td></tr> <tr><td> HTML # print channel image if ($rss->{'image'}->{'link'}) { print <<HTML; <center> <p><a href="$rss->{'image'}->{'link'}"><img src="$rss->{'image'}->{'ur +l'}" alt="$rss->{'image'}->{'title'}" border="0" HTML print " width=\"$rss->{'image'}->{'width'}\"" if $rss->{'image'}->{'width'}; print " height=\"$rss->{'image'}->{'height'}\"" if $rss->{'image'}->{'height'}; print "></a></center><p>\n"; } # print the channel items foreach my $item (@{$rss->{'items'}}) { next unless defined($item->{'title'}) && defined($item->{'link'}); print "<li><a href=\"$item->{'link'}\">$item->{'title'}</a><BR>\n" +; } # if there's a textinput element if ($rss->{'textinput'}->{'title'}) { print <<HTML; <form method="get" action="$rss->{'textinput'}->{'link'}"> $rss->{'textinput'}->{'description'}<BR> <input type="text" name="$rss->{'textinput'}->{'name'}"><BR> <input type="submit" value="$rss->{'textinput'}->{'title'}"> </form> HTML } # if there's a copyright element if ($rss->{'channel'}->{'copyright'}) { print <<HTML; <p><sub>$rss->{'channel'}->{'copyright'}</sub></p> HTML } print <<HTML; </td> </TR> </TABLE> </td></tr></table> HTML }


Is this a hash?
<a href="$rss->{'channel'}->{'link'}">$rss->{'channel'}->{'title'}</a>


This syntax looks odd. What is it doing and how do I read it?
foreach my $item (@{$rss->{'items'}})

Thank you monks, Christine

20040321 Edit by jeffa: Changed title from 'Understanding rss?'

Replies are listed 'Best First'.
Re: Need help understanding RSS script?
by Happy-the-monk (Canon) on Mar 20, 2004 at 21:04 UTC

    Is this a hash?
    <a href="$rss->{'channel'}->{'link'}">$rss->{'channel'}->{'title'}</a>

    $rss is a reference to a "HoH"- hash of hashes.

    This syntax looks odd. What is it doing and how do I read it?
    foreach my $item (@{$rss->{'items'}})

    $rss->{'items'} is being dereferenced as an array.

    read   perldoc perlreftut   for the references,   perldoc perllol   and   perldoc perldata   for the complex data structures.

    Sören

Re: Need help understanding RSS script?
by chanio (Priest) on Mar 22, 2004 at 05:48 UTC
    It works like this:
  • You get an RSS address (like a commercial flier) that may be from a site (http://site.com/file.rss or http://site.com/cgi-bin/give.cgi/myRSS). Or from your local machine (you have the rss-file at some directory) file.rss.
  • Now you call this script to transform the XML to a simple html page (another form of XML, in fact). That page should contain some news. That is: a title of the site, an image, some lines that have a title of a news, some text explaining a little more, and a link to the site where you could read all the refered article.
  • This script has thought all the XML tag substitutions. Calling the RSS module instead of having only one way of showing the RSS content it uses certain hashes that contain the parsed elements. These are hashes inside hashes. (see perldoc) keeping the same structure of the XML tree (like folders and subdirectories). You have the content of all the RSS inside those variables. Just keep trying some until you see how they work.

    Hope it helps!