1: #!/usr/bin/perl
   2: use strict;
   3: 
   4: use LWP::Simple;
   5: use HTML::Parse;
   6: use HTML::FormatText;
   7: use URI::Escape;
   8: 
   9: my ($word, $dict, $url, $html, $ascii);
  10: unless(defined $ARGV[0]){
  11:         print "usage: \n";
  12:         print "dictionary:      lookup.pl <word> | '<phrase>'\n";
  13:         print "thesaurus:       lookup.pl -syn <word> | '<phrase>'\n";
  14:         print "word du jour:    lookup.pl -wod\n";
  15:         print "random word:     lookup.pl -rand [list]\n";
  16: 
  17:         exit 1;
  18: }
  19: 
  20: if ($ARGV[0] eq "-wod"){
  21:         $url = "http://www.dictionary.com/wordoftheday/";
  22: }elsif($ARGV[0] eq "-syn"){
  23:         $word = uri_escape($ARGV[1]);
  24:         $url = "http://www.thesaurus.com/cgi-bin/search?config=roget&words=$word";
  25: }elsif($ARGV[0] eq "-rand"){
  26:         if (defined $ARGV[1]){ $dict = $ARGV[1]}else{$dict = "/usr/dict/words"};
  27:         open (WORDS, $dict);
  28:         srand;
  29:         rand($.) < 1 && ($word = $_) while <WORDS>;
  30:         $word = uri_escape($word);
  31:         $url = "http://www.dictionary.com/cgi-bin/dict.pl?term=$word";
  32: }else{
  33:         $word = uri_escape($ARGV[0]);
  34:         $url = "http://www.dictionary.com/cgi-bin/dict.pl?term=$word";
  35: }
  36: 
  37: print "getting it...\n";
  38: $html = get($url);
  39: defined $html or die "Can't lookup $ARGV[0] from dictionary.com\n";
  40: print "got it.\n";
  41: $ascii = HTML::FormatText->new->format(parse_html($html));
  42: print "converted it...\n";
  43: print $ascii;
  44: print $url, "\n";

Replies are listed 'Best First'.
Home-made URI escapes considered harmful
by chip (Curate) on Nov 13, 2001 at 10:44 UTC
    Why substitute only %20 for spaces when there are so many other characters illegal in query strings?

    Gisle was kind enough to provide you URI::Escape. Please be kind enough to use it.

        -- Chip Salzenberg, Free-Floating Agent of Chaos

      ok, I did. Thanks for the tip.

      d$hahin

Re: lookup.pl - a command line dictionary/thesaurus
by strfry() (Monk) on Oct 23, 2001 at 00:50 UTC