in reply to variable sees it, array doesn't - stumped!
The cause of the problems is found, now let's look at some other things. Most often when you say "our $variable" you mean "my $variable".
"my $variable" means "please create a brand new variable with this name that's accessible only within this block". "our $variable" on the other hand means just "I'm going to use the global variable (OK, package variable, there are multiple namespaces for globals) with this name within this block. I know it's global, but I do not want to waste time typing out its whole name each time". "our $variable", doesn't create a new variable, it just silences use strict;.
Compare,
use strict; { my $x; $x = 1; print "\$x=$x\n"; } { my $x; $x +=1; print "\$x=$x\n"; } # and { our $y; $y = 1; print "\$y=$y\n"; } { our $y; $y +=1; print "\$y=$y\n"; } # and see the globals in the main namespace print "\$main::x=$main::x, \$main::y=$main::y\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: variable sees it, array doesn't - stumped!
by shrdlu (Novice) on Jan 01, 2009 at 15:11 UTC |