in reply to print() on Closed File Handle Error!

Ah that helps! Printing out the error message is what I needed! I was assuming that the filehandle would automatically create the file to write to, and it didn't hence the error message. Thank you very much you guys! Now I know what to do next time! :-)

  • Comment on Re: print() on Closed File Handle Error!

Replies are listed 'Best First'.
Re^2: print() on Closed File Handle Error!
by lostjimmy (Chaplain) on Aug 16, 2010 at 15:04 UTC
    I still think you are missing a very important point, so I'll just reiterate it here.

    Opening a file for writing will automatically create the file if necessary. But you are not opening the file for writing (>), you are opening it for reading (<). Please read the docs and make sure you understand the different modes used by open.

Re^2: print() on Closed File Handle Error!
by dasgar (Priest) on Aug 13, 2010 at 19:07 UTC

        I was assuming that the filehandle would automatically create the file to write to, and it didn't hence the error message.

    If that's what you're trying to do, you'll need to use the '>>' mode for open, which will create the file if it doesn't exist and destroy the contents if it does exist. Still, even when I have used this mode in my scripts, I still include the die statement and would recommend that you do so too. Very useful in debugging issues as you have just learned.

      That is incorrect. As is states in open

      If three or more arguments are specified then the mode of opening and the filename are separate. If MODE is '<' or nothing, the file is opened for input. If MODE is '>' , the file is truncated and opened for output, being created if necessary. If MODE is '>>' , the file is opened for appending, again being created if necessary.

      Both > and >> will create a file. > empties the file and >> appends. Your post has that backwards.

        Ooops! My bad.

        (Note to self: multitasking + responding to posts + going from memory = bad idea.)

      You should always have a check whether an file open worked - that's true for any language! In Perl we use the construct "or die". There a many ways that an open can fail some common ones besides name or path wrong: disk quota exceeded, don't have the right permissions to do what you are trying to do, file is already open by somebody else and the list goes on and on with all sorts of "yeah but's".