Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Nice bear trap with $FOO file handle and $_

Reply
Thread Tools

Nice bear trap with $FOO file handle and $_

 
 
Dave Saville
Guest
Posts: n/a
 
      08-23-2012
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
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      08-23-2012
Ben Morrow <(E-Mail Removed)> writes:
> Quoth "Dave Saville" <(E-Mail Removed)>:


[...]

>> 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.
 
Reply With Quote
 
 
 
 
Dave Saville
Guest
Posts: n/a
 
      08-23-2012
On Thu, 23 Aug 2012 14:06:29 UTC, Ben Morrow <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Bear Turns Nasty Alasdair Baxter Computer Support 4 03-25-2005 03:58 PM
File Handle Reading Blues: Rereading a File Handle for Input Dietrich Perl 1 07-22-2004 10:02 AM
DVD Verdict reviews: BROTHER BEAR, CHEAPER BY THE DOZEN (2003), and more! DVD Verdict DVD Video 0 04-26-2004 09:05 AM
DVD Verdict reviews: THE LITTLE POLAR BEAR, THE WUBBULOUS WORLD OF DR. SEUSS, and more! DVD Verdict DVD Video 0 02-28-2004 10:05 AM
Bear trap in scoping rules John Ramsden Perl Misc 5 09-27-2003 12:06 AM



Advertisments