#!/usr/bin/perl# google.pl - command line tool to search google# 2009 by Stefan Grothkopp, this code is public domain use it as you wish!useLWP::Simple;useTerm::ANSIColor;# change this to false for b/w output$use_color=true;#result size: large=8, small=4$result_size="large";# unescape unicode characters in" content"sub unescape{my($str)=splice(@_);$str=~s/\u(.{4})/chr(hex($1))/eg;return$str;}# number of command line args$numArgs=$#ARGV+1;if($numArgs==0){# print usage info if no argument is givenprint"Usage:n";print"$0 <searchterm>n";}else{# use first argument as query string$q=$ARGV[0];# url encode query string$q=~s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;# get json encoded search result from google ajax apimy$content=get("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&start=0&rsz=$result_size&q=$q");#Get web page in contentdie"get failed"if(!defined$content);# ugly result parsing (did not want to depend on a parser lib for this quick hack)while($content=~s/"unescapedUrl":"([^"]*)".*?"titleNoFormatting":"([^"]*)".*?"content":"([^"]*)"//){# those three data items are extrated, there are more$title=unescape($2);$desc=unescape($3);$url=unescape($1);# print resultif($use_color){printcolored['blue'],"$titlen";print"$descn";printcolored['green'],"$urlnn";printcolor'reset';}else{print"$titlen$descn$urlnn";}}}