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

RE: [ID 20010820.007] variables being hidden

August 21, 2001 05:30
RE: [ID 20010820.007] variables being hidden
Message ID:
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 you could see code like:

foreach my $key (keys %hashOfHashes) {
        my $hashRef=$hashOfHashes{$key};

        foreach my $key (keys %$hashRef) {

for example...  I'm not saying that's good or bad code, just that
it would work, and it's probably in use somewhere by a programmer
who made his/her code -w clean, and won't appreciate perl warning
about it now.
Mike Giroux
via, but not for, Bear-Stearns

Bear Stearns is not responsible for any recommendation, solicitation, 
offer or agreement or any information about any transaction, customer 
account or account activity contained in this communication.
*********************************************************************** Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About