Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Problems w/ encoding in terminal

by humble (Acolyte)
on Aug 06, 2013 at 11:39 UTC ( #1048097=perlquestion: print w/ replies, xml ) Need Help??
humble has asked for the wisdom of the Perl Monks concerning the following question:

Good time of the day, people.

I'm localizing perl script (from english to russian) and have problems w/ encoding when script prints to terminal (encoding utf-8).

I made a module (encoding utf-8) where is my collection of variables, now i translate it. Then i transfer those variables to the script through qw().

Problem is that after direct printing to terminal those variables give me unreadable characters, like ив.

I have tried to put

use Encode; binmode STDOUT, ":encoding(UTF-8)";

but it did not help me.

So, i have to decode every variable before use unless i do want to see the garbage:

$out=decode( "UTF-8", $out);

My questions are:

1. Is there an easier way to output variables w/o need of decoding every variable?

2. Why i having variables' contetnt written in utf8 and terminal in utf8 - can not simply print it so it might be readable in russian?

3. May you have other (simpler) suggestions regarding localization of scripts?

Thanks for any advance.

Comment on Problems w/ encoding in terminal
Select or Download Code
Replies are listed 'Best First'.
Re: Problems w/ encoding in terminal
by rjt (Deacon) on Aug 06, 2013 at 12:04 UTC

    Provided you save your localization module with utf8 encoding, just include use utf8; in that module. See perlunicode, and Tom Christiansen's most excellent Stackoverflow answer on unicode. Here's an example you can work from:

    use Encode; binmode STDOUT, ':encoding(UTF-8)'; require ''; my $en = 'Hello'; printf "English: %s, Russian: %s\n", $en, MyPackage::ru::translate($en);


    English: Hello, Russian: привет

    If you run this exact code with encoded in utf8, and do not see the expected Cyrillic script, check your terminal settings and/or try another. Also beware if you are piping the output through other programs that are not aware of the encoding.

    package MyPackage::ru; use utf8; # Tells Perl to interpret this source file as utf8 use Carp; # Very crude translation table my %trans = ( # XXX - Note actual code contains the literal utf8 characters, # not any kind of escapes. PerlMonks insists on converti +ng # them to HTML character entities. hello => 'привет', ); sub translate { $trans{ lc $_[0] } || croak 'No translation for '. +$_[0] }

    As a (sort-of) aside to your question, however, you might want to check out Locale::Maketext to save yourself the work of re-inventing a localization module.

    use strict; use warnings; omitted for brevity.
      Thank you ver much! Very insteresting idea! I will consider how i can use the modules in my future work.
Re: Problems w/ encoding in terminal
by choroba (Chancellor) on Aug 06, 2013 at 11:43 UTC
    What encoding do you use to save the script? If you save it as utf-8 and tell Perl you did so by
    use utf8;
    everything should just work (i.e. no Encode, output set to utf-8, terminal set to utf-8).
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      What would you advice me to correctly output a file content from the same my script? - Again, the file content is in UTF8, but in UTF8 terminal it is not readable since outputed from my script. Do i have to decode the file content every time i want to output it using 'Encode' module?
        Just tell Perl what encoding the file uses. There are several ways:

        When opening the file

        open my $OUT, '>:utf8', $filename or die $!;

        Or, after opening the file:

        open my $OUT, '>', $filename or die $!; binmode $OUT, ':utf8';

        Or, if you want all your output be in UTF-8:

        use open OUT => ':utf8';

        See open, open, binmode for details. You can also use ':encoding(UTF-8)' instead of ':utf8' to let Perl check the validity of the data.

        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thank you very much: worked and so i will be watching for the directive to use in the future!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1048097]
Front-paged by rjt
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2016-05-25 14:38 GMT
Find Nodes?
    Voting Booth?