perl5-dbi / DBD-CSV

DBD::CSV - DBI driver for CSV files
11 stars 6 forks source link

Can't call method "print" on an undefined value #8

Closed ufobat closed 3 years ago

ufobat commented 3 years ago

Hi everyone, I've taken the example from the SYNOPSIS and added a line with the content of csv_class => 'Text::CSV_XS' to it:

use strict;
use warnings;
use DBI;

# See "Creating database handle" below
my $dbh = DBI->connect("dbi:CSV:", undef, undef, {
    f_ext => ".csv/r",  RaiseError => 1,
    csv_class => 'Text::CSV_XS'
}) or die "Cannot connect: $DBI::errstr";
$dbh->do ("CREATE TABLE foo (id INTEGER, name CHAR (10))");

Though I expected to change nothing, because Text::CSV_XS should be the default value, my programm now fails. With the help of Carp::Always I got this error stacktrace:

DBD::CSV::db do failed: Can't call method "print" on an undefined value at /home/ba01m/perl5/perlbrew/perls/perl-5.32.0-threads/lib/site_perl/5.32.0/DBD/CSV.pm line 410.
    DBD::CSV::Table::push_row(DBD::CSV::Table=HASH(0x5587daf78350), DBI::st=HASH(0x5587da2cb1d0), ARRAY(0x5587da36b3d0)) called at /home/ba01m/perl5/perlbrew/perls/perl-5.32.0-threads/lib/site_perl/5.32.0/SQL/Statement.pm line 232
    SQL::Statement::CREATE(DBD::CSV::Statement=HASH(0x5587da2cb230), DBI::st=HASH(0x5587da2cb1d0), ARRAY(0x5587daff92d8)) called at /home/ba01m/perl5/perlbrew/perls/perl-5.32.0-threads/lib/site_perl/5.32.0/SQL/Statement.pm line 157
    SQL::Statement::execute(DBD::CSV::Statement=HASH(0x5587da2cb230), DBI::st=HASH(0x5587da2cb1d0), ARRAY(0x5587daff92d8)) called at /home/ba01m/perl5/perlbrew/perls/perl-5.32.0-threads/lib/site_perl/5.32.0/x86_64-linux-thread-multi/DBI/DBD/SqlEngine.pm line 1271
    eval {...} called at /home/ba01m/perl5/perlbrew/perls/perl-5.32.0-threads/lib/site_perl/5.32.0/x86_64-linux-thread-multi/DBI/DBD/SqlEngine.pm line 1269
    DBI::DBD::SqlEngine::st::execute(DBI::st=HASH(0x5587da2cb1d0)) called at /home/ba01m/perl5/perlbrew/perls/perl-5.32.0-threads/lib/site_perl/5.32.0/x86_64-linux-thread-multi/DBI.pm line 1635
    DBD::_::db::do(DBI::db=HASH(0x5587daf77c38), "CREATE TABLE foo (id INTEGER, name CHAR (10))") called at wtf2.pl line 11

Can someone help, please?

ufobat commented 3 years ago

Is this related? https://rt.cpan.org/Public/Bug/Display.html?id=133294

Tux commented 3 years ago

I don't think it is. I fixed the issue for this ticket, but I have to add some tests for it. If you cannot wait, this is the fix:

diff --git a/lib/DBD/CSV.pm b/lib/DBD/CSV.pm
index 8f46ffd..2a3a1a3 100755
--- a/lib/DBD/CSV.pm
+++ b/lib/DBD/CSV.pm
@@ -15,8 +15,8 @@ require DynaLoader;
 require DBD::File;
 require IO::File;

-our @f_SHORT = qw( file dir dir_search ext lock lockfile schema encoding );
-our @c_SHORT = qw( class eof
+our @f_SHORT = qw( class file dir dir_search ext lock lockfile schema encoding );
+our @c_SHORT = qw( eof
        eol sep_char quote_char escape_char binary decode_utf8 auto_diag
        diag_verbose blank_is_undef empty_is_undef allow_whitespace
        allow_loose_quotes allow_loose_escapes allow_unquoted_escape
Tux commented 3 years ago

Now pushed with test cases. Note that using a different class (not the case in your example, which pointed to a real bug) requires the invoking script to use/require that class before connecting, so your example for Text::CSV would be

use strict;
use warnings;
use DBI;
use Text::CSV; # <--- this line is required when using another class

my $tbl = shift || "foo";

# See "Creating database handle" below
my $dbh = DBI->connect ("dbi:CSV:", undef, undef, {
    f_ext      => ".csv/r",
    RaiseError => 1,
    PrintError => 1,
    csv_class  => "Text::CSV",
    }) or die "Cannot connect: $DBI::errstr";
$dbh->do ("CREATE TABLE $tbl (id INTEGER, name CHAR (10))");
Tux commented 3 years ago

DBD::CSV-0.58 released