For globals (our, use vars qw(), package variables) I'd use the analogy of school - you have
two classrooms, and in each there's, say, somebody called "John Smith", but they are not the
same person obviously, and their names got written to different class books (symbol tables).
For lexical vs. dynamic scoping, there's the wonderful my/local, space/time explanation.
our now... it's lexical also. Perhaps the family serves. Outside, you are called with
surname (package) and first name (variable) name, in your family (lexical block - space) and in some
places at work (which share you, hence "our"), calling you with your first name is ok.
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
| [reply] |
In the first place, packages aren't really scope. They're namespace. Package variables have exactly the same scope as global variables. All you really have to explain about them, then, is that if the package namespace isn't specified, the current package is implied, or main if you haven't specified a package. Other than that naming issue, they're just global.
In the second place, most new programmers already
have a pretty good understanding of global scope.
(Actually, it's really process scope (or possibly
thread scope if you're into that sort of perversion),
but nevermind: most new programmers understand
it well enough.)
So that really just leaves you explaining the
difference between lexical scope and dynamic scope.
And it's a pretty simple difference, in principle:
with dynamic scope, other code that you call will
see (and set, if applicable) your dynamic value.
With lexical scope, that's not the case.
Of course,
this simple difference in how lexical and dynamic
scope work leads to larger differences in terms
of how they are typically used. Lexical scope
(my in Perl) is typically used to
avoid having your private variables trompled on
by other code; whereas, dynamic scope
(local in Perl) is most often
used to change the behavior of other code that you
are calling, by temporarily changing the values
of some of its variables. So if you want a
function you are calling (say, print)
to behave in a
different way from the usual, you might use
local to dynamically scope one of
its variables (such as $/ for instance),
but if you don't want other code that's calling you
to mess with your private variables, you might use
my to scope them lexically to your
function or block.
The real confusion comes from the use of the
word local. Most people hear the word "local" and
think "local to this function", "local to this block",
or something along those lines, but in fact that's
lexical scope, which is what my does.
The Perl keyword
local does dynamic scope, which isn't
really local at all, except temporally.
| [reply] [d/l] [select] |
Everybody seems to care about cars and taxes. ;-)
So one could start to point out the difference in scope between federal, state and local tax as if you drive from one place to another.
The boundaries of the federation, state or locality are similar to blocks of a program.
Mmm, and programs compare with....cars? Yes, that quality program is like a robust car, and the notion of a beautiful program (style etc) certainly has a personal taste flavor to it! | [reply] |