Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hello nysus,

For sure the best thing to do is to open always lexical filehandles to let Perl close them for you. The habit to close them esplicitally is a sane one anyway.

Extra scope can help if you have many declared into the top scope.

But the strange behaviour you announce (leaking file handles like a sieve)

I'd expand the mr_mischief's solution: take count of what you open.

You can override open builtin function very soon in the program or in a BEGIN block, and you can profit of the ${^LAST_FH} special variable: if I understand the doc correctly it tracks the last open $fh (only read-fh ?) taking a reference to it

You probably need to override close to erase from your tracking variable.

An END block can dump the tracking datastructure.

So given a %fhs in the outher scope you can store there your fh

# TOTALLY UNTESTED!! no warning 'redefine'; my %fhs; BEGIN { *CORE::GLOBAL::open = sub { my ($fh,$mode,$path) = @_; # open the file $fhs{fileno($fh)} = $path.' '.${^LAST_FH}; }; } END{ print "Files already opened:\n", map {"fileno $_ $fhs{$_}\n"} sor +t keys %fhs; }

PS sorry the node was composed on a spare PC where was impossible to test anything..

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: A way to report open file handles a Perl script has open? -- using ${^LAST_FH} ? by Discipulus
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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (7)
    As of 2018-06-20 06:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?



      Results (116 votes). Check out past polls.

      Notices?