Important alert: (current site time 7/15/2013 9:37:01 PM EDT)
 

VB icon

Look up a word from Merriam-Webster site

Email
Submitted on: 7/30/2000 12:28:12 AM
By: Found on the World Wide Web 
Level: Intermediate
User Rating: By 3 Users
Compatibility: 5.0 (all versions), 4.0 (all versions), 3.0 (all versions), Pre 3.0
Views: 15245
 
     A special-purpose simple script that looks up a word from Merriam-Webster site. This script only uses Socket and no other external modules or packages, and it demonstrates the use of POST method to submit a FORM. However, the specific use of this script is limited to talking to www.m-w.com, and the fact that many parameters are hard-coded makes it dependent on the stability of that web site. Nonetheless, since everything is explicitly written, it is very easy to manually change those hard-coded strings
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
=**************************************
= Name: Look up a word from Merriam-Webster site
= Description:A special-purpose simple script that looks up a word from Merriam-Webster site.
This script only uses Socket and no other external modules or packages, and it 
demonstrates the use of POST method to submit a FORM. However, the specific use 
of this script is limited to talking to www.m-w.com, and the fact that many 
parameters are hard-coded makes it dependent on the stability of that web site. 
Nonetheless, since everything is explicitly written, it is very easy to manually
change those hard-coded strings
= By: Found on the World Wide Web
=**************************************

#!/usr/bin/perl -w
use Socket;
use strict;
my $word = $ARGV[0] or die "Usage: mw word\n";
my $host = "www.m-w.com";
my $port = 80;
my $socketaddr;
my $content = "jump=$word"; # This seems to work with white space in word
my $wholePage = "";
my $form = "";
my $buf = "";
my @listvalue = ();
my @option = ();
my $selections = 0;
my $count = 0;
while ($content) {
openSock();
post ($content);
$wholePage = "";
while ( <SOCK> ) {
	$wholePage .= $_;
}
close SOCK;
$wholePage =~ /(<form .*<\/form>)/gs;
$form = $1 or die "Can not find the word\n";
# this is heavy duty kludge, geared toward www.m-w.com, needs maintenance
# find out if the form has a selection of options
$selections = 0;
if ($form =~ s/^To view.*?GO TO.$//m) {
	$selections = 1;
	@option = ($form =~ /^<option.*>(.*)$/mg);
	@listvalue = ($form =~ /name=list value="(.*)">/g);
}
# convert html into something more readable
$form =~ s/<br>/\n/g;# change html linebreak to newline
$form =~ s/<option.*?\n//mg;# delete the selection list, to be shown later
$form =~ s/<[^>]*>//g; # delete all the other html tags
$form =~ s/>/>/g;# make visible the greater-than sign
$form =~ s/</</g;# make visible the less-than sign
$form =~ s/&/&/g;# make visible the less-than sign
$form =~ s/\n+/\n/g;# delete multiple newlines
print $form;
print "\n";
# prompt the user for further actions: look up another word or stop here
$content = "";
if ($selections) {
	print "Here are the related words:\n";
	for (my $i=0;$i<@option;$i++){
		print "$i: $option[$i]\n";
	}
	print "\nEnter a number to select from the list, or enter . to quit\n";
	$buf = <STDIN>; # don't know how to use "read"
	chomp $buf;
	if ($buf eq '.') {
		$content = "";
	}
	elsif ($buf !~ /\d/ or $buf >= @option) {
		print "What did you just do?\n";
		$content = "";
	}
	else{
		$content = "hdwd=$word&book=Dictionary&jump=";
		$content .= urlencode ($option[$buf]);
		$content .= "&list=";
		$content .= urlencode ($listvalue[0]);
	}
} # end of if selections
} # end of while content
###########
# subroutine: open a socket at SOCK
###########
sub openSock {
$socketaddr= sockaddr_in $port, inet_aton $host or die "Bad hostname\n";
socket SOCK, PF_INET, SOCK_STREAM, getprotobyname('tcp') or die "Bad socket\n";
connect SOCK, $socketaddr or die "Bad connection\n";
select((select(SOCK), $| = 1)[0]);
}
###########
# subroutine: urlencode a string
###########
sub urlencode {
my $ask = shift @_;
my @a2 = unpack "C*", $ask;
my $s2 = "";
while (@a2) {
$s2 .= sprintf "%%%X", shift @a2;
}
return $s2;
}
###########
# subroutine: send post request to target web site
###########
sub post {
my $content = shift @_;
print SOCK "POST http://www.m-w.com/cgi-bin/dictionary HTTP/1.0\n";
print SOCK "Content-type: application/x-www-form-urlencoded\n";
my $contentLength = length $content;
print SOCK "Content-length: $contentLength\n";
print SOCK "\n";
print SOCK "$content";
}
=head1 NAME
Save this file to "mw", which stands for merriam-webster, then you can run it as
"mw word" or "perl mw word"
=head1 DESCRIPTION
a simple web robot to look up a word from Merriam-Webster site using POST 
method, and print the text response to STDOUT. 
=head1 README
A special-purpose simple script that looks up a word from Merriam-Webster site.
This script only uses Socket and no other external modules or packages, and it 
demonstrates the use of POST method to submit a FORM. However, the specific use 
of this script is limited to talking to www.m-w.com, and the fact that many 
parameters are hard-coded makes it dependent on the stability of that web site. 
Nonetheless, since everything is explicitly written, it is very easy to manually
change those hard-coded strings
=head1 PREREQUISITES
requires strict module and Socket module
=head1 SCRIPT CATEGORIES
Web
=cut


Other 100 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
6/19/2001 12:23:22 AMdamian

LWP::Simple....
(If this comment was disrespectful, please report it.)

 
1/3/2002 8:32:54 PMJTEP

This is an excellent script!!!!!!!

It is very well written and helps me in solving the problem of reading contents from a URL.

Thank you very much for your submission of this script.

Thank you, again.

Kind Regards,
Jason Tepoorten
(If this comment was disrespectful, please report it.)

 
5/2/2005 9:10:26 PMCrotts

Can someone please help me figure out how to impliment this feature in my search engine? I want to display the results to $keywords queried by web surfers. I have no idea where to start with this code. Do i create my own search form?
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.