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:
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).use strict; sub open { # log stuff here. CORE::open(@_); } if (-e $0 and open(my $fh, '<', $0)) { while(<$fh>) { print; } }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |