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

Hello Monks,

In the following program (perl cookbook recipe):
use XML::RSS; my $rss = XML::RSS->new; $rss->parsefile($RSS_FILENAME); my @items = @{$rss->{items}}; foreach my $item (@items) { print "title: $item->{'title'}\n"; print "link: $item->{'link'}\n\n"; }

I don't understand exactely what:
my @items = @{$rss->{items}};

is and why the author is using:
print "title: $item->{'title'}\n"; print "link: $item->{'link'}\n\n";

to obtain the values. I assume these are references of some kind but could someone explain the unusual syntax to me?

Thank you.

Alexa

Replies are listed 'Best First'.
Re: Confused by RSS reader program.
by davido (Cardinal) on Dec 26, 2005 at 03:06 UTC

    Welcome to complex datastructures.

    my @items = @{$rss->{items}}; means this: Start with the hashref held in $rss. Dereferece that hashref, inspecting the content of $rss->{items}. Apparently $rss->{items} contains an array reference. The array reference is dereferenced with @{....}. And the resulting list is assigned to @items.

    Later, $item->{'title'} is dereferencing the hashref held in $item. The value held in $item->{'title'} is apparently...the title. ;)

    See perlreftut, perllol, and perlref for some informative reading.


    Dave

Re: Confused by RSS reader program.
by planetscape (Chancellor) on Dec 26, 2005 at 06:20 UTC
Re: Confused by RSS reader program.
by tirwhan (Abbot) on Dec 26, 2005 at 07:22 UTC

    davido has already explained perfectly how to analyse the existing structure. Just for completeness, here is how you'd build this up yourself. Two ways are shown, one using "ordinary" structures and taking references to these, the second by building the final structure up directly with anonymous hash/array references:

    # 1: %item = ( title => 'SomeTitle', link => 'http://somelink' ); push (@items,\%item); %rss = ( items => \@items ); $rss = \%rss; #2: $rss = { items => [ { title => 'SomeTitle', link => 'http://somelink' } ] };

    A computer is a state machine. Threads are for people who can't program state machines. -- Alan Cox