tags:

views:

296

answers:

2

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?

+2  A: 

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
codaddict
+3  A: 

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 Encode qw(encode_utf8);
open my $out, '>', $sectionfilename;
print {$out} encode_utf8 $section;
close $out;

Do not forget to add error-checking, e.g. use autodie.

daxim