Nice bear trap with $FOO file handle and $_

Discussion in 'Perl Misc' started by Dave Saville, Aug 23, 2012.

  1. Dave Saville

    Dave Saville Guest

    Just fell into a nice bear trap.

    open FOO, '>', $bar or die "$!";
    while ( <some file> )
    {
    print FOO;
    }

    I was changing my scripts to using $HANDLE - AIUI this is s "Good
    Thing(TM)" although I am not sure why :)

    So the above print turned into "print $FOO;" - which of course does
    not default to printing $_. It prints a GLOB to STDOUT.
    --
    Regards
    Dave Saville
    Dave Saville, Aug 23, 2012
    #1
    1. Advertising

  2. Ben Morrow <> writes:
    > Quoth "Dave Saville" <>:


    [...]

    >> I was changing my scripts to using $HANDLE - AIUI this is s "Good
    >> Thing(TM)" although I am not sure why :)

    >
    > Suppose you are working on a large program. Also suppose that in your
    > program you have a function foo
    >
    > sub foo {
    > ...
    > open FOO, ">", $bar or...;
    > bar();
    > print FOO;
    > }
    >
    > and a function bar
    >
    > sub bar {
    > ...
    > open FOO, ">", $somewhere_else or...;
    > my @lines = <FOO>;
    > close FOO;
    > }
    >
    > foo will not work as expected, since bar will reopen and then close the
    > same filehandle as foo is using, but this is not at all obvious from
    > looking at foo itself.


    That's sort-of a disingenious example because just using

    sub foo
    {
    local *FOO;

    and

    sub bar
    {
    local *FOO;

    the old-style way for creating local file handles, would work fine
    here. The downside of this is that using local changes the environment
    for all subroutines called from the subroutine containing the
    local: The newly created glob is implicitly accessible from all
    subroutines called from either foo or bar (and to subroutines called
    from these subroutines and so on) and it may even cause them to access
    a different filehandle then the one they were supposed to
    access. Because of this, local should be avoided except when this
    particular phenomenon is actually the desired behaviour.
    Rainer Weikusat, Aug 23, 2012
    #2
    1. Advertising

  3. Dave Saville

    Dave Saville Guest

    On Thu, 23 Aug 2012 14:06:29 UTC, Ben Morrow <> wrote:

    > Suppose you are working on a large program. Also suppose that in your
    > program you have a function foo
    >
    > sub foo {
    > ...
    > open FOO, ">", $bar or...;
    > bar();
    > print FOO;
    > }
    >
    > and a function bar
    >
    > sub bar {
    > ...
    > open FOO, ">", $somewhere_else or...;
    > my @lines = <FOO>;
    > close FOO;
    > }
    >
    > foo will not work as expected, since bar will reopen and then close the
    > same filehandle as foo is using, but this is not at all obvious from
    > looking at foo itself. If they are in different files, or indeed if bar
    > is in some module you are using which you didn't write yourself, it may
    > take a while for you to work out where the problem is.


    Thanks for the explanation Ben. I *knew* there was a good reason :)
    --
    Regards
    Dave Saville
    Dave Saville, Aug 23, 2012
    #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. Replies:
    3
    Views:
    374
    John Roth
    Jul 29, 2005
  2. JB
    Replies:
    3
    Views:
    555
  3. .rhavin grobert

    vector: Foo[5] == ((foo*)Foo) + 5 ?

    .rhavin grobert, Sep 23, 2008, in forum: C++
    Replies:
    4
    Views:
    379
    JaredGrubb
    Sep 24, 2008
  4. John Ramsden

    Bear trap in scoping rules

    John Ramsden, Sep 25, 2003, in forum: Perl Misc
    Replies:
    5
    Views:
    69
    Michael Carman
    Sep 27, 2003
  5. Replies:
    4
    Views:
    131
    Thomas 'PointedEars' Lahn
    Dec 23, 2007
Loading...

Share This Page