blahblahblah has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: anonymous filehandle for overridden open sub
by dragonchild (Archbishop) on Apr 07, 2005 at 18:31 UTC
    1. If security matters to you, convert to:
      open( my $fh, "<", $file ); if ( $fh ) { ... } else { log_error( "Couldn't open $file for 'reading': $!" ); }
      If you don't log why you couldn't open the filehandle, there's no point. Plus, open() implicitly performs a -e check, so that's superfluous. Now, doing a -d check might be more useful ...
    2. No, I haven't found a way to do what you're asking. I tried for an article I wrote for http://www.perl.com, but you cannot treat a bare string filename as a variable under strict, which is a pity.
Re: anonymous filehandle for overridden open sub
by BrowserUk (Patriarch) on Apr 07, 2005 at 18:33 UTC
    ... but then strict complains.

    What is strict complaining about?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco.
    Rule 1 has a caveat! -- Who broke the cabal?
      Good question. It was saying:

      Bareword "AFILE" not allowed while "strict subs" in use at (the line where the open call is)

      Is it significant that this code is in a sub that's autoloaded, where the autoload boils down to this?:

      eval 'require "$fileThatContainsTheSub"'; &$varThatContainsNameOfSub;
      I think it must be, because when I paste this sub into a small test script (no autoload), strict doesn't complain.

      -Joe

Re: anonymous filehandle for overridden open sub
by tilly (Archbishop) on Apr 07, 2005 at 21:34 UTC
    Untested but you should be able to modify the logging to:
    sub open { # ... some logging happens here ... return CORE::open($_[0], @_[1..$#_]); }
    The key is that variables are passed into functions by reference, so modifying the variable while it is still in @_ will be seen where the variable originally was.

    Note that, as others have pointed out, it is a bad idea to just try to open files and not have an error check in case of failure. See perlstyle for details. Furthermore I think that it is a good idea to switch to 3-arg open: <code>open(my $AFILE, "<", $file)<code> which will keep metacharacters in $file from being wrongly interpreted.

Re: anonymous filehandle for overridden open sub
by tlm (Prior) on Apr 07, 2005 at 18:39 UTC

    Does this help?

    use IO::File; if (-e $file && open (my $AFILE = IO::File->new(), "<$file")) { ...

    the lowliest monk

Re: anonymous filehandle for overridden open sub
by ysth (Canon) on Apr 07, 2005 at 21:48 UTC
    In addition to passing $_[0] to CORE::open, prototyping the logging function with *$;@ (assuming you don't want to support 1-arg open, which is more of a historical curiousity than anything else) may be needed for code that you haven't yet upgraded to using lexicals. Also, if the logging function is in a different package, use Symbol::qualify_to_ref (see perlsub) on defined non-glob, non-reference $_[0].
Re: anonymous filehandle for overridden open sub
by Joost (Canon) on Apr 07, 2005 at 18:31 UTC

      I get no output when I run this:

      use strict; sub open { my $filehandle = shift; return CORE::open($filehandle,@_[0..$#_]); } my $file = $0; my $AFILE; if (main::open ($AFILE, '<', $file)) { print while <$AFILE>; } else { warn 'Failed'; }
      (I do get output if the condition in the if statement is
      main::open($AFILE = IO::File->new(), "<$file")
      )

      the lowliest monk

Re: anonymous filehandle for overridden open sub
by Tanktalus (Canon) on Apr 07, 2005 at 21:28 UTC

    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.

      open's prototype makes just passing @_ not work. See tilly's answer for the way to do this.

      Update: well, I was so sure the above wouldn't work that I didn't even try it...so I wasn't misled by the apparent success. Because Tanktalus's code doesn't use warnings, he/she is missing this:

      Ambiguous call resolved as CORE::open(), qualify as such or use &
      So in fact the sub never even gets called. To call a sub with the same name as a builtin, you need to specify & (or have actually overriden the builtin as described in perlsub). Modifying the code to say &open(my $fh.... gives the error I was cautioning about:
      Can't use string ("3") as a symbol ref while "strict refs" in use
      due to the scalar prototype on open's first argument. (@_ == 3).

      Surprisingly, no one managed to hit the reason why this fails.

      Sorry for tooting my own horn here, but some of us did :-) .

      In your original code, $filehandle is referencing a global glob. With your new code, $filehandle is now ... undef.

      Therefore, give the overriding open a non-undef lexical handle instead: open(my $AFILE = IO::File->new(), ... ). This solution has the virtue that it does not require that the overriding open be modified, something that may not be possible or desirable.

      the lowliest monk