in reply to Error in insertion of MULTIPLE FILENAMES & CONTENTS INTO DATABASE

You mean "for" where you have "while". Simple debugging could have found this problem for you. After verifying that @files is set correctly, there are two possibilities: $ENV{DOCUMENT_ROOT} is not what you think it is or $_ is not what you think it is. Either step through your code in the debugger or insert debugging print statements; you will quickly discover $ENV{DOCUMENT_ROOT} to be correct, but some wacky value for $_. At this point, you know that while() is not setting $_ as you expected; either you decide perl is way to buggy and move on to PHP, C#, or the like, or you review the fine manual and discover that while() loops until an expression is true, not loops over an array.
  • Comment on Re: Error in insertion of MULTIPLE FILENAMES & CONTENTS INTO DATABASE

Replies are listed 'Best First'.
Re^2: Error in insertion of MULTIPLE FILENAMES & CONTENTS INTO DATABASE
by Nik (Initiate) on Jan 01, 2008 at 21:34 UTC
    i swithed it to:
    foreach (@files) { open FILE, "<$ENV{'DOCUMENT_ROOT'}/data/text/$_" or die "error open +ing $ENV{'DOCUMENT_ROOT'}/data/text/$_: $!";
    then i got this error error opening D:/www/data/text/D:/www/data/text/Ένα μήνυμα σε όλους τους άπιστους.txt: Invalid argument at D:\www\cgi-bin\init.pl line 70.

    $ENV{DOCUMENT_ROOT} i correct and $- is correctly set to but why not open Ένα μήνυμα σε όλους τους άπιστους.txt which is a file that does exist on /data/text ?

      The answer is right there in the error message -- you just have to read it carefully. Here is just the filename string from the error message:
      D:/www/data/text/D:/www/data/text/Ένα μήνυμα σε όλους τους άπιστους.txt
      
      Can you see the problem now? You have the value of $_, which comes from a file glob, and that value is:
      D:/www/data/text/Ένα μήνυμα σε όλους τους άπιστους.txt
      
      Then, inside the loop, you append that value to this string:
      $ENV{'DOCUMENT_ROOT'}/data/text/
      
      and the result of that concatenation is that the disk and directory path appears twice in the string that is passed to "open()". Fix that, and it will probably work as intended.
        I cannot believe iam THAT careless....my apologies and think you for pointing this out.
        my $insert = $db->prepare( 'INSERT INTO articles (title, body) VALUES +(?, ?)' ); my @files = glob "$ENV{'DOCUMENT_ROOT'}/data/text/*.txt"; foreach (@files) { open FILE, "$_" or die "error opening $_: $!"; while (<FILE>) { chomp; $insert->execute(split /\t/, $_, 2) or print $db->errstr; } close FILE; }
        gives me DBD::mysql::st execute failed: called with 1 bind variables when 2 are needed.... But iam passing it 2 vars why it says iam passing it only 1?
      You need only $_
      foreach (@files) { open FILE, "$_" or die "error opening $_: $!"; ... }

      Here is a cool video at google explaining glob
      http://www.youtube.com/v/j5vWBhq49ps&rel=1