in reply to Writing a simple RSS feed 'grabber' with XML::Parser.

Hi DK. I'm trying to figure out what your objective here is. Are you trying to learn how XML::Parser works? Or are you trying to do something with RSS? I mean, if its the latter then I would do it like this:

#!/usr/bin/perl -w use strict; use XML::Simple; use LWP::Simple; use Data::Dump::Streamer; $|++; my $ticker=['http://perlmonks.org/index.pl?node_id=30175&xmlstyle=rss' +, "http://rss.news.yahoo.com/rss/science"]->[rand 2]; print "Getting RSS from $ticker\n"; my $feed = get($ticker); print "Parsing RSS...\n"; my $ref = XMLin($feed); print "Dumping Parse Tree...\n"; Dump $ref;

If its the former then I can't really help much beyond pointing out that what you are doing with the lexical var "$feed" in there scares the willies out of me.


---
demerphq

    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi

    Flux8


Replies are listed 'Best First'.
•Re^2: Writing a simple RSS feed 'grabber' with XML::Parser.
by merlyn (Sage) on Oct 20, 2004 at 10:11 UTC
    And in today's "making Perl work a lot harder than you need to do", let's nominate the following entry:
    my $ticker=['http://perlmonks.org/index.pl?node_id=30175&xmlstyle=rss' +, "http://rss.news.yahoo.com/rss/science"]->[rand 2];
    So, we've asked Perl to construct an array, take a reference to it, then dereference that reference to pick out one of the items, then discard the reference, which then garbage-collects the array. All when we could have written that this way:
    my $ticker=('http://perlmonks.org/index.pl?node_id=30175&xmlstyle=rss' +, "http://rss.news.yahoo.com/rss/science")[rand 2];
    saving two characters of typing, and all that mess of creating the new array and reference and garbage collecting. We're simply constructing a list, then picking out an element of that list with a literal slice (a construct I suggested for Perl 3, by the way {grin}).

    To optimize this further, I'd go with a qw for that first list:

    my $ticker=(qw(http://perlmonks.org/index.pl?node_id=30175&xmlstyle=rs +s http://rss.news.yahoo.com/rss/science))[rand 2];
    And in recent versions of Perl, you can even drop that outer set of parens:
    my $ticker=qw(http://perlmonks.org/index.pl?node_id=30175&xmlstyle=rss http://rss.news.yahoo.com/rss/science)[rand 2];

    I saw bracket-arrow-bracket as a "cute syntax" once. I'm trying to stomp it out, because there's an equivalent construct (as I showed) that is a lot less work for Perl. Please don't propogate "cute syntax" that is more expensive.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      perl -MBenchmark=cmpthese -e'cmpthese -1, { cute => sub { [0,1]->[rand + 2] }, list => sub { qw(0 1)[rand 2] } }'
      Rate cute list cute 768000/s -- -74% list 2899719/s 278% --
      Yes, the list slice is much faster. But for something that probably runs only once in every 5 minutes, isn't 768000 per second fast enough? Optimizing seems premature here. For things like this, I am against choosing a particular language or syntax for its speed.

      I'm not saying that your reply is useless. It's important to know what code does and this information will certainly help some of the readers when they do have to optimize. But the code is written now and not much is gained by changing it, so I'd just let it be. Programmer time is still much more expensive than computer time.

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

        Please don't trot this out as an "optimization". What I'm saying is that this "cute syntax" is in fact gaining nothing, and only loses. So, if the only thing it's optimized for is "cuteness", it doesn't belong in the standard idiom list.

        So, at worst, I'm saving typing. Typically, I'm also saving execution speed. Why would you have a complaint about that?

        And what about the "programmer time" to figure out what the "cute" construct does? There's a real expense there. The literal slice is well documented. The cute block-arrow-block form shows up more like an obfuscation than a clarification, costing clarity, but for what reward? None, ever. Why unnecessarily obfuscate your code?

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

      Yep, guilty as charged. But lighten up a little. It was just a snippet to make things a little more interesting.


      ---
      demerphq

        First they ignore you, then they laugh at you, then they fight you, then you win.
        -- Gandhi

        Flux8


      at least you have your disclaimer to guard against anyone thinking that you're an ass...