in reply to Re: print() on closed filehandle
in thread print() on closed filehandle

Does the output file have to exist before it the system call on this code is made? Can it not create the file on the fly?

Replies are listed 'Best First'.
Re^3: print() on closed filehandle
by ikegami (Patriarch) on Dec 14, 2004 at 16:14 UTC

    It can fail for numerous reasons. The most common are probably:

    • Insufficient permissions
    • Path doesn't exist
    • Disk error (someone ejected it)
    • Invalid characters in file name
      I am referring to a file that is on another drive, a shared network drive. But I have done this with the input file that is used in the same system call. The file name is fine and so is the directory path. The permissions on the directory are fine.

        Yes, but when you open, and check the return value as recommended previously with the "or die $!" conditional, what error gets printed? That's an important part of the puzzle, you've got to check it.

        There is a possibility that you don't have write permissions in the network drive, or that your script isn't running with the correct permissions (especially if it's a CGI script, or being called by a CGI script, for example).

        Or there could be a network issue. Just try it with that "open FH, '>', $filename or die $!" statement and see that the error message is.


        Dave

        If you're so sure the open worked, how come you can't print to the handle? What is the objection you have to adding an error check? As those to whom you came for help, we're telling you that you should check if open succeeded.

        How you are reading the permissions? Reading FLAT_FILE requires less permissions than writing to OUTPUT_FILE.

Re^3: print() on closed filehandle
by davis (Vicar) on Dec 14, 2004 at 15:57 UTC

    The correct answer is: "Try it, or read perldoc -f open". The short answer is that using ">" in your open will (attempt to) open the file, clobbering (ie removing) its contents, or creating the file anew if necessary.

    Update: added documentation pointer.

    davis
    It wasn't easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.
      But my question really regards the system called statement.

        Sorry, but I cannot understand that. Could you rephrase?


        davis
        It wasn't easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.