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?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.