in reply to Re^3: Cannot work on second file after reading first file.
in thread Cannot work on second file after reading first file.


Hi Dave,

I apologize, I don't mean to waste your time.

The code I posted is all the code, there's nothing else.

I added the warn inside the while loop and it does not print. I had done something similar, using print instead of warn, which is how I (thought I) knew it was never entering the loop

As I said, it works perfectly when run independently of the large script. It stops working when I add it to it

Thanks
  • Comment on Re^4: Cannot work on second file after reading first file.

Replies are listed 'Best First'.
Re^5: Cannot work on second file after reading first file.
by davido (Cardinal) on Feb 28, 2014 at 17:42 UTC

    Ok, so you know the following:

    • The file opens.
    • The first line of the CSV file contains headers, as you expected.
    • The file has not reached "eof" after reading the first line (because we've now tested that).
    • The while loop never gets entered (because your "I'm inside the while loop" never prints.
    • The only way for the loop to not be entered is for my $row = $csv->getline($fh) to return a false value.
    • The documentation for Text::CSV says that 'getline' returns undef (which is false) if it fails to get and parse the line. This can happen at end of file, or in an error condition.
    • You set auto_diag in the constructor, so errors are being reported. I'm assuming you would tell us if an error was being reported, so it's probably safe to say one isn't. That only leaves "end of file" as a possible reason for you to not be entering the loop.

    Some of those things we know are contradictory. They can't both be true. We can't both not be at the end of the file, and be at the end of the file. We can't both have "auto_diag" set, and have an unreported error occurring.

    Is your "large script" running as the same user as your test script?


    Dave

      In my testing, $csv->eof did not return true even after trying a second get_line (According to the documentation is does not return true until after an attempt to read past the EOF.)

      In any event the cause was a change in global state in open_po: $/ was being set to undef. See Re: Cannot work on second file after reading first file.

        Good work.


        Dave

Re^5: Cannot work on second file after reading first file.
by SuicideJunkie (Vicar) on Feb 28, 2014 at 17:45 UTC

    "It stops working" is why you want to have those prints in it. They will tell you *where* it stopped working, and if you put useful variables and comments in the prints, it will also tell you *what* things are not as you expect. From where and what, you can run the nearby steps of the code in your head and thus figure out *why* it is not working.