Re: First-Class Filehandle Trick in 5.8

From: Brian McCauley (nobull_at_mail.com)
Date: 01/30/04


Date: 30 Jan 2004 19:52:03 +0000

Scott McGerik <scottlm@visi.com> writes:

> I am using the First-Class Filehandle Trick, described at
> http://perl.plover.com/local.html#3_The_First_Class_Filehandle_Tr.
>
> In a particular program, I had this line:
>
> $INFILE = do { local *FH }; $OUTFILE = do { local *FH };
>
> I then proceed to open $INFILE for reading and $OUTFILE for writing with
>
> unless ( open $INFILE, '<', $infile ) {
>
> and
>
> unless ( open $OUTFILE, '>', $outfile ) {
>
>
> This worked in 5.6.1, but when I do the same under 5.8.0, I get the
> following error message:
>
> Filehandle FH opened only for output at ...
>
> I find this confusing because I thought that
>
> do { local *FH }
>
> would return an unnamed glob. But from the error message, it appears
> that it returned the glob for FH.
>
> I looked through the perldelta file, at
> http://www.perldoc.com/perl5.8.0/pod/perldelta.html, for 5.8, but I did
> not find any hints. Pointers and explanations are appreciated.

I must admit that my gut feeling is that the semantics of do { local
*FH } would not be something I'd expect to be stable.

Indeed anything to do with putting bare GLOBs in to scalars as opposed
to GLOBrefs makes my head (and my guts) spin.

I suggest you ask MJD what are the 'definite disadvantages' he
perceives in using GLOBrefs like everybody else does.

(Note that as of 5.6.1[*] you have autovification of GLOBrefs in open()
et al).

All that said, I just tried MJD's hack in 5.8.0 and it worked just
fine. So this brings us back to the advice is the posting guidelines:
"First make a short (less than 20-30 lines) and /complete/ program
that illustrates the problem you are having. People should be able to
run your program by copy/pasting the code from your article."

[*] The autovification functionality appears in 5.6.1 but didn't get
into the POD in that version.

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


Relevant Pages

  • Re: First-Class Filehandle Trick in 5.8
    ... >> that it returned the glob for FH. ... > perceives in using GLOBrefs like everybody else does. ... (creation of anonymous unopened filehandles) ... the references "do not ...
    (comp.lang.perl.misc)
  • Re: Raw Strings (I Think)
    ... Lanny wrote: ... us your code and show us the error message you're getting, so that we can determine what the cause really is. ... >>> import glob, os, random ... >>> print file ...
    (comp.lang.python)
  • Re: This should be a simple one
    ... You might have better luck with a glob. ... And with a good error message: ... use Perl; ...
    (comp.lang.perl.moderated)