On 09/05/2013 11:47 PM, Nikolay Mishin wrote:
> Антон, Руслан, спасибо
> итог
> https://github.com/mishin/gists/blob/master/gtrans2.pl
>  
> #use utf8;
> #use open qw<:std>;
> use open OUT => ':encoding(cp1251)';
>  
>  
> &main;
> exit;
>  
> sub main {
>     translate_text( $from, $to, $text );
> }
>  
> sub translate_text {
>     my ( $from, $to, $words ) = @_;
>  
>     my $url =
>         'http://translate.google.com/translate_t?langpair='
>       . $from . '|'
>       . $to
>       . '&text=' . '+'
>       . $words;
>     my $ua = LWP::UserAgent->new;
>     $ua->agent('');
>     my $res = $ua->get($url);
>     die $res->status_line if $res->is_error;
>     my $html = $res->decoded_content;
>  
>     my @matches =
>       $html =~
> m{onmouseout="this.style.backgroundColor='#fff'">(.*?)</span>}g;
>  
>     foreach my $translated_string (@matches) {
>  
>         # my $flag = utf8::is_utf8($translated_string);
>         # say "[ $flag ]"; $flag==1
>         utf8::encode($translated_string);
>         say $translated_string;
>     }
> }
>  
> меня удивило, что можно закомментировать use utf8, видимо он где-то есть еще
> 

perldoc utf8
...
Utility functions
The following functions are defined in the "utf8::" package by the Perl
core. You do not need to say "use utf8" to use these and in fact you
should not say that unless you really want to have UTF-8 source code.
...

>  
>>  
>> 05.09.2013, 23:24, "Anton Nikishaev" <[email protected] <mailto:[email protected]>>:
>>>
>>> On Sep 5, 2013, at 11:14 PM, Nikolay Mishin <[email protected]
>>> <mailto:[email protected]>> wrote:
>>>
>>>      Руслан, круто, так  работает
>>>
>>>      use utf8;
>>>      use open OUT => ':encoding(cp1251)';
>>>
>>> +use open qw<:std>;
>>>
>>>      sub translate_text {
>>>          my ( $from, $to, $words ) = @_;
>>>
>>>          my $url =
>>>              'http://translate.google.com/translate_t?langpair='
>>>            . $from . '|'
>>>            . $to
>>>            . '&text=' . '+'
>>>            . $words;
>>>          my $ua = LWP::UserAgent->new;
>>>          $ua->agent('');
>>>          my $res = $ua->get($url);
>>>          die $res->status_line if $res->is_error;
>>>          my $html = $res->decoded_content;
>>>
>>>          my @matches =
>>>            $html =~
>>>     m{onmouseout="this.style.backgroundColor='#fff'">(.*?)</span>}g;
>>>
>>>          foreach my $translated_string (@matches) {
>>>              say $translated_string;
>>>          }
>>>      }
>>>
>>>      https://github.com/mishin/gists/blob/master/gtrans2.pl
>>>      но
>>>      $./gtrans2.pl --from en --to ru --text "This is a test"
>>>      Wide character in say at ./gtrans2.pl line 63.
>>>      Это тест
>>>
>>>      05.09.2013, 22:50, "Ruslan Zakirov" <[email protected]
>>>     <mailto:[email protected]>>:
>>>
>>>          ->decoded_content и use open OUT => ':encoding(cp1251)';
>>>
>>>          Тогда совсем можно не заморачиваться :)
>>>
>>>
>>>          2013/9/5 Anton Nikishaev <[email protected] <mailto:[email protected]>>
>>>
>>>          On Sep 5, 2013, at 10:16 PM, Nikolay Mishin <[email protected]
>>>         <mailto:[email protected]>> wrote:
>>>
>>>               Encode::from_to( $translated_string, 'koi8', 'cp1251' );
>>>
>>>              $perl gtrans.pl --from=en --to=ru --text='This is a test'
>>>              Unknown encoding 'KOI8' at gtrans.pl line 63
>>>
>>>          Она называется koi8-r
>>>
>>>              05.09.2013, 20:01, "Олег Алексеенков" <[email protected]
>>>             <mailto:[email protected]>>:
>>>
>>>                  Nikolay Mishin <[email protected] <mailto:[email protected]>> писал(а)
>>>                 в своём письме Thu, 05 Sep 2013
>>>                  08:38:50 +0400:
>>>
>>>                       вопрос, можно ли строку
>>>                       Convert::Cyrillic::cstocs( 'KOI8', 'UTF8',
>>>                     $translated_string );
>>>                       Encode::from_to( $interm_var, 'utf-8', 'cp1251' );
>>>                       заменить одним преобразованием, чтобы на выходе
>>>                     был win1251?
>>>                       у меня этого не получилось
>>>
>>>                  а почему не работает
>>>                  Encode::from_to( $interm_var, 'koi8', 'cp1251' );
>>>                  ?
>>>                  --
>>>                  Moscow.pm mailing list
>>>                  [email protected] <mailto:[email protected]> |
>>>                 http://moscow.pm.org <http://moscow.pm.org/>
>>>
>>>              --
>>>              С уважением
>>>              Николай Мишин
>>>
>>>              --
>>>              Moscow.pm mailing list
>>>              [email protected] <mailto:[email protected]> |
>>>             http://moscow.pm.org <http://moscow.pm.org/>
>>>
>>>          --
>>>          Moscow.pm mailing list
>>>          [email protected] <mailto:[email protected]> |
>>>         http://moscow.pm.org <http://moscow.pm.org/>
>>>
>>>
>>>
>>>          --
>>>          Best regards, Ruslan.
>>>          ,
>>>          --
>>>          Moscow.pm mailing list
>>>          [email protected] <mailto:[email protected]> |
>>>         http://moscow.pm.org <http://moscow.pm.org/>
>>>
>>>      --
>>>      С уважением
>>>      Николай Мишин
>>>
>>>      --
>>>      Moscow.pm mailing list
>>>      [email protected] <mailto:[email protected]> |
>>>     http://moscow.pm.org <http://moscow.pm.org/>
>>>
>>>  
>>>
>>> ,
>>>
>>> -- 
>>> Moscow.pm mailing list
>>> [email protected] <mailto:[email protected]> | http://moscow.pm.org
>>> <http://moscow.pm.org/>
>>>
>>  
>>  
>> -- 
>> С уважением
>> Николай Мишин
>>  
>  
>  
> -- 
> С уважением
> Николай Мишин
>  
> 
> 
-- 
Moscow.pm mailing list
[email protected] | http://moscow.pm.org

Ответить