Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

If I'm opening multiple files in a way that might lead to a leak (that is, a dynamic number of them rather than bespoke input/output file handles for an operation) then I keep track of related ones in a data aggregate. An array of file handles or a hash of descriptive keys with values that are file handles makes keeping information about them in one place more simple.

If you're using different file paths or have different IP hosts connecting to your sockets, perhaps a hash like this would be helpful.:

while ( readdir $dirhandle ) { if ( -f $_ ) { if ( open $file{ $_ }, '<', $_ ) { warn sprintf q{I have %d files open from the working direc +tory.}, scalar keys %file; # do stuff } else { delete $files{ $_ }; # report error } } } my @files = keys %file; for ( @files ) { # do something that for some reason requires all the files in the +directory open at once } for ( @files ) { close $file{ $_ }; delete $file{ $_ }; }

The easy solution is to explicitly close file handles as soon as your code is done with them or to open them as lexical handles. If you need to keep multiple file handles open over long periods, close them when you're done with them. Using a hash, you can easily delete the keys for files you no longer need after they're closed, too. Using an array you can shift or pop them if they're closed in array order or use splice. An array or hash and the associated item counts for those structures are easy ways to keep track of things.

I have to wonder, how are you storing your file handles in a way that you can't readily know how many you have open? Are you dynamically creating symbolic scalars for file handles? Don't do that. Are you storing them in arrays or hashes and just never closing them?


In reply to Re: A way to report open file handles a Perl script has open? by mr_mischief
in thread A way to report open file handles a Perl script has open? by nysus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-03-29 13:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found