in reply to Most Annoying Error : Use of uninitialized value in concatenation (.) or string at C:\Perl\O

Well, the offending line seems to be the one that starts with $DB->sql. Clearly, some element of @db_values is undefined. If that's OK with you, then do this:

{ no warnings 'uninitialized'; $DB->sql("INSERT INTO Files (File_Path, File_Name, Size_Byte, Cr +eated, Modified, Accessed, Type) VALUES ('$db_values[0]','$db_values[ +5]','$db_values[1]','$db_values[2]','$db_values[3]','$db_values[4]',' +$db_values[6]')"); }
Alternatively, leave that line as is and instead do something like this:
my @db_values = map defined( $_ ) ? $_ : '', split (/,/,$entry);

the lowliest monk

  • Comment on Re: Most Annoying Error : Use of uninitialized value in concatenation (.) or string at C:\Perl\O
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Most Annoying Error : Use of uninitialized value in concatenation (.) or string at C:\Perl\O
by jhourcle (Prior) on Jul 15, 2005 at 13:23 UTC

    It would also assume that there were no bad characters in the data being inserted, as there's nothing in the code to escape them.

    In a situation like this, I'd use bind variables, nulls were allowed. I'd use 'em even if nulls weren't allowed, I'd just verify my data before trying the insert:

    my $sth = $DB->prepare( 'INSERT INTO Files ( File_Path, File_Name, Siz +e_Byte, Created, Modified, Accessed, Type ) VALUES ( ?,?,?,?,?,?,? )' + ); foreach my $entry (@data) { next if ($entry =~ /^start|^Full/i); my @db_values = split (/,/,$entry); # do whatever value checking here ... checking for 7 values, etc. $sth->execute( @db_values ); }

    It wouldn't be bad to check for errors after doing an execute, either:

    $sth->execute( @db_values ) or warn $DB->errstr();