#!/usr/bin/perl -w # # dict - fetches a definition from www.dict.org # # Copyright (C) 2000 Steven Pritchard # This program is free software; you can redistribute it # and/or modify it under the same terms as Perl itself. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. use strict; use LWP::UserAgent; use HTML::TreeBuilder; use HTML::FormatText; my ($VERSION)=q( $Id: dict,v 1.3 2000/07/24 19:43:18 steve Exp $ )=~/ (\d+\.\d+) /; die "no search string specified\n" if (!@ARGV); my $ua=new LWP::UserAgent; $ua->agent("dict-perl/$VERSION " . $ua->agent); $ua->env_proxy; # Get the proxy settings from %ENV. my $url="http://www.dict.org/bin/Dict?Form=Dict1&Strategy=*&Database=*&Query=" . url_encode("@ARGV"); my $request=new HTTP::Request("GET", $url); my $response=$ua->request($request); if ($response->is_success) { my $tree=HTML::TreeBuilder->new; $tree->parse($response->content); $tree->eof; my $formatter=HTML::FormatText->new(leftmargin => 0, rightmargin => 70); my $out=$formatter->format($tree); $out=~s/\[[A-Z]+\s+NOT\s+SHOWN\]\n//g; print $out; } else { die "failed to get '$url': ", $response->status_line, "\n"; } sub url_encode { my $foo=shift; $foo=~s/(\W)/sprintf "%%%02x", ord($1)/eg; return $foo; }