[Moscow.pm] перекодировка

vividsnow vividsnow на gmail.com
Чт Сен 5 12:57:36 PDT 2013



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" <me на lelf.lu <mailto:me на lelf.lu>>:
>>>
>>> On Sep 5, 2013, at 11:14 PM, Nikolay Mishin <mi на ya.ru
>>> <mailto:mi на ya.ru>> 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" <ruz на bestpractical.com
>>>     <mailto:ruz на bestpractical.com>>:
>>>
>>>          ->decoded_content и use open OUT => ':encoding(cp1251)';
>>>
>>>          Тогда совсем можно не заморачиваться :)
>>>
>>>
>>>          2013/9/5 Anton Nikishaev <me на lelf.lu <mailto:me на lelf.lu>>
>>>
>>>          On Sep 5, 2013, at 10:16 PM, Nikolay Mishin <mi на ya.ru
>>>         <mailto:mi на ya.ru>> 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, "Олег Алексеенков" <proler на gmail.com
>>>             <mailto:proler на gmail.com>>:
>>>
>>>                  Nikolay Mishin <mi на ya.ru <mailto:mi на ya.ru>> писал(а)
>>>                 в своём письме 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
>>>                  moscow-pm на pm.org <mailto:moscow-pm на pm.org> |
>>>                 http://moscow.pm.org <http://moscow.pm.org/>
>>>
>>>              --
>>>              С уважением
>>>              Николай Мишин
>>>
>>>              --
>>>              Moscow.pm mailing list
>>>              moscow-pm на pm.org <mailto:moscow-pm на pm.org> |
>>>             http://moscow.pm.org <http://moscow.pm.org/>
>>>
>>>          --
>>>          Moscow.pm mailing list
>>>          moscow-pm на pm.org <mailto:moscow-pm на pm.org> |
>>>         http://moscow.pm.org <http://moscow.pm.org/>
>>>
>>>
>>>
>>>          --
>>>          Best regards, Ruslan.
>>>          ,
>>>          --
>>>          Moscow.pm mailing list
>>>          moscow-pm на pm.org <mailto:moscow-pm на pm.org> |
>>>         http://moscow.pm.org <http://moscow.pm.org/>
>>>
>>>      --
>>>      С уважением
>>>      Николай Мишин
>>>
>>>      --
>>>      Moscow.pm mailing list
>>>      moscow-pm на pm.org <mailto:moscow-pm на pm.org> |
>>>     http://moscow.pm.org <http://moscow.pm.org/>
>>>
>>>  
>>>
>>> ,
>>>
>>> -- 
>>> Moscow.pm mailing list
>>> moscow-pm на pm.org <mailto:moscow-pm на pm.org> | http://moscow.pm.org
>>> <http://moscow.pm.org/>
>>>
>>  
>>  
>> -- 
>> С уважением
>> Николай Мишин
>>  
>  
>  
> -- 
> С уважением
> Николай Мишин
>  
> 
> 


Подробная информация о списке рассылки Moscow-pm