First-Class Filehandle Trick in 5.8

Discussion in 'Perl Misc' started by Scott McGerik, Jan 30, 2004.

  1. 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.

    Scott McGerik
     
    Scott McGerik, Jan 30, 2004
    #1
    1. Advertising

  2. Scott McGerik <> 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\\
     
    Brian McCauley, Jan 30, 2004
    #2
    1. Advertising

  3. [A complimentary Cc of this posting was sent to
    Brian McCauley
    <>], who wrote in article <>:
    > > 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.


    > 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.


    I know only one such "defdis": for the target discussed above
    (creation of anonymous unopened filehandles) the references "do not
    work". Maybe it is fixed now, but at the time of 5.005 you could not
    create a reference to an anonymous (and properly garbage-collected)
    glob.

    Thus the abomination used above.

    Hope this helps,
    Ilya
     
    Ilya Zakharevich, Jan 30, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eduard W. Lohmann
    Replies:
    1
    Views:
    649
  2. E11
    Replies:
    1
    Views:
    4,842
    Thomas Weidenfeller
    Oct 12, 2005
  3. Tony Young
    Replies:
    3
    Views:
    321
    Daniel T.
    Mar 26, 2006
  4. eran
    Replies:
    7
    Views:
    408
  5. Massimo Di Pierro

    Class.__class__ magic trick help

    Massimo Di Pierro, Aug 20, 2012, in forum: Python
    Replies:
    7
    Views:
    217
    Massimo Di Pierro
    Aug 21, 2012
Loading...

Share This Page