CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Simple question about File handling



Nov 14, 2014, 3:33 AM

Post #1 of 2 (2197 views)
Simple question about File handling Can't Post

Hi all,

I have a perl script file that can take a few hours to run. I At the start of the script I open the file:

Then as the script run print to the file:
print FH...

Then only close the file at the very end of the script.

Is this best practice or would I be better to open and close the file every time I want to add to it?



Veteran / Moderator

Nov 14, 2014, 6:14 AM

Post #2 of 2 (2194 views)
Re: [Chancer] Simple question about File handling [In reply to] Can't Post

You can't open the file in read mode as you've shown if you plan on writing to that file. You need to open it write or append mode.

You should be using a lexical var for the filehandle instead of the bareword.

You should be using the 3 arg form of open and verify that the open call was successful before continuing.

You haven't provided enough info for us to say if you should keep the file open or close/reopen it every time you need to write to it.

1) How often does the write happen?
2) How much data is being written at each write?
3) Will some other process need to access (read or write to) the file during the running of this script?
4) Can you show us the relevant portion of the script so that we can get a better idea of what you're doing?


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives