Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Error in insertion of MULTIPLE FILENAMES & CONTENTS INTO DATABASE

by Nik (Initiate)
on Dec 31, 2007 at 14:17 UTC ( [id://659750]=perlquestion: print w/replies, xml ) Need Help??

Nik has asked for the wisdom of the Perl Monks concerning the following question:

#=========== INSERTING MULTIPLE FILENAMES & CONTENTS INTO DATABASE === +========== my $insert = $db->prepare( 'INSERT INTO articles (title, body) VALUES +(?, ?)' ); my @files = glob "$ENV{'DOCUMENT_ROOT'}/data/text/*.txt"; while (@files) { open FILE, "<$ENV{'DOCUMENT_ROOT'}/data/text/$_" or die $!; while (<FILE>) { chomp; $insert->execute(split /\t/, $_, 2) or print $db->errstr; } close FILE; }
I'm getting an No such file or directory at D:\www\cgi-bin\init.pl line 70. when i try to run the above code which is supposed to grab all .txt filenames from '/data/text/' and their contents respectively and insert them into mysql table 'articles' into 2 columns (title and body).

Replies are listed 'Best First'.
Re: Error in insertion of MULTIPLE FILENAMES & CONTENTS INTO DATABASE
by marto (Cardinal) on Dec 31, 2007 at 14:37 UTC
    Nik,

    Given that you have been posting here (and on devshed, often cross posting the same question) for many years you don't seem to have taken the repeated advice from the monks, and learned some basic Perl and general programming skills. There is no evidence here that you have made any effort to debug this problem for yourself. jettero has kindly given you a huge hint.

    Many could see this continued behavior as being lazy, some could find it offensive that you continue to make no either little or no effort in simple debugging techniques, and even less in the way you describe/present your problem and expect people to spoon feed you a solution, while they (often) painfully have to extract further information relating to your current issue.

    Perhaps, with it being this time of year, you could make a new years resolution to develop better learning/programming habits.

    Martin
Re: Error in insertion of MULTIPLE FILENAMES & CONTENTS INTO DATABASE
by jettero (Monsignor) on Dec 31, 2007 at 14:23 UTC
    If you did a
    die "error opening $ENV{'DOCUMENT_ROOT'}/data/text/$_: $!"
    instead of an nondescript die $!, you would immediately see the problem I bet.

    -Paul

      error opening D:/www/data/text/: No such file or directory at D:\www\cgi-bin\init.pl line 70.

      Thats what the the error said. Why $_ didnt get interpolated?
      Why? while (@files) supposed to contain all available .txt filenames from '/data/text'
      And i say while(@files) so to iterate one filename item after another in the loop so with the use of $_ i could open them and read the contents of it.
      Did i made a syntax error in the code? Isn't while (@files) a valid statement meaning iterate on every item of array @files?

        Isn't while (@files) a valid statement meaning iterate on every item of array @files?

        It is a valid statement, but it does not mean what you think it means. It iterates as long as @files is true, i.e., forever (since you do not modify @files). Again, I implore you to go read perlsyn (and/or a good perl book) and learn something.

Re: Error in insertion of MULTIPLE FILENAMES & CONTENTS INTO DATABASE
by runrig (Abbot) on Dec 31, 2007 at 17:21 UTC
    while (@files) {
    Read perlsyn on the difference between the various forms of compound statements and loop control.
Re: Error in insertion of MULTIPLE FILENAMES & CONTENTS INTO DATABASE
by ysth (Canon) on Dec 31, 2007 at 21:38 UTC
    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.
      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.
        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
Re: Error in insertion of MULTIPLE FILENAMES & CONTENTS INTO DATABASE
by graff (Chancellor) on Dec 31, 2007 at 17:27 UTC
    Sorry, Nik, I have to agree with what marto said. Maybe it's the language barrier (English vs. Greek) that keeps getting in your way, or it's some sort of learning disability that you have. Maybe you are simply not cut out to be a programmer. But I find it really strange that your script contains this line:
    while (@files) {
    What are you thinking when you write stuff like this? Are you thinking? Try thinking: "what would that sort of while loop actually do?"

    Good luck.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://659750]
Approved by Corion
Front-paged by parv
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-25 19:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found