I am trying to get my perl code to log all opened files with code like this:

my @OpenCalls; BEGIN { # See: http://www.perlmonks.org/?node_id=909403 *CORE::GLOBAL::open = sub(*;$@) { push @OpenCalls, [ @_[1..$#_] ]; use Symbol (); my $handle = Symbol::qualify_to_ref($_[0], scalar caller); $_[0] = $handle unless defined $_[0]; # pass up to caller if (@_ == 1) { CORE::open $handle; } elsif (@_ == 2) { CORE::open $handle, $_[1]; } elsif (@_ == 3) { if (defined $_[2]) { CORE::open $handle, $_[1], $_[2]; } else { CORE::open $handle, $_[1], undef; # special case } } else { CORE::open $handle, $_[1], $_[2], @_[3..$#_]; } }; }

I am running into an issue with OOPerl where my filehandle is disappearing before the call to the DESTROY method:

"print() on closed filehandle PKG:: at PKG.pm line 104 during global destruction

Note that the CORE::GLOBAL::open override happens in a package different than PKG. I am using perl 5.10.0 on linux.

Dear wise Monks, any insights would be appreciated!


In reply to Override the open builtin globally.. redux by Anonymous Monk

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, details, 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, summary, 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.