Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I save a file as UTF-8 from Perl?

Tags:

utf-8

perl

I'm trying to create/save HTML files in Perl in UTF-8, but nothing I have done so far works. A previous answer here on SO said to use binmode, so I tried that. Here is my code:

open (OUT, ">$sectionfilename");
binmode(OUT, ":utf8");
print OUT $section;
close OUT;

When I open these files in a text editor like Notepad they are still in ANSI encoding. What am I doing wrong?

like image 545
Joshua Avatar asked Feb 26 '10 05:02

Joshua


2 Answers

A text editor is a poor tool to examine low-level things such as encodings. Use a hexviewer/hexdumper instead. The modern way to write your example:

use autodie qw(:all);
open my $out, '>:encoding(UTF-8)', $sectionfilename;
print {$out} $section;
close $out;

autodie enables automatic error-checking.

like image 112
daxim Avatar answered Oct 24 '22 22:10

daxim


Seems to work for me:

C:\Documents and Settings>cat a.pl
$sectionfilename = "a.txt";
$section = "Hello \x{263A}!\n";

open (OUT, ">$sectionfilename");
binmode(OUT, ":utf8");
print OUT $section;
close OUT;    

C:\Documents and Settings>perl a.pl

C:\Documents and Settings>file a.txt
a.txt: UTF-8 Unicode text, with CRLF line terminators

But when I change the text to be written to:

$section = "Hello";

and run:

C:\Documents and Settings>perl a.pl

C:\Documents and Settings>file a.txt
a.txt: ASCII text, with no line terminators
like image 26
codaddict Avatar answered Oct 24 '22 21:10

codaddict