Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force UTF-8 Byte Order Mark in Perl file output

Tags:

utf-8

perl

I'm writing out a CSV file using Perl. The data going into the CSV contains Unicode characters. I'm using the following to write the CSV out:

#OPEN THE FILE FOR WRITE
open(my $fh, ">:utf8", "rpt-".$datestring.".csv")
or die "cannot open < rpt.csv: $!";

That is writing the characters correctly inside the file but doesn't appear to be including the UTF8 Byte Order Mark. This in turn throws off my users trying to open the file in Excel. Is there a way to force the Byte Order Mark to be written?

I attempted it the following way:

print $fh "\x{EFBBBF};

I ended up with gibberish at the top of the file. Any help would be greatly appreciated.

like image 962
Carl Bullard Avatar asked Sep 14 '11 15:09

Carl Bullard


1 Answers

Try doing this:

print $fh chr(65279);

after opening the file.

like image 179
ErikR Avatar answered Nov 11 '22 00:11

ErikR