Listing 1. google-query.pl

#!/usr/bin/perl

use strict;
use diagnostics;
use warnings;

use SOAP::Lite;

# ------------------------------------------------------------
# Get the Google key from ~/.google_key
my $google_key_file = "/Users/reuven/.google_key";
open GOOGLE_KEY, $google_key_file or die "Cannot read
'$google_key_file': $! ";

my ($google_key) = <GOOGLE_KEY>;
chomp $google_key;

close GOOGLE_KEY;

# ------------------------------------------------------------
# Get the command-line argument
if ($#ARGV != 0)
{
    print "$0: Invoke with a single argument, your Google search
term.\n";
    exit;
}

my $query_string = shift @ARGV;

# ------------------------------------------------------------
# Get the WSDL file
my $google_wsdl = "http://api.google.com/GoogleSearch.wsdl";
my $query = SOAP::Lite->service($google_wsdl);

# ------------------------------------------------------------
# Use the WSDL to make the query
my $starting_page = 1;
my $max_results = 10;
my $filter = 'false';
my $geographic_restriction = '';
my $safe_search = 'false';
my $language_restriction = '';

my $results =
    $query->doGoogleSearch($google_key,
               $query_string,
               $starting_page,
               $max_results,
               $filter,
               $geographic_restriction,
               $safe_search,
               $language_restriction, 'utf-8', 'utf-8');

my @results = @{$results->{resultElements}};

if (@results)
{
    # Iterate through each result we got
    my $counter = 1;
    foreach my $result (@results)
    {
    print "Result $counter of ", $#results + 1, ":\n";

    foreach my $key (sort keys %{$result})
    {
        my $value = $result->{$key};

        # Is this a hash value?  If so, display it accordingly
        if (UNIVERSAL::isa($value, 'HASH'))
        {
        print "\t'$key':\n";

        foreach my $subkey (sort keys %{$value})
        {
            print "\t\t'$subkey' => '$value->{$subkey}'\n";
        }
        }

        # Display the value as a simple string

        else
        {
        print "\t'$key' => '$value'\n";
        }
    }

    $counter++;
    }
}
else
{
    print "There were no results for your query of '$query_string'.\n";
}

