O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  

Program Google with the Net::Google Perl Module
A crisp, clean, object-oriented alternative to programming Google with Perl and the SOAP::Lite module
The Code
[Discuss (0) | Link to this hack]

The Code

Save the following script as net_googly.pl. Replace insert key here with your Google API key as you type in the code.


Mind you, you'll still need SOAP::Lite and a couple of other prerequisites to use Net::Google.

# net_googly.pl
# A typical Google API script using the Net::Google Perl module.
# Usage: perl net_googly.pl <query>
use strict;
# Use the Net::Google Perl module.
use Net::Google;
# Your Google API developer's key.
use constant GOOGLE_API_KEY => 'insert key here';
# Take the query from the command line.
my $query = shift @ARGV or die "Usage: perl net_googly.pl <query>\n";
# Create a new Net::Google instance.
my $google = Net::Google->new(key => GOOGLE_API_KEY);# And create a new Net::Google search instance.
my $search = $google->search( );# Build a Google query.

# Query Google.
$search->results( );
# Loop through the results.
foreach my $result ( @{$search->results( )} ) {
 # Print out the main bits of each result.
  join "\n",  
  $result->title( ) || "no title",
  $result->URL( ),
  $result->snippet( ) || 'no snippet',

Notice that the code is all but identical to that of googly.pl . The only real changes (called out in bold) are cleaner object-oriented method calls for setting query parameters and dealing with the results. So, rather than passing a set of parameters to a SOAP::Lite service call like this:

      $google_key, $query, 0, 10, "false", "",  "false",
      "", "latin1", "latin1"

Set these parameters individually like this:


Not much difference, but definitely cleaner.

O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.