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

In a long script that uses a handful of file handles with open I get a failure when opening a particular file for read only. The $! and $^E syatem variables don't return any information on why the open failed. I'm using a Windows 2K platform. All other files opened during this script work fine. The file does exist becuase I logged the error with the time and it is a few minutes after the file was created. I also wrote a small test script and $! and $^E return (No such file or directory) and (The system cannot find the file specified) respectively.

Has anyone had this problem? Or, can anyone recommend a debugging tip?

Thanks in advance.

Replies are listed 'Best First'.
Re: open fails with no error code
by bluto (Curate) on May 20, 2004 at 15:09 UTC
    Without seeing more code, the only thing that has bitten me in the past with $! is that I made another system call (e.g. logging some text) after the open but before checking $!, wiping it out. Obviously you can just save it off immediately after the open call is finished.

    As far as your "No such file..." message, one way to verify that the file does exist is to either use stat() or something like -f $path to check it's existance, but open() should work ok for this. Also, make sure you are either using a full path on the open() call or that you really are chdir'd into the correct directory.

Re: open fails with no error code
by sweetblood (Prior) on May 20, 2004 at 14:59 UTC
    Can you supply the code that is failing?
      I wish I could. It is esentially thousands of lines long including the packages it uses. It also uses other subsystems. When I make a snippet with the open that gives me a problem, it works correctly. It might be hard to come up with a solution. Even if I could get some ideas, I'd appreciate it. I'm kind of stumped. Thanks.
        Look very hard at the code you stripped out, that caused it to work. It must be something you trimmed out.

        Cheers,
        Sweetblood

Re: open fails with no error code
by ambrus (Abbot) on May 20, 2004 at 19:33 UTC

    I don't know what's bit you, as $! should really get set if open returns false. Just one thought: if there are signal handlers set, they should localize $! if they do anything that might effect it. I've been bitten by that once using signal handlers. (Don't assume that a function does not change $! just because it's not documented. Also, a function may still change $! if it succeds, as it may have called other functions that have failed.) Also check if you are really calling the builtin open function, not just a function that shaded it.

    Update: also, make sure that the open call really returns false, it's not just you thinking it failed because you can't read the filehandle or similar simptoms.

Re: open fails with no error code
by graff (Chancellor) on May 21, 2004 at 02:40 UTC
    can anyone recommend a debugging tip?

    Have you tried using the perl debugger?

    If you have identified the particular open statement that causes the failure, and you can work out the line number of that statement, run the script with a command line like this:

    perl -d name_of_script [...]
    Where the square brackets around "..." mean "this is where you would put command-line args for the script, if it takes args, otherwise, don't put anything here".

    That runs the script via the perl debugger, which starts by giving you a prompt for what to do next. At this point, it's handy to have the source code on display in a separate window (e.g. in an editor).

    The first thing to do is "b NNN" (where NNN is the line number of the open statement in question). This sets a breakpoint at that line, so that the debugger will stop execution and prompt you for more things to do just before it executes the chosen line. After setting the breakpoint, enter "c" to "continue" execution of the script, and wait for it to reach the breakpoint.

    Once execution stops, use the "p" command to inspect the values of relevant variables, to confirm that they contain what you expect. Read the "perldebug" man page, and try the "h" command in the debugger for more pointers on what you can do.

Re: open fails with no error code
by mifflin (Curate) on May 20, 2004 at 16:06 UTC
    I've had similar problems here but my problems where more file system.
    We recently installed Netapp file servers and the implementation did not go smoothly.
    File visibility would periodically go away so it looked like files or whole directories were suddenly gone.
    Our Unix SA's have fixed the problem (knocks on wood)
Re: open fails with no error code
by Anonymous Monk on May 20, 2004 at 14:59 UTC
    When I reduce the script to a snippet the open works fine. I can't post the extire script here because it relies on a lot of other data and subsystems. Can someone just point me in the right direction or let me know if this has happened to them?

    Thanks again.

      The "right direction" is always in reducing your code until the error goes away, and then adding code again, until the error reoccurs.

      In many cases, the process of properly preparing the failing script for posting here, you will find the error already.

      If you expect people to use magic or employ ESP remotely via the internet, you'll have to wait for the release of PSI::ESP I guess.

        Good point Corion. I'll widdle it down. I'm in the process of doing that but the error only seems to happen after about 10 hours of processing. Unfortunately the error seems to happen in the most intense part of the script. I reduced the intense part to about NIL and the problem wen away so it's in there somewhere. This is going to be a tedious task and I'm still working on it. I was hoping someone would say "Oh yea, I've seen this problem...". I'll get there I guess.