in reply to Re: Using an "outer" lexical in a sub?
in thread Using an "outer" lexical in a sub?
#!/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". :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Using an "outer" lexical in a sub?
by ikegami (Patriarch) on Nov 01, 2006 at 21:42 UTC | |
by Joost (Canon) on Nov 01, 2006 at 21:52 UTC | |
|
Re^3: Using an "outer" lexical in a sub?
by Joost (Canon) on Nov 01, 2006 at 21:00 UTC | |
by tilly (Archbishop) on Nov 01, 2006 at 23:36 UTC | |
by Joost (Canon) on Nov 01, 2006 at 23:49 UTC | |
by tilly (Archbishop) on Nov 02, 2006 at 00:09 UTC | |
by Joost (Canon) on Nov 02, 2006 at 01:26 UTC | |
|