in reply to auto-vivifying file handles

Well hopefully we will have error messages that also includes the filename and the contents of $!. (Like perlstyle says to do.)

But lexically scoped filehandles have something to say for them. And you can even do it in Perl 5.005. Consider:

use Carp; # time passes # Takes a name to open, returns an open filehandle sub my_open { my $file = shift; # Put the declaration in the open to confuse people? :-) my $fh = do {local *FH}; open($fh, $file) or confess("Cannot open '$file': $!"); return $fh; }
There you are. Now you can just write:
my $handle = my_open("whatever");
and you will have lexically scoped filehandles. The only gotcha is that people may get confused about why they cannot use them like this:
print $self->{handle} @stuff;
but instead must either put the handle in a scalar, turn print into a method call, or use the syntactic trick:
print { $self->{handle} } @stuff;

Replies are listed 'Best First'.
Re: Re (tilly) 1: auto-vivifying file handles
by blakem (Monsignor) on Sep 26, 2001 at 03:46 UTC
    my $fh = do {local *FH};
    Isn't there some perl lore surrounding this idiom? I seem to remember someone writing a 500 line module to localize a filehandle, only to have the perl community stumble upon this construct a few weeks later?

    -Blake

      Well once upon a time there was a flamewar on p5p. It was not a small flamewar. It was one of those things which went on and on and on through hundreds of messages. It was originally about whether chmod should accept symbolic modes (eg "o+w" to add write permission for the owner). Some liked the idea. Others though it was bloat. Some thought that people who couldn't sling bits were better off serving Real Programmers their fries. You know how it is.

      As these things will go, it digressed. One of many threads got into some complaining about what beasts the FileHandle and IO::* modules had turned into, with a comment that the actual need was simple.

      At that point Tom Christiansen replied pointing out that the original need was already met. There was some discussion and surprise as people wondered how it worked. Not all of which is easy to trace in the link because the thread was so long that the automatic thread reconstruction is a little flaky...

      The construct was around before that. I don't know when it was originally discovered, but certainly many big names in the Perl community were surprised by it in 1999.

Re: Re (tilly) 1: auto-vivifying file handles
by John M. Dlugosz (Monsignor) on Sep 25, 2001 at 23:20 UTC
    Using scalars for file handles without relying on auto-vivication: gotcha.

    What does $self->{handle} have to do with my $handle ?

    —John

      Sorry for being unclear. My warning is that when you start using any old scalars as filehandles, people are likely to expect that you can use any construct that gives a scalar as a filehandle. Unfortunately this doesn't work.

      And a classic case that causes confusion comes when you put the handle in an object and then wonder why the obvious syntax doesn't work very well...

        Yea, I've hit that myself (not for files, though) where I forget that I'm implicitly omitting braces, and that only works on a simple scalar name.

        —John