First, like all non-standard Perl modules, you will need to install Yahoo::Search from CPAN, and then you can include it at the top of your Perl programs with the use statement:
use Yahoo::Search;If you intend to use the Yahoo Search API regularly, you'll need to get yourself a Yahoo Developer Account, but luckily Yahoo provides us with a generic demo ID appropriately named YahooDemo. Once you're past that part, pulling search results from Yahoo is as easy as a single line:
my @results = Yahoo::Search->Results(Doc => "About Perl", AppId => "YahooDemo");Of course there are a ton of options, which are detailed on Yahoo's web site and in the Perl Module Documentation, but lets keep this simple and just look over a few. The Start and Count options let you pull the results in pages. Count tells Yahoo how many results to return at once, and Start determines the starting record (first result number in the set).
With that in mind, lets take a look at a simple script that pulls a few search results and then prints out some of the stuff that Yahoo sends back to us:
#!/usr/bin/perlSifting through the results, we can see that Url, somewhat predictably, has the link to the resource, and Title and Description contain the page's title and description meta information, respectively. Of course, there's a lot more you can do with a web search engine than just plastering the results on a page - with a handful of powerful API's like Yahoo's, there's no telling what you could do.
use Yahoo::Search;
my @results = Yahoo::Search->Results(
Doc => "About Perl",
AppId => "YahooDemo",
Start => 0,
Count => 3);
warn $@ if $@;
for my $result (@results) {
printf "Result: #%d\n", $result->I + 1,
printf "Url:%s\n", $result->Url;
printf "%s\n", $result->ClickUrl;
printf "Summary: %s\n", $result->Summary;
printf "Title: %s\n", $result->Title;
printf "In Cache: %s\n", $result->CacheUrl;
print "\n";
}
Module Links:
