Program Google with the Net::Google Perl Module

A crisp, clean, object-oriented alternative to programming Google with Perl and the SOAP::Lite module.

An alternative, more object-oriented Perl interface to the Google API is Aaron Straup Cope’s Net::Google (http://search.cpan.org/search?query=net+ google&mode=module). While not fundamentally different from using SOAP::Lite [Hack #93] as we do throughout this book, constructing Google API queries and dealing with the results is a little cleaner.

There are three main Google API interfaces defined by the module: search(), spelling(), and cache( ) for talking to the Google Web search engine, spellchecker, and Google cache, respectively.

To provide a side-by-side comparison to googly.pl [Hack #92] , the typical SOAP::Lite-based way to talk to the Google API, we’ve provided a script identical in function and almost so in structure.

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.

Tip

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

#!/usr/local/bin/perl
# 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"; ...

Get Google Hacks, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.