Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Wide character in print

by PetreAdi (Novice)
on Jan 22, 2014 at 10:23 UTC ( #1071584=perlquestion: print w/ replies, xml ) Need Help??
PetreAdi has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to write a Perl script using the "utf8" . In my file have data


î ă

Ți ța

întâ să

use strict; use warnings; my $filename = 'data.txt'; open(my $fh, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!"; while (my $row = <$fh>) { chomp $row; print "$row\n"; }

I expect to get

î ă

Ți ța

întâ să

Comment on Wide character in print
Download Code
Re: Wide character in print
by choroba (Abbot) on Jan 22, 2014 at 10:27 UTC
    print without a file handle prints to STDOUT. You have to turn on the UTF-8 encoding for the STDOUT handle, as well as you correctly did for the input:
    binmode STDOUT, ':encoding(UTF-8)';

    Or, use open:

    use open OUT => ':encoding(UTF-8)', ':std';
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Alternatively, PetreAdi can set the character encoding of both input and output to UTF-8 in one fell swoop like this:

      use strict; use warnings; use open qw( :encoding(UTF-8) :std ); use autodie qw( open close ); my $filename = 'data.txt'; open my $fh, '<', $filename; while (my $row = <$fh>) { print $row; } close $fh; exit 0;

      I like to use autodie, too.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2014-09-20 03:32 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (152 votes), past polls