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.

-- 
We're working on a six-year set of freely redistributable Vacation Bible School materials.

In reply to Re: Non-programmers and variable scope by jonadab
in thread Non-programmers and variable scope by akho

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.