Previous Section  < Day Day Up >  Next Section

Hack 92. Program Google in Perl

This simple script illustrates the basics of programming the Google Web API with Perl and lays the groundwork for the lion's share of hacks to come.

The vast majority of hacks in this book are written in Perl. While the specifics vary from hack to hack, much of the busy work of querying the Google API and looping over the results remain essentially the same. This hack is utterly basic, providing a foundation on which to build more complex and interesting applications. If you haven't done anything of this sort before, this hack is a good starting point for experimentation. It simply submits a query to Google and prints out the results.

9.9.1. The Code

Type the following code into your preferred plain-text editor—be it Notepad, TextEdit, or a command-line editor like vi or Emacs—and save it to a file named googly.pl. Remember to replace insert key here with your Google API key, as explained in "Using Your Google API Key" earlier in this chapter.

In addition to the Google API Developer's Kit, you'll need the SOAP::Lite Perl module installed [Hack #93] before running this hack.


#!/usr/local/bin/perl

# googly.pl

# A typical Google Web API Perl script.

# Usage: perl googly.pl <query>

     

# Your Google API developer's key.

my $google_key='insert key here';

     

# Location of the GoogleSearch WSDL file.

my $google_wdsl = "./GoogleSearch.wsdl";

     

use strict;

     

# Use the SOAP::Lite Perl module.

use SOAP::Lite;

     

# Take the query from the command line.

my $query = shift @ARGV or die "Usage: perl googly.pl <query>\n";

     

# Create a new SOAP::Lite instance, feeding it GoogleSearch.wsdl.

my $google_search = SOAP::Lite->service("file:$google_wdsl");

     

# Query Google.

my $results = $google_search -> 

    doGoogleSearch(

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

      "", "latin1", "latin1"

    );

     

# No results?

@{$results->{resultElements}} or exit;

     

# Loop through the results.

foreach my $result (@{$results->{resultElements}}) {

 # Print out the main bits of each result

 print

  join "\n",  

  $result->{title} || "no title",

  $result->{URL},

  $result->{snippet} || 'no snippet',

  "\n";

}

9.9.2. Running the Hack

Run this script from the command line ["How to Run the Hacks" in the Preface], passing it any Google search that you want to run like so:

$ perl googly.pl  "query keywords"

9.9.3. The Results

Here's a sample run. The first attempt doesn't specify a query and so triggers a usage message and doesn't go any further. The second searches for learning perl and prints out the results.

% perl googly.pl

Usage: perl googly.pl <query>

% perl googly.pl "learning perl"



oreilly.com -- Online Catalog: Learning 

Perl, 3rd Edition

http://www.oreilly.com/catalog/lperl3/

... learning perl, 3rd Edition Making Easy Things Easy and Hard Things 

Possible By Randal L. Schwartz, Tom Phoenix 3rd Edition July 

2001 0-596-00132-0

...

Amazon.com: buying info: learning perl (2nd Edition)

http://www.amazon.com/exec/obidos/ASIN/1565922840

... learning perl takes common programming idioms and expresses them 

in "perlish"<br> terms. ... (learning perl, 

Programming Perl, Perl Cookbook).

    Previous Section  < Day Day Up >  Next Section