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.
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.
Tip
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: ...
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.