Reputation: 306
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(my $fh, ">:utf8", "rpt-".$datestring.".csv")
or die "cannot open < rpt.csv: $!";
The characters are being written correctly inside the file but it doesn't appear to be including the UTF8 Byte Order Mark. This throws off my users, when they try 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.
Upvotes: 12
Views: 6493
Reputation: 6076
Is there a way to force the Byte Order Mark to be written?
To write this out, you must use File::BOM
to write the Byte Order Mark out when the file is opened.
For example, writing a little-endian UTF-16 file with BOM:
use File::BOM ();
my $filename = "out.bin";
open(FH, '>:encoding(UTF-8):via(File::BOM)', $filename);
print FH "ʇsǝ⊥\n";
Then run the program and check the output:
% file out.bin
out.bin: Unicode text, UTF-8 (with BOM) text
Prior to perl 5.8.7, there were bugs with wide characters.
Upvotes: 0