'..inaccessible from outside.. EXCEPT IF REFERRED TO BY AN EXPORTED SUB!'
Yes, I think that's what the first part of the sentence is trying to say: "They are available to all functions in that same file declared below them". Perhaps this helps: "inaccessible" does not mean "have gone out of scope" or "have stopped existing":
foo(); sub foo { my $foo = "foo"; bar(); print $foo; } sub bar { print $foo; # doesn't work! }
Here, I hope it's obvious that $foo is very much alive and well during the call to bar(), it is also still in scope inside sub foo, but sub bar has no way of accessing it!1 Think of sub foo and sub bar as being two different files (remember "file scope" is basically just another lexical scope), and I hope then the meaning of "but are inaccessible from outside" becomes clear.
So my explanation to myself now goes along the lines: "by referring to its own file-scoped lexicals, a module's exported subs become agents to the persisting state of those lexicals, available to any importer of those subs". Would you agree?
Yes, that's a good way to put it, keeping in mind the above distinction between "available/inaccessible" vs. having gone out of scope. Personally, I think it's easiest to think about it in terms of references, although in this case not the hard references described in perlref, but instead the "references" described in perlsub that I quoted before: "If something more permanent is still aware of the lexical, it will stick around." Since the subs in DataBank.pm refer to %webpaths, it is kept around.
(Note: Conceptually, this is like closures.)
Importer ... some of that oblique wizardry we referred to earlier
I wrote a bit about the export/import mechanism here: "... the mechanisms that Perl uses for modules and pragmas are actually relatively transparent in that there isn't a ton of magic involved, and one can see all the moving parts." (most of that thread is a little OT to this one since it's about the lexical effect of pragmas, but perhaps the simplified description of export-/importing in that node is helpful).
1 Ok, this is Perl, which likes to give you enough rope to shoot yourself in the foot if you want it, so every rule has an exception, but using that module should be considered dark magic to be used for debugging only.
In reply to Re^5: modular file scoping
by haukex
in thread modular file scoping
by Pstack
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |