Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Non-programmers and variable scope

by akho (Hermit)
on Apr 28, 2007 at 15:42 UTC ( [id://612563]=perlmeditation: print w/replies, xml ) Need Help??

Most Revered Monks,

I've noticed many questions in Seekers of Perl Wisdom are, in essence, about variable scope (or at least are caused by a confusion related to scoping). And that is understandable; variable scope in Perl is much more complicated than in, say, Python (or BASIC). We have 'my', 'our', 'local', package variables, and I didn't even mention any of the weird effects tied variables may produce.

These options are convenient if you know how to use them (or have solid computer science knowledge). But the added complexity causes much grief to beginners.

How do you explain variable scope to people with no programming background? If they don't easily understand blocks? Is there a simple way?

Replies are listed 'Best First'.
Re: Non-programmers and variable scope
by shmem (Chancellor) on Apr 28, 2007 at 22:26 UTC
    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}
Re: Non-programmers and variable scope
by jonadab (Parson) on Apr 28, 2007 at 18:18 UTC

    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.
Re: Non-programmers and variable scope
by varian (Chaplain) on Apr 28, 2007 at 17:45 UTC
    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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://612563]
Approved by shigetsu
Front-paged by shigetsu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-23 06:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found