senik148 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks,

im having a small issue with a parameter on the module WWW::Babelfish.

here is the code:
use WWW::Babelfish; $obj = new WWW::Babelfish( service => 'Google', agent => 'Mozilla/8.0' +); die( "Google server unavailable\n" ) unless defined($obj); $en_text = $obj->translate( 'source' => 'English', 'destination' => 'Spanish', 'text' => "i am mexican with black h +air and brown eyes", 'delimiter' => "\n\t", 'ofh' => \*STDOUT); die("Could not translate: " . $obj->error) unless defined($en_text); @languages = $obj->languages;


As i read on CPAN, the parameter 'ofh'
ofh: Output filehandle; if provided, the translation will be written to this filehandle.

ofh parameter must be a filehandle. what kind of filehandle?? <FH>, how many more are they STDOUT.. etc??

my problem is i want to pass the data output to an @array

please help!

Replies are listed 'Best First'.
Re: Google Translation using WWW::Babelfish
by sk (Curate) on Jan 25, 2006 at 04:07 UTC
    I ran your code and I get this output

     soy mejicano con el pelo negro y los ojos marrones

    If you want to store the output in a file then you can do something like this -

    open (FH, '>', "junk") or die $!; $en_text = $obj->translate( 'source' => 'English', 'destination' => 'Spanish', 'text' => "i am mexican with black hair an +d brown eyes", 'delimiter' => "\n\n", 'ofh' => \*FH);
    This will write the translation into a file.

    If you do not want that then just remove the 'ofh' from the call. When i do that it returns the text and it is stored in $en_text. You can just print it or you can then push this text into an @array of your choice.

    Something like this ? -

    #!/usr/bin/perl use strict; use warnings; use WWW::Babelfish; use Data::Dumper; # open (FH, '>', "junk") or die $!; my $obj = new WWW::Babelfish( service => 'Google', agent => 'Mozilla/ +8.0'); die( "Babelfish server unavailable\n" ) unless defined($obj); my @sentences = ("I would like to translate this sentence into Spanis +h", "Because i am mexican with black hair and brown eyes"); my @translations = (); for (@sentences) { my $en_text = $obj->translate( 'source' => 'English', 'destination' => 'Spanish', 'text' => $_, 'delimiter' => "\n\n"); die("Could not translate: " . $obj->error) unless defined($en_text +); push(@translations,$en_text); } print Dumper (@translations);

    Output

    $VAR1 = "Quisiera traducir esta oraci\x{f3}n a espa\x{f1}ol"; $VAR2 = 'Porque soy mejicano con el pelo negro y los ojos marrones';

    -SK

      yeah your code works, i did not know it will be stored in $en_text if you romve the ofh parameter.
      but again both code examples are good, i stick more with the last example because i want to output the translation maybe on a web server running it as a cgi. and i dont wanna store it on a file. etc.

      hey but thanx!