develooper Front page | perl.perl5.porters | Postings from August 2001

[ID 20010820.007] variables being hidden

From:
John Gerschwitz-Senior Software Programmer PGS SPS Perth
Date:
August 20, 2001 23:52
Subject:
[ID 20010820.007] variables being hidden
Message ID:
200108210646.OAA04197@pug.prth.tensor.pgs.com
I have a piece of Perl code that effectively does the following.

my $var1=0;
foreach my $var2 (@allVars) {
  if ($var2 eq $someValue) {
     my $var1=1;
  }
}
print $var1;

The declaration of $var1 within the loop hides the previous declaration of $var1 
so that if $var1 is assigned a new value within the loop it is lost outside of 
the loop. Even though I was running this with the -w switch the interpreter 
didn't warn me that I was hiding the first declaration of $var1. Is there anyway 
the interpreter can be run so that a warning is generated in this case as this 
seems a common problem.

Thanks 
John Gerschwitz
john.gerschwitz@prth.pgs.com





nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About