Surprisingly, no one managed to hit the reason why this fails. In your original code, $filehandle is referencing a global glob. With your new code, $filehandle is now ... undef. So CORE::open looks at your old code, gets the string, and populates that glob, while with the new code, it gets the reference to $filehandle and updates it.

$filehandle is now its own copy of undef, rather than a reference to it. So update your code like this:

use strict; sub open { # log stuff here. CORE::open(@_); } if (-e $0 and open(my $fh, '<', $0)) { while(<$fh>) { print; } }
Works fine. The key point is not to shift off the reference to your caller's parameters so that you can continue to update them. Your logging stuff that is happening probably will need some changes (to stop referencing $filehandle, and all the array indexes are changing by one), but this should allow the new overridden open to work with both your updated code and your old-style code (with globs).

Update: No changes to the above, but to address ysth's comment, all I know is that I tested the above code prior to posting, and it printed itself out. Perl 5.8.6. YMMV.


In reply to Re: anonymous filehandle for overridden open sub by Tanktalus
in thread anonymous filehandle for overridden open sub by blahblahblah

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.