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*
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |