Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3875848
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:18:52+00:00 2026-05-19T22:18:52+00:00

Is it customary in such a case to open the file only once? #!/usr/bin/env

  • 0

Is it customary in such a case to open the file only once?

#!/usr/bin/env perl
use warnings;
use 5.012;
use autodie;

my $file = 'my_file';

open my $fh, '>>', $file;
say $fh "Begin";
close $fh;

$SIG{INT} = sub { 
    open my $fh, '>>', $file; 
    say $fh "End";
    close $fh;
    exit 
};

my $result;
while ( 1 ) {
    $result++;
    # ...
    # ...
    # ...
    open my $fh, '>>', $file; 
    say $fh $result;
    close $fh;
    sleep 3;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T22:18:52+00:00Added an answer on May 19, 2026 at 10:18 pm

    Short answer: Almost always, you should open/close only once. Details below.

    The decision of whether to do that depends on 4 things:

    1. Are there other processes that may need to write to the file?

      If so, you may need to lock the file, and the good behavior for a process designed for concurrent use is to release the locked shared resource as fast as possible so others can get the lock.

    2. Are there MANY files you need to open?

      If so, you might run out of file handles with too many open files, so you need to close.

    3. How much tolerance you have for losing data in the file if the program crashes.

      If you need to save the data from buffer into the file, you need to flush it. This CAN be done by frequently closing, although a better solution is either frequent flushing or autoflush on the file handle being turned on.

    4. Do you care greatly about not being able to close the file after running out of disk space?

      If so, the more often you close/reopen the file, the less data you will lose due to filesystem being full so whatever you wrote since last open is gone.

    In any other scenario, only open/close once (well, plus maybe extra close in __DIE__ handler and END{} block (and majority of time, you WILL likely be in other scenarios).

    That is because opening/closing the file just wastes system resources for no reason whatsoever AND makes your code longer. To be more specific, file open and close are expensive operations, requiring both system calls (that may force jump to kernel from userland), and extra disk IO which is VERY expensive resource wise. To verify that, run some system utilization measurment utility on your OS, and run a Perl script that does nothing except open/close 10000 different file names 100 times each.

    Please note (regarding scenarios #3/#4) that if you care greatly about not losing any data, you should not be using file IO in the first place – use a database or a messaging system with delivery guarantees.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After writing code to populate textboxes from an object, such as: txtFirstName.Text = customer.FirstName;
I know it is customary, but why? Are there real technical reasons why any
A customer of ours has Quickbooks 2005 and is looking to have their web
For customer service week this year, I have the privileged task of creating a
My customer is replacing MS Office with OpenOffice in some workstations. My program export
Our customer would like to know who is online and currently using the custom
A customer's application AppName has its configuration files stored in CommonAppData. Under Windows XP
Problem: Customer X has requested that pages using XML DataBinding and Databound tables in
The customer wants us to log the actions that a user performs on our
Background: Customer X is a low-budget non-profit outfit that nonetheless has a lot of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.