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

Re: [ID 20010820.007] variables being hidden

From:
Hugo van der Sanden
Date:
August 21, 2001 07:10
Subject:
Re: [ID 20010820.007] variables being hidden
Message ID:
200108211404.f7LE48q20015@crypt.compulink.co.uk
"Giroux, Mike (Exchange)" <mgiroux@bear.com> wrote:
:John Gerschwitz-Senior Software Programmer PGS SPS Perth wrote:
:> 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.
:
:It's also, quite often, intended behaviour.

I think it is only rarely intended behaviour to declare a lexical
variable that you assign to but never use. It would not be
unreasonable to have an optional warning for such things, though
it might be a bit tricky to detect.

Hugo



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