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

[ID 20010820.007] variables being hidden

John Gerschwitz-Senior Software Programmer PGS SPS Perth
August 20, 2001 23:52
[ID 20010820.007] variables being hidden
Message ID:
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.

John Gerschwitz Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About