Jerry Kassebaum wrote:
> Friends,
>
> Below is code from "Perl and LWP" by Sean Burke. Run it from the command
> line with " -gk It seems that it's going to be a fine day." as arguments.
> The goal is to translate to Greek and back. Doing so manually at the site it
> uses yields "It appears that it is to be one thin day." Apparently the site
> has changed since the book came out, so it doesn't work.
>
> I have several questions.
>
> What is "require 5"???
Make sure we're Perl5 or greater.
> On about line 12 I added, "#print "XXX"; <>;" When you take off the comment
> marker how does that mess up the command line arguments???
Try: print "XXX"; <STDIN>;
> What needs to be changed to make it work?
#!perl --
use strict;
use warnings;
use LWP;
use Data::Dumper; $Data::Dumper::Indent=1; $Data::Dumper::Sortkeys=1;
$| = 1; binmode STDOUT; select ((select (STDERR), $| = 1)[0]); binmode STDERR;
our %A; # get commandline switches into %A
for (my $ii = 0; $ii < @ARGV; ) {
if ($ARGV[$ii] =~ /^--$/) { splice @ARGV, $ii, 1; last; }
if ($ARGV[$ii] !~ /^-{1,2}(.*)$/) { $ii++; next; }
my $arg = $1; splice @ARGV, $ii, 1;
if ($arg =~ /^([\w]+)=(.*)$/) { exists ($A{$1}) ? ($A{$1} .= "|$2") :
($A{$1} = $2); } else { $A{$1}++; }
}
my %languages = (it => 'Italian', fr => 'French', de => 'German',
es => 'Spanish', ja => 'Japanese', pt => 'Portuguese', );
my @languages = sort keys %languages;
my $languages = '';
$languages .= "\t\t\t$_\t$languages{$_}\n" foreach @languages;
my $debug = $A{d} || 0; print Data::Dumper->Dump([\%A], [qw(\%A)]) if $debug;
(my $prog = $0) =~ s/^.*[\\\/]//;
my $usage = <<EOD;
Usage: $prog [-d] <langs> <text-to-translate>
-d debug
<langs> from and to languages (EG: enfr [english->french])
$languages
<text-to-translate> Text that needs translating
EOD
die $usage if $A{h} or $A{help} or @ARGV < 2;
my $from = 'en';
my $to = 'fr';
my $lang = "$from$to";
if (@ARGV and $ARGV[0] =~ m/^(\w\w)(\w\w)$/) {
$from = lc $1;
$to = lc $2;
$lang = "$from$to";
shift @ARGV;
}
my $rev_lang = "$to$from";
die "What to translate?\n" unless @ARGV;
my $in = join ' ', @ARGV;
my $t1 = translate ($in, $lang);
my $t2 = translate ($t1, $rev_lang);
printf "\nOrig: %s\n%-4s: %s\nback: %s\n\n", $in, $to, $t1, $t2;
exit;
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub translate {
my ($text, $language_path) = @_;
my ($content, $message, $is_success) = do_POST(
'http://babelfish.altavista.com/translate.dyn', ['urltext' => $text,
'lp' => $language_path, 'enc' => 'utf8']);
die "Error in translation $language_path: $message\n" unless $is_success;
# brute force the extract for now
if ($content =~
/<!--\s*target text.*?-->(.*?)<!--\s*end:\s*target text.*?-->/is) {
my $translation = $1;
my @trs = $translation =~ /<tr>(.*?)<\/tr>/gis;
# 2nd tr in target text is our reply
$trs[1] =~ s/<[^>]*>//g;
# Trim whitespace:
$trs[1] =~ s/\s+/ /g;
$trs[1] =~ s/^ //s;
$trs[1] =~ s/ $//s;
return $trs[1];
} else {
die "Can't find translation in response to $language_path";
}
}
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Parameters:
# the URL,
# an arrayref or hashref for the key/value pairs,
# and then, optionally, any header lines: (key,value, key,value)
sub do_POST {
my $browser = LWP::UserAgent->new(); # unless $browser;
my $resp = $browser->post(@_);
return ($resp->content, $resp->status_line, $resp->is_success, $resp) if
wantarray;
return $resp->content;
}
__END__
> C:\book\code\ch02>perl alienate.pl "It was more fun than a barrel of
> monkeys"
> => via it => It was more divertimento that a keg of the monkeys
>
> C:\book\code\ch02>perl alienate.pl -de "Guess what! I'm a computer!"
> => via de => Assumption which! I am a computer!
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs