in reply to Re^2: Generic accessor for inside-out objects...
in thread Generic accessor for inside-out objects...
Assuming that's all in that file, Foo::foo() is the only subroutine with a handle on the lexical $bee. Note that foo() even keeps that variable available when $bee goes "out of scope". this is very important in understanding what's really going on. You'll want to do a search on "perl closures".# file Foo.pm use strict; package Foo.pm my $bee = 2; # lexical variable sub foo { return $bee++; }
That means there is no way to get at $bee except via Foo::foo(). Say:
That fails because there is no $Bar::bee variable, but also, there is no way to specify you actually meant the $bee variable bound to the Foo::foo function.# some other file Bar.pm use strict; package Bar; sub bar { return $bee; }
Note that package (or "global") variables can be accessed from other scopes:
# file Foo.pm package Foo; our $blah = 2; # or use vars
#file Bar.pm package Bar; print $Foo::blah;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Generic accessor for inside-out objects...
by Anonymous Monk on Dec 10, 2007 at 22:19 UTC | |
by Joost (Canon) on Dec 10, 2007 at 22:49 UTC |