Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Worked like a charm. Thank-you

Glad I could help.

BUT:

Until Tux' change is released on CPAN, please do yourself a favour and add an explaining comment how that trick works. This trick is everything but obvious, and I doubt that it will work for any combination of the relevant modules. Don't make your future self invent a time machine to come back and hurt you for playing dirty tricks.

When the change is released, test it, get rid of my dirty trick, and explicitly require the new version of DBD::CSV.

If your code has to work with "any" version of DBD::CSV, check its version, prefer to use the Tux' official csv_bom attribute, and only if it is not supported, fall back to use the File::BOM trick. Something like this:

use DBD::CSV (); use if $DBD::CSV::VERSION < 0.50, File::BOM => (); # ^---- adjust to working version number my $dbh=DBI->connect( 'dbi:CSV:', undef, undef, { RaiseError => 1, PrintError => 0, f_ext => '.csv', ( # v---- adjust to working version numbe +r $DBD::CSV::VERSION < 0.50 ? (f_encoding => 'utf-8):via(File::BOM') : (csv_bom => 1) ) } );

Alternatively, patch DBD::CSV at runtime if it is too old, and use Tux' official attribute csv_bom even with old versions:

#!/usr/bin/perl use v5.12; use warnings; use autodie qw( :all ); use DBI; use DBD::CSV (); # v----- adjust to working version if ($DBD::CSV::VERSION < 0.50) { require File::BOM; my $old_connect=\&DBD::CSV::dr::connect; my $new_connect=sub { my ($drh, $dbname, $user, $auth, $attr) = @_; if ($attr && exists($attr->{'csv_bom'}) && $attr->{'cs +v_bom'}) { delete $attr->{'csv_bom'}; $attr->{'f_encoding'}='utf-8):via(File::BOM'; } goto $old_connect; }; do { no strict 'refs'; no warnings 'redefine'; *DBD::CSV::dr::connect=$new_connect; }; } open my $h,'>:encoding(utf-8)','test.csv'; say $h qq<\x{FEFF}"foo","bar","baz">; say $h qq<"1","2","3">; say $h qq<"4","5","6">; close $h; my $dbh=DBI->connect( 'dbi:CSV:', undef, undef, { RaiseError => 1, PrintError => 0, f_ext => '.csv', csv_bom => 1, } ); my $sth=$dbh->prepare('select * from test'); $sth->execute(); while (my @a=$sth->fetchrow_array()) { say join(",",@a); } $sth->finish();

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re^3: DBD::CSV - how to I coax it to read BOM prefixed files? by afoken
in thread DBD::CSV - how to I coax it to read BOM prefixed files? by ELISHEVA

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (5)
    As of 2020-07-10 10:38 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?