in reply to Re: Why Doesn't Text::CSV_XS Print Valid UTF-8 Text When Used With the open Pragma?
in thread Why Doesn't Text::CSV_XS Print Valid UTF-8 Text When Used With the open Pragma?
Thank you very much, Tux! Thankfully, one can still get kind and gracious help with Perl problems from Perl experts on PerlMonks.
Upgrading to a bleadperl version of IO isn't practical for me in my environment. So for now, I'll simply not use the open pragma to set default I/O layers and, instead, open files and set I/O layers explicitly in my Perl programs, like this…
#!perl use strict; use warnings; use autodie qw( open close ); use English qw( -no_match_vars ); use Text::CSV_XS; my $csv = Text::CSV_XS->new({ always_quote => 1, binary => 1, eol => $INPUT_RECORD_SEPARATOR, }); binmode STDOUT, ':encoding(UTF-8)'; for my $file (@ARGV) { open my $fh, '<:encoding(UTF-8)', $file; while (my $fields = $csv->getline($fh)) { $csv->print(\*STDOUT, $fields); } $csv->eof() or $csv->error_diag(); close $fh; } exit 0;
Now if I could just figure out how best to handle UTF-8 CSV files that have byte order marks in them. ;-) Text::CSV_XS alone chokes on them. I'm currently doing this…
use File::BOM qw( open_bom ); open my $input_fh, '<:via(File::BOM)', $input_file; open my $output_fh, '>:encoding(UTF-8):via(File::BOM)', $output_file;
Is this The Right Way?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Why Doesn't Text::CSV_XS Print Valid UTF-8 Text When Used With the open Pragma?
by Tux (Canon) on Oct 03, 2011 at 06:37 UTC | |
by Jim (Curate) on Oct 03, 2011 at 14:16 UTC |