LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-24-2011, 07:35 PM   #1
eherron.lanline
LQ Newbie
 
Registered: May 2011
Posts: 7

Rep: Reputation: Disabled
perl - writing to file - keep open or only during write?


Code:
i am working on this thread:
http://www.linuxquestions.org/questi...te=1&p=4366091
my question is if it is better to open a file every time i need to write to it or should i keep a file open the whole time and when i am done with the script, close it and sendmail it out?

Or i just thought of this: i could keep concatenating to a string and just sendmail when done.

thanks in advanced

Last edited by eherron.lanline; 05-24-2011 at 07:37 PM.
 
Old 05-24-2011, 10:31 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
There are potential issues either way. Opening and closing a file frequently is going to put extra load on the file system; since it is optimized for lots of writes, not for frequent reopening (and each time you close the file it will have to flush the pending writes). On the other hand, an open file has a greater potential for corruption (eg during a power failure). Both of these are only issues when taken to the extreme (thousands of file closes vs keeping it open for hours at a time). So most people would err on the side of keeping the file open because the code is simpler (especially since newer file systems cope quite well with sudden outages).

The other code consideration is localization of use; open the file only in the section/module/class that needs it, and close it when it has been used. This will localize the effect of any code changes.

Last edited by neonsignal; 05-24-2011 at 10:33 PM.
 
1 members found this post helpful.
Old 05-27-2011, 07:28 PM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Quote:
Originally Posted by eherron.lanline View Post
Code:
i am working on this thread:
http://www.linuxquestions.org/questi...te=1&p=4366091
my question is if it is better to open a file every time i need to write to it or should i keep a file open the whole time and when i am done with the script, close it and sendmail it out?

Or i just thought of this: i could keep concatenating to a string and just sendmail when done.

thanks in advanced
Perl will normally buffer writes and then flush the buffer when you close the file or exit the program.

If you're worrying about losing data by keeping the file open, you can set autoflush on the file
handle so it writes to disk on every I/O.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Where does the actual write happens when writing to a proc file? archieval Linux - Kernel 4 11-21-2010 11:43 PM
Writing file in perl/cgi ?? !! ashok.g Programming 4 04-16-2010 07:02 AM
perl scripting, writing to more than one file homey Programming 2 06-28-2006 03:56 PM
problem with writing into a file using socket(perl) akaash Programming 3 04-08-2004 06:06 AM
perl - writing a data structure to a file.. is it possible? paavaka Programming 4 07-14-2001 12:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration