while (<FILE>) clobbers previous value of $_ 
Author Message
 while (<FILE>) clobbers previous value of $_

This is definitely undesirable behaviour, in my opinion.  Is it a bug?
I'm pretty sure it is.

% cat file
Any old file.

% cat perlbug.pl
#! /usr/local/bin/perl -w

open(FILE,"<file");

$foo = "bar";

for ($foo) {
    for ("ok"){}        # local $_ is reallocated
    print;
    print "\n";

Quote:
}

print "$foo\n";         # This should print "bar"

for ($foo) {
    while (<FILE>) {}   # this CLOBBERS whatever $_ refers to
                        # but when this "local" $_ goes out of (while) scope,
                        #     it becomes undefined.
    print;      #line 18
    print "\n";

Quote:
}

print "$foo\n"; #line 21;  This should print "bar"

% perlbug.pl
bar
bar
Use of uninitialized value at perlbug.pl line 18, <FILE> chunk 1.

Use of uninitialized value at perlbug.pl line 21, <FILE> chunk 1.

%

--
Michael Schmahl -- Math/CS/Phil/Thr -- University of Alaska Fairbanks
http://www.*-*-*.com/
Unsolicited commercial email spellchecked for only $100!  No warranties.
rand:[ Procrastination is the art of keeping up with yesterday. ]



Wed, 12 Jan 2000 03:00:00 GMT  
 while (<FILE>) clobbers previous value of $_

Quote:

> This is definitely undesirable behaviour, in my opinion.

Not in mine, considering all $_ is is another variable ( all be it a bit
of a special one ) so why would you want it to behave differently from
any other variables. I am not sure where it states it in the man pages
but it the Camel and the refence guide it does state that $_ is a global
variable and should be localised within blocks/subroutines.

Quote:
>  Is it a bug?

Nope, I think you are expecting too much of it, after all you can only
have one instance of a variable within any given block. How would you
access the different instances of the same variable i.e:




they can't both be in $_.


not really any slower than using the defaults and it is probably clearer
( and safer in the long run ) if someone else has to maintain your
scripts in the future.

Simon



Sun, 16 Jan 2000 03:00:00 GMT  
 while (<FILE>) clobbers previous value of $_

Simon Fairey babbled:

Quote:

> > This is definitely undesirable behaviour, in my opinion.

> Not in mine, considering all $_ is is another variable ( all be it a
> bit
> of a special one ) so why would you want it to behave differently from

> any other variables. I am not sure where it states it in the man pages

> but it the Camel and the refence guide it does state that $_ is a
> global
> variable and should be localised within blocks/subroutines.

> >  Is it a bug?

> Nope, I think you are expecting too much of it, after all you can only

> have one instance of a variable within any given block. How would you
> access the different instances of the same variable i.e:




> entry,
> they can't both be in $_.


> not really any slower than using the defaults and it is probably
> clearer
> ( and safer in the long run ) if someone else has to maintain your
> scripts in the future.

> Simon

I have since been corrected that some of the above is a complete load of
old tosh. I guess I should have looked at the problem in more detail.
Although my first argument holds up for the while() within a for() loop
it is completely disemboweled by the for() within a for() loop. The only
thing I can say for certain is that I would still stand by my last

Thats what I get for trying to be too clever, I am now more confused
than before, but at least I have been enlightened in some way.

Simon



Sun, 16 Jan 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. <<<<<< HELP >>>>>>>>

2. >>>> A Singaporean Girl looking for more penpals!!<<<<<

3. <<>> COMPUTER SOFTWARE / HARDWARE <<>>

4. - - - = = =<><> W e b - U S A <><>= = = - - -

5. <<>> COMPUTER SOFTWARE / HARDWARE <<>>

6. ==>>COMPUTER HARDWARE / SOFTWARE <<==

7. >>>>>>>>>>>>>> Perl-script archiv wanted <<<<<<<<<<<<<<

8. Perl debugger prompt question <<##>>

9. newbie question regarding s/<old string>/<new string>/g

10. how to bootstrap <name_x>.dll from <non-name_x>.pm

11. <<My Sort Problem>>

 

 
Powered by phpBB® Forum Software