cmd.line.geek has asked for the wisdom of the Perl Monks concerning the following question:

After trying to compile, I receive the error "Use use of uninitialized value in concatenation (.) or string at line 54 Can someone point me in the right direction?
#!/usr/bin/perl use DBI; use strict; use warnings; # Declare varaibles my $DBNAME = "Test"; my $DBTABLE = "Pandora"; my $DBUSER = "slacker"; my $DBPASS = "password"; my $DBHOST = "localhost"; my $csvfile = "/tmp/nowplayingtest.csv"; # Connect to database at hand, or die my $dbh = DBI->connect("DBI:mysql:$DBNAME:$DBHOST", $DBUSER, $DBPASS); open (INFILE, "/tmp/nowplayingtest.csv") or die "Can't open file!"; while (<INFILE>) { my @rows = split(';', $_); my $artist = $rows[0]; my $title = $rows[1]; my $album = $rows[2]; $dbh->do(qq/insert into "$DBTABLE" (Artist, Title, Album) values ($ +artist, $title, $album/) or warn "failed to insert $artist, $title, $ +album into table - $dbh->errstr" if ($@); my $res = $dbh->selectall_arrayref( q( SELECT Artist, Title, Album FRO +M Pandora)); foreach( @$res ) { print "\n$_->[0], $_->[1] $_->[2] $_->[3]\n\n"; } }

Replies are listed 'Best First'.
Re: Use of uninitialized value...
by wind (Priest) on Feb 08, 2011 at 02:05 UTC

    Your problem is likely in this section of code:

    my $res = $dbh->selectall_arrayref( q( SELECT Artist, Title, Album + FROM Pandora)); foreach( @$res ) { print "\n$_->[0], $_->[1] $_->[2] $_->[3]\n\n"; }

    You're selecting 3 columns from the table, but are trying to print out 4.

    - Miller

      Thanks a lot. That took care of the error. Though the script isn't doing what I thought it was supposed to.
Re: Use of uninitialized value...
by Anonymous Monk on Feb 08, 2011 at 02:10 UTC
    use diagnostics or splain
    $ perl -we " print $_.undef" Use of uninitialized value $_ in concatenation (.) or string at -e lin +e 1. Use of uninitialized value in concatenation (.) or string at -e line 1 +. $ perl -Mdiagnostics -we " print $_.undef" Use of uninitialized value $_ in concatenation (.) or string at -e lin +e 1 (#1) (W uninitialized) An undefined value was used as if it were alread +y defined. It was interpreted as a "" or a 0, but maybe it was a mi +stake. To suppress this warning assign a defined value to your variables. To help you figure out what was undefined, perl will try to tell y +ou the name of the variable (if any) that was undefined. In some cases it + cannot do this, so it also tells you what operation you used the undefine +d value in. Note, however, that perl optimizes your program and the opera +tion displayed in the warning may not necessarily appear literally in y +our program. For example, "that $foo" is usually optimized into "that + " . $foo, and the warning will refer to the concatenation (.) operat +or, even though there is no . in your program. Use of uninitialized value in concatenation (.) or string at -e line 1 + (#1)

    The code you posted does not have a line 54

      Your absolutely right, sorry about that, I forgot to compensate for GPL license block.
        In the future, please don't "compensate" and actually run the code you post to get the output you say it produces. Thanks
Re: Use of uninitialized value...
by wind (Priest) on Feb 08, 2011 at 18:32 UTC

    You also have another major problem in your script with this line:

        $dbh->do(qq/insert into "$DBTABLE" (Artist, Title, Album) values ($artist, $title, $album/) or warn "failed to insert $artist, $title, $album into table - $dbh->errstr" if ($@);

    Basically, your insert statement will never run because it will only run if there is an eval error, $@. I would advise you to change your script in the following ways by doing better error checking and using placeholders in your insert statement:

    #!/usr/bin/perl use DBI; use strict; use warnings; # Declare varaibles my $DBNAME = "Test"; my $DBTABLE = "Pandora"; my $DBUSER = "slacker"; my $DBPASS = "password"; my $DBHOST = "localhost"; my $csvfile = "/tmp/nowplayingtest.csv"; # Connect to database at hand, or die my $dbh = DBI->connect("DBI:mysql:$DBNAME:$DBHOST", $DBUSER, $DBPASS) or die "DB connect failed: $DBI::errstr"; my $update_h = $dbh->prepare(qq{INSERT INTO $DBTABLE (Artist, Title, A +lbum) VALUES (?, ?, ?)}); open my $ih, $csvfile or die "Can't open file, $csvfile: $!"; while (<$ih>) { chomp; my @rows = split ';'; # Is a semicolon your delimiter, or is it a +comma? my $artist = $rows[0]; my $title = $rows[1]; my $album = $rows[2]; $update_h->execute($artist, $title, $album) or die $dbh->errstr; } my $res = $dbh->selectall_arrayref( q( SELECT Artist, Title, Album FRO +M Pandora)); foreach( @$res ) { print "\n$_->[0], $_->[1] $_->[2]\n\n"; }

    - Miller