#!/usr/bin/perl use strict; use warnings; # "our $n;" disables "use strict 'vars';" for $n for the # remainder of the lexical scope in which it is located. sub foo { our $n; $n++; } our $n = 10; print "$n\n"; # 10 foo(); print "$n\n"; # 11
Well, this example helps me somewhat (though seemingly (to me) not directly with what I was originally confused about). Thank you. I'd previously always written "our $n = $whatever;" assuming that it atomically meant, "create a package global named $n with $whatever value". Looking more closely at our though, I see that that statement is really doing *2* separate things:
(Should've read the docs on our more carefully -- they're very good.)
So, in your example above, I now see that "our $n;" in sub foo isn't defining anything -- just telling perl that I'll be referring to $main::n in here as "$n". :)
In reply to Re^2: Using an "outer" lexical in a sub?
by cornballer
in thread Using an "outer" lexical in a sub?
by cornballer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |