in reply to Re: Trying to get an RSS to display
in thread Trying to get an RSS to display

It has been updated, but is not the most current version.

Replies are listed 'Best First'.
Re^3: Trying to get an RSS to display
by Anonymous Monk on Oct 27, 2011 at 04:54 UTC

    Welcome, see The Perl Monks Guide to the Monastery, see How do I post a question effectively?, Where should I post X?

    Your updated code does not compile :)

    If you look at the docs for XML::RSS you can see an example of accessing items . See also References quick reference

    #!/usr/bin/perl -- use constant DEBUG => !!( 0 || $ENV{PERL_DEBUG_MYAPPNAME} ); use CGI::Carp qw( fatalsToBrowser ); use CGI; # to avoid those pesky 500 errors BEGIN { CGI::Carp::set_message( sub { print "<h1>something broke, we know what it is, thank you, + try again later</h1>\n"; if (DEBUG) { print '<p>', CGI->escapeHTML(@_), '</p>'; } } ); } ## end BEGIN use strict; use warnings; use Data::Dumper (); use LWP::Simple qw( get ); use XML::RSS; Main( @ARGV ); exit( 0 ); sub Main { #~ return DebugCGI(); # generic, env.cgi return RSSMoo('file:animals.rss.xml'); # for testing #~ return RSSMoo("http://feeds.feedburner.com/animals"); } ## end sub Main sub RSSMoo { my $url = shift; my $rss_content = get($url); my $cgi = CGI->new; my $feed = XML::RSS->new->parse($rss_content); binmode STDOUT, ":encoding(UTF-8)"; $cgi->charset('UTF-8'); print $cgi->header, $cgi->start_html; for my $item ( @{ $feed->{items} } ) { print '<div style="border: 2px solid black ;">'; print FudgeA( $item ); print '</div>'; } print '<div style="white-space: pre; overflow: scroll; height: 300 +px;">', $cgi->escapeHTML( DD( $feed ) ), '</div><hr><hr><hr>'; print $cgi->end_html; } ## end sub RSSMoo sub FudgeA { my $iref = shift; return '<p>'.CGI->a( { -href => $iref->{link}, }, CGI->escapeHTML( + $iref->{title} ) ).'</p>'; } sub FudgeB { my $iref = shift; return CGI->new( $iref )->Dump . '<hr>'; } sub FudgeC { my $iref = shift; my $ret = '<div>'; $ret .= '<p>' . CGI->escapeHTML( $iref->{title} ) . '</p>'; while( my( $k, $v ) = each %$iref ){ $ret .= "<p><strong>"; $ret .= CGI->escapeHTML( $k ); $ret .= "</strong> "; $ret .= CGI->escapeHTML( $v ); $ret .= '</p>'; } $ret .= '</div>'; return $ret; } sub DebugCGI { my $cgi = CGI->new; print $cgi->header(); # Write HTTP header print $cgi->start_html, $cgi->b( rand time, ' ', scalar gmtime ), '<table border="1" width="%100"><tr><td>', $cgi->Dump, '</td>', '<td><div style="white-space: pre-wrap; overflow: scroll;">', $cgi->escapeHTML( DD($cgi) ), '</div></td></tr></table>', CGI->new( \%ENV )->Dump, $cgi->end_html; } ## end sub DebugCGI sub DD { scalar Data::Dumper->new( \@_ )->Indent(1)->Useqq(1)->Dump; }
      Thanks. That really helped. :)
      Okay so I go it to work by doing it a little different than what you directed me to, but it works. So to give everyone what it looks like now, here is the code:
      #!/usr/bin/perl # RSS CGI Prog use strict; use warnings; use CGI; use XML::RSS; use LWP::Simple; use Data::Dumper; my $cgi = CGI->new; my $url = "http://feeds.feedburner.com/animals"; my $content; my $rss = XML::RSS->new; if ($url=~ /http:/i) { $content = get($url); die "Could not retrieve $url" unless $content; $rss->parse($content); } print $cgi->header, $cgi->start_html(-style=>"/css/cgi_lesson1.css",-title=>"This is + an RSS thingy."); print_html($rss); sub print_html { my $rss = shift; print <<"HTML"; <div class="container"><div class="header"><B><center><h2><a href="$rs +s->{'channel'}->{'link'}">$rss->{'channel'}->{'title'}</a></h2></cent +er></B> HTML 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 qq{ width="$rss->{'image'}->{'width'}"} if $rss->{'image'}->{'width'}; print qq{ height="$rss->{'image'}->{'height'}"} if $rss->{'image'}->{'height'}; print "></a></center><p></div>\n"; } print qq{<div class="content">}; foreach my $item (@{$rss->{'items'}}) { next unless defined($item->{'title'}) && defined($item->{'link'}); print qq{<a href="$item->{'link'}">$item->{'title'}</a><BR>\n$item +->{'description'}}; } if ($rss->{'channel'}->{'copyright'}) { print <<"HTML"; </div> <div class="footer"><center><p><sub>$rss->{'channel'}->{'copyright'}</ +sub></p></center> HTML } print <<"HTML"; </div></div> HTML } print $cgi->end_html;

        Okay so I go it to work by doing it a little different than what you directed me to, but it works

        "it works" for now. You're missing an awful lot of escapeHTML