Quick Search for:  in language:    
POST,FORM,specialpurpose,simple,script,looks,
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Perl Stats

 Code: 74,273. lines
 Jobs: 25. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for Perl.
Click here to see a screenshot of this code!Mailing List v2.0
By Aaron L. Anderson on 1/7

(Screen Shot)

ShowIMG
By Jeff Mills on 1/5


Simple Perl Ping
By John Hass on 12/29


Very basic login script template with cookies
By Aaron L. Anderson on 12/29


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

Look up a word from Merriam-Webster site

Print
Email
 
VB icon
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

Users have accessed this code 6386 times.
 
 
     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-Webs
    =     ter site
    = Description:A special-purpose simple s
    =     cript 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 103 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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 AM:damian
LWP::Simple....
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/3/2002 8:32:54 PM:JTEP
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Perl Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.