in reply to Another variable scoping oddity

Roger, if you run your code with -w, it gives you warning:

"my" variable $k masks earlier declaration in same scope at a.pl line +6.

Once you resolve the warning by only declaring $k once, everything is now fine:

use strict; my $k = 1; print "wanted: \$k = $k, got " . InvestigateValue() . "\n"; $k = 2;#modified print "wanted: \$k = $k, got " . InvestigateValue() . "\n"; sub InvestigateValue { return defined $k ? $k : 'undef'; }

Replies are listed 'Best First'.
Re: Re: Another variable scoping oddity
by Roger (Parson) on Nov 12, 2003 at 06:32 UTC
    I see, so that's the reason. That should teach me not to omit the -w flag, and add #!/usr/bin/perl -w to my code. I became too lazy these days. :(