MrDanA
MrDanA

Reputation: 11647

How to properly print non-English characters to a file with Perl?

I am using Perl to print some data read from one file to another. Sometimes I read in non-English characters, such as accented characters like é. However, doing:

print FILE_HANDLER "... $variable ...";

does not keep the accents. The é actually gets printed out as "é".

How can I print these characters out so that they're properly preserved? For more information, the files that I open and write to are done as such:

open READ_FILE, "<", "file.xml" or die $!;
open WRITE_FILE, ">", "file.txt" or die $!;

Thanks for all your help.

Upvotes: 2

Views: 490

Answers (1)

Cornel Ghiban
Cornel Ghiban

Reputation: 902

perldoc -f open says:

You may (and usually should) use the three-argument form of open to specify I/O layers (sometimes referred to as "disciplines") to apply to the handle that affect how the input and output are processed (see open and PerlIO for more details). For example:

 open(my $fh, "<:encoding(UTF-8)", "filename")
 || die "can't open UTF-8 encoded filename: $!";

opens the UTF8-encoded file containing Unicode characters; see perluniintro

Upvotes: 7

Related Questions