Hi, I work on a very large and very old application, most of which doesn't use strict. We're always looking to incrementally clean it up, and I recently have been opening files like this:
if (-e $file && open (my $AFILE, "<$file")) { ...
instead of using our old idiom, which used a global filehandle name:
if (-e $file && open (AFILE, "<$file")) { ...
Someone else recently tried my code, and they happened to have some logging turned on which broke the code. The logging overrides 'open' so that it can log files which have been opened. It looks like this:
sub open { my $filehandle = shift; # ... some logging happens here ... my $return; $return = CORE::open($filehandle,@_[0..$#_]); return $return; }
We use this logging very infrequently, but still it's useful sometimes so I don't want to break it. The easy solution is to go back to using the global filehandle, but then strict complains. Anyone have any good ideas as to how I can make this work? I found this old post, which talks about "tricks to create a reference to an anonymous filehandle" but doesn't go into detail. I'm wondering if I can create the filehandle first and then pass open a reference to it, or something like that. Thanks.

-Joe


In reply to 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.