Thanks for the pointer, Corion.

Here's a simple example to try and compare my hypothetical API to Web::Scraper:

Example: Get the 5 most recent tweets from someone's Twitter page

The idea is that the demo script should take a twitter nickname as a command-line argument, and print the time, author, and text body of the last five tweets from that person's Twitter page to STDOUT.

The implementation with Web::Scraper:

use URI; use Web::Scraper; my $tweets_url = "http://twitter.com/%s"; my $tweets_query = scraper { process 'li[data-item-type="tweet"]', 'tweets[]' => scraper { process '*[data-name]', 'name' => '@data-name'; process '*[data-time]', 'time' => '@data-time'; process '.content p', 'text' => 'TEXT'; }; }; my $tweets = $tweets_query->scrape( URI->new(sprintf $tweets_url, $ARGV[0])); for my $tweet (@{$tweets->{tweets}}[0..4]) { last if !$tweet; my $date = strftime('%b %d', localtime $tweet->{time}); print "\n$tweet->{name} tweeted on $date:\n $tweet->{text}\n"; }

The implementation with my proposed API:

use My::Query qw(register_query); register_query 'recent_tweets' => { url => "http://twitter.com/%s", items => '//li[@data-item-type="tweet"]', parse => { 'name' => '//@data-name', 'time' => '//@data-time', 'text' => '//*[@class="content"]/p' }, } my $it = recent_tweets( $ARGV[0] ); for (0..4) { my $tweet = $it->() or last; my $date = strftime('%b %d', localtime $tweet->{time}); print "\n$tweet->{name} tweeted on $date:\n $tweet->{text}\n"; }

(I haven't used the "return rows as objects" thing here, as I've decided it should be optional and it doesn't gain us anything in simple cases like this.)

Sample output (same for both implementations):

$ ./recent-tweets.pl TimToady Larry Wall tweeted on Mar 15: @anocelot Lemme guess, only the first word is different... Larry Wall tweeted on Mar 13: I need to ask the Guinness folks what the current world record is fo +r number of invitations to connect on LinkedIn ignored. Larry Wall tweeted on Feb 14: @genespeth \o/ Larry Wall tweeted on Feb 12: Let us not forget that the perfect is also the enemy of the bad and +the ugly. Larry Wall tweeted on Feb 03: Wow. Just...wow. #sb48

Both get the job done, and it's certainly not a difference as day and night, but I do prefer my API even for simple cases like this, because...

What do you think?


In reply to Re^2: RFC: API for declarative parameterized web queries by smls
in thread RFC: API for declarative parameterized web queries by smls

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.