Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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..


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

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?

    What's my password?
    Create A New User
    [Lotus1]: I was just pleasantly surprised to find I have shasum on my Windows machine. It was included in Strawberry Perl

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (6)
    As of 2018-03-18 00:13 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (228 votes). Check out past polls.