Skip to Content
Perl for Web Site Management
book

Perl for Web Site Management

by John Callender
October 2001
Beginner
528 pages
15h 20m
English
O'Reilly Media, Inc.
Content preview from Perl for Web Site Management

A Single-Search Results Tool

To give you an idea of how the WWW::Search module works, we’ll start with a simple script that runs from the command line. This script will let us use arguments to specify which search engine to search, what query to submit to it, and so on. The script, called search_rank.plx, is in Example 18-1.

Example 18-1. Querying search engines from the command line with WWW::Search

#!/usr/bin/perl -w

# search_rank.plx

# using the WWW::Search module, compute the rank of the highest-ranked
# page for a particular site when searching a particular search
# engine for a particular query string.

use strict;

use WWW::Search;
use Getopt::Std;

my %opt;

getopts('s:u:q:m:', \%opt);

unless ($opt{s} and $opt{u} and $opt{q}) {

    die <<"EOF";
Usage: $0 [options]

Required options: -s search_engine
                  -u base_url
                  -q 'search query' 

Optional options: -m max_#_to_retrieve (defaults to 50)

EOF

}

my $max = $opt{m} || 50;

my $search = new WWW::Search($opt{s});
$search->maximum_to_retrieve($max);

my $base_url = quotemeta($opt{u});
my $rank     = 0;
my $count    = 1;

$search->native_query(WWW::Search::escape_query($opt{q}));
while (my $result = $search->next_result(  )) {
    if (not $rank and $result->url =~ /$base_url/o) {
        $rank = $count;
    }
    print "$count: ", $result->title || $result->url,
        ', ', $result->url, "\n";
    ++$count;
}

print "Rank: $rank\n";

Using the Getopt::Std Module

As you scan through this script, the first interesting thing you’ll notice is the use of the Getopt::Std module. This is a standard ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Web Client Programming with Perl

Web Client Programming with Perl

Clinton Wong
Embedding Perl in HTML with Mason

Embedding Perl in HTML with Mason

Ken Williams, Dave Rolsky

Publisher Resources

ISBN: 1565926471Catalog PageErrata