WorkingOnBeingBetter
WorkingOnBeingBetter

Reputation: 241

setting BOM to Unicode U code UTF8 perl

This question is similar to others that have been posted before. however trying all combinations nothing is working.

I need to have my excel file read in Unicode Utf8, I am attempting to set my bom:

my $csv = Text::CSV->new ({binary=>1, eol =>$/}) or die "cannot use CSV: ".Text::CSV->error_diag (); open my $csvFile, ">:encoding(UTF-8)", "teht.csv" or die "teht.csv: $!"; print($csvFile "\x{FEBBBF}");

however this gets an errror and says that "0xFEBBBF is not Unicode..."

all information that I have found indicates that the code for utf8 should read print($csvFile "\N{U+FEBBBF}") or ... "\xFE\xBB\xBF" or similar.

Is it possible to force Excel recognize UTF-8 CSV files automatically? is one source which says this many times.

https://stackoverflow.com/a/22711105/6557829 is another source.

So far I have actually been able to get UTF-16 to work with the same print statement: print($csvFile "\N{U+FEFF}"); however that is more space than I mean to use. Thanks in advance for any help you can give me.

Upvotes: 0

Views: 347

Answers (1)

ikegami
ikegami

Reputation: 385590

The BOM is U+FEFF, not U+FEBBBF. Replace

"\x{FEBBBF}"

with any one of following:

chr(0xFEFF)
"\x{FEFF}"
"\N{U+FEFF}"
"\N{BOM}"

This will create a string with a single character (FEFF), which print will encode using UTF-8 as requested (EF BB BF).

Upvotes: 2

Related Questions