|P is for Practical|
One thing you need to know to master Perl is how to deal with the scoping mechanisms it provides you with. You want globals? You got 'em! You want to avoid "collisions" (two variables with the same name clobbering each other)? You got it, and there's more than one way to manage the trick. But Perl's scoping rules aren't always so well understood, and it's not just the difference between my and local that trips people up, although clearing that up is going to be one of my purposes.
I've learned a lot from Coping with Scoping and sections in various Perl books ( e.g.Effective Perl Programming ). So credit has to go to those authors (Dominus for the first, and Joseph N. Hall and merlyn for the second. Dominus also provided some helpful corrections to errors (some egregious) to an earlier version of this tutorial, so he should get at least second-author credit. However, the documentation that comes with your local perl installation is always the most up-to-date you can get, so don't be afraid to use perldoc perlop and perldoc -f foo</code>! on your own system.
Yes, at the beginning ...
A basic idea, although one you need not master to write many scripts, is the notion of a namespace. Global variables (variables not declared with my live in a package. A package provides a namespace, which I'm going to explain by reference to the metaphor of a family name. In English speaking countries, "Robert" is a reasonably common name, so you (assuming you live in one) probably know more than one "Robert." Usually, for us humans, the current conversational context is enough to determine for our audience which Robert we're talking about (my chums down at the pool hall know Robert the darts genius, but at work, "Robert" is the CEO of our failing dot-com).
Of course these people have family names too (yes, those can be shared by different people as well -- but you can't expect this metaphor to be perfect =), and if we wanted to be fully explicit we'd add that to allow our audience to determine which Robert we are talking about. $Smith::Robert is a creature distinct from $Jones::Robert. When you have two different variables with the same (as it were) 'first name', you can explicitly declare which one you want to refer to, no matter where you are in your code, by using the full name of the variable.
Use the package operator to set the current package. When you put package Smith in your code, you are, in effect, saying that every unqualified variable or function name should be understood to belong to the Smith package. To go with our metaphor, you're saying "in this bit of code, I want to talk about the Smith family."
Implicitly, there's a package main; at the top of your scripts; that is, unless you explicitly declare a different package, all the variables you declare (keeping the caveat about my in mind) will be in main. Variables that live in a package are reasonably called "package globals", because they are accessible by default to every operator and subroutine that lives in the same package (and, if you're explicit about their names, outside the package, too).
Using packages makes accessing Perl variables sort of like travelling in different circles. For example, at work, it's understood that "Robert" is "Robert Szywiecki", the boss. At the pool hall, it's understood that "Robert" is "Robert Yamauchi", the darts expert. Here's a little code to illustrate the use of packages:
The variable $Robert's full name, as it were, is $Szywiecki::Robert (note how the $ moves out to the front of the package name, indicating that this is the scalar Robert that lives in package Szywiecki). To code and, most importantly, subroutines in the Szywiecki package, an unqualified $Robert refers to $Szywiecki::Robert -- unless $Robert has been 'masked' by my or local (more on that later).
Now, if you use strict (and you should, you should, you should -- see strict.pm, for example), you'll need to declare those global variables before you can use them, UNLESS you want to fully qualify them. That's why the second (apparent) call to terminate in the above example will fail. It's expecting to find a subroutine terminate in the main package, but no such critter has been defined. That is,
will produce an error, whereas if we fully qualified the name (remember that implicit package main in there), there's no problem:
To satisfy strict 'vars' (the part of strict that enforces variable declaration), you have two options; they produce different results, and one is only available in perl 5.6.0 and later:
One difference between our and and the 'older' use vars is that our provides lexical scoping (more on which in the section on my below).
Another difference is that with use vars, you are expected to give an array of variable names, not the variables themselves (as with our). Both mechanisms allow you to use globals while still maintaining one of the chief benefits of strict 'vars': you are protected from accidently generating a new variable via a typo. strict 'vars' demands that your variables be explicitly declared (as in "here's a list of my package globals"). Both of these mechanisms allow you to do this with package globals.
A thing to remember about packages (and potentially a bad thing, depending on how big a fan you are of "privacy") is that package globals aren't just global to that package, but they can be accessed from anywhere in your code, as long as the names are fully qualified. You can talk about Robert the darts expert at work, if you say "Robert Yamauchi" (warning: I didn't use strict here, but it's only for purposes of brevity!):
See? Understanding packages isn't really all that hard. Generally, a package is like a family of variables (and subroutines! the full name of that terminate in the example above is &Szywiecki::terminate -- similar remarks apply to hashes and arrays, of course).
my (and a little more on our) a.k.a. lexical scoping
Variables declared with my are not globals, although they can act sort of like them. A main use of my is to operate on a variable that's only of use within a loop or subroutine, but that's by no means where it ends. Here are some basic points about my
As long as you're writing one-file scripts (e.g. ones that don't import modules), some of these points don't matter a great deal. But if you're heavily into "privacy" and "encapsulation", and if you write modules and OO modules you will be, you'll need to understand all of the above.
Here's some commented code to explain some of these points:
As the bottom bit in the above example shows, because they don't live in any package, my variables can be visible even though a new package has been declared because the block is the file (at least for these purposes)
Now the example above used a 'naked' block -- there's no control structure (e.g. if or while) involved. But of course that makes no difference to the scoping.
File-level my variables ARE accessible from within blocks defined within that file (as the example above shows) this is one way in which they're sort of like globals. If, however, subroutine had been defined in a different file, we would have a run-time error. Once you know how my works, you can see, just by looking at the syntax of the file, where a my variable is going to be accessible. This is one reason the scoping it provides is called "lexical scoping." Here's a place where use vars and the 'new' our operator differ: if you specify our $foo in package Bar but outside of an explicit block, you're in effect saying that (until some other scoping operator comes into play) occurrences of $foo are to be understood as referring to $Bar::foo. This should illustrate the difference between use vars and the newer our:
Note that having the second print will produce an error, because $carol is interpreted as $Movie::carol, while $bob is interpreted as $main::bob.
While this "package spanning" (which is only apparent in the case of our!) is a partial functional similarity between the two different kinds of lexical scoping operators, don't forget the difference, which is that our declares a package global, while my does not.
local -- a.k.a. dynamic scoping
Now we arrive at local, which is only sort of like my, but due to its name, its function is sometimes confused with that of my. Here's the skinny : local $foo saves away the current value of the (package) global $foo, and determines that in the current block and any code called by the current block, $foo refers to whatever value you give it in that block (a bare local $foo will set $foo to undef; the same goes for my). As things now stand, local only works on globals, you can't use it on a my variable.
Since local can affect what happens outside of the block in which it's used, local provides what's called dynamic scoping, as its effect is determined by what happens when the script is run. That is, the compiler can't tell when local is going to have its effect or not at the time it's compiling the script (which happens before the script is run). This distinguishes dynamic scoping from the lexical scoping provided by my and our, the effects of which can be checked at compile time.
The basic upshot of this difference is that if you localize a variable within a block and call a subroutine from that block, that subroutine will see the value of the localized variable. This is a major difference between my and local. Compare the above example to this one:
original example modified as per Masem's note. Thanks!
Notice that the my declaration in mysub gets (apparently) ignored by showfoo (since we've left the block in which the my declaration is valid, but the local declaration in localsub doesn't get ignored. But after we've left that block, the original value of $foo is visible again.
I hope you learn as much from reading this as I did from writing it!