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

  • SEARCH
  • Home
  • 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 1055561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:34:53+00:00 2026-05-16T17:34:53+00:00

I recently inherited some code that someone else had written. I discovered that everywhere

  • 0

I recently inherited some code that someone else had written.

I discovered that everywhere in the code that a directory was opened for reading, it was never closed because the original developer had a syntax problem – he was using the close function to try to close a directory handle instead of the closedir function.

The code was something like this:

opendir( DIR, $dir ) or die "Cannot open $dir: $!\n";
@files = readdir( DIR );
close( DIR );

(Which is another good point that is made in Perl Best Practices (pages 208,278) about checking the return of the close function. If the return of close were checked in this case, it would be failing with “Bad file number”.)

I’ve since changed this to closedir, but it made me start wondering: Since the directory handle was never closed, what are the negative implications to keeping a directory handle opened for a long duration?

This program is larger (3,500 lines of code), runs for a while (5-10 minutes), and multiple instances of this program are running at the same time. In the case of this directory in the example above, $dir is the same value for all instances. If 10 instances of this program were running at the same time, they all held an open directory handle against the same directory for 5 minutes or longer. I’m sure Perl is closing the directory handle automatically when the program finishes, but best practice says to close it as soon as possible.

It is more obvious to me where leaving file handles open can cause problems (especially for file handles that are open for writing), but what bad things can happen by not closing a directory handle?

The reason I am asking is because there has been an odd circumstance where this program was trying to create a file (in the directory defined by $dir above). The filename had the PID embedded in it, so it is a smaller chance that the file could already be there, but Perl was unable to open the file for writing, because it said it already existed. When we looked in the directory, that file did not exist. I am wondering if all of the open directory handles on this directory could cause such a problem?

I’m not sure if the OS makes a difference, but this program is running on AIX.

Thanks in advance, and happy Friday!

  • 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-16T17:34:54+00:00Added an answer on May 16, 2026 at 5:34 pm

    You wasted a directory descriptor – which probably counts as a file descriptor. It would ultimately hurt you if your program opened enough directories to run out of file descriptors. Otherwise, it is pretty harmless, though less than ideal. It makes the system (and Perl) keep resources around which it might otherwise be able to release.

    If the directory handle was a local variable, not a plain DIR-style name, you might have Perl cleaning up behind you. See opendir which says:

    Opens a directory named EXPR for processing by readdir, telldir, seekdir, rewinddir, and closedir. Returns true if successful. DIRHANDLE may be an expression whose value can be used as an indirect dirhandle, usually the real dirhandle name. If DIRHANDLE is an undefined scalar variable (or array or hash element), the variable is assigned a reference to a new anonymous dirhandle. DIRHANDLEs have their own namespace separate from FILEHANDLEs.

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

Sidebar

Related Questions

In some code that I've recently inherited the responsibility for, I've found several instances
I inherited some code that was recently attacked where the attacker sent repeated remote
I inherited some code that was recently attacked by repeated remote form submissions. Initially
I have recently inherited a J2EE Struts web app that was written back in
I've inherited a 10K-line program written in 8051 assembly language that requires some changes.
I have recently inherited some Java code and need to integrate it into a
I recently inherited some C# code where nearly every item in a file was
I've inherited a project at work recently that was written by one of our
I recently inherited a project that is using Spring SAML 2.0 but the code
So, I recently inherited some VHDL code, and my first reaction was, VHDL has

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.