Paul Archer wrote: > Actually, while we're on the subject: can anyone *really* explain the > difference between the two, or perhaps more importantly, when someone > would want to use 'our' over 'my'? I've read the docs, but they're > not sinking in.
Perl has two kinds of variables. The first kind are global variables (also called "package" variables or "symbol table" variables). These variables a) have a name in a package's symbol table, and b) have a lifetime equal to the duration of your process (Notwithstanding Perl's "local" statement, which adds a twist. But save that for another discussion.) You refer to a package variable like this: $MyPackage::foo = 1; This assigns to a scalar, $foo, in the "MyPackage" package. You don't have to declare such a variable; it is created automatically simply by your having used it's name. Perl has a "package" statement, which allows you to define a default package to be used when you don't explicitly specify one. So the following is equivalent to what's above: package MyPackage; $foo = 1; (If no package statement has been issued, the default package is "main"). But that raises a problem. Since global variables are created automatically, it's easy to introduce bugs into your program by making typos in variable names. That's where "use strict" comes in. "use strict" (which should *always* be used in your programs), requires you to pre-declare package variables that you plan to use without a package name. Consider: package MyPackage; use strict; $foo = 1; # compile-time error: $foo not defined "our" is a declaration that introduces a package variable. So instead you write: package MyPackage; use strict; our $foo; $foo = 1; # OK; $foo has been declared You can combine the declaration and assignment in one step: our $foo = 1; Now if you accidentally misspell $foo as $fooo later in your program, Perl will raise a compile-time error. You can think of "our" as a license to use the variable without its package name under "use strict". "our" obeys lexical scoping rules. That is to say, your "license" is good from the point you issue the "our", until the end of the innermost enclosing block, eval, or file. Consider: use strict; { our $foo; $foo = 1; # OK, "our" declaration is in force } # block ends, "our" declaration ends $foo = 2; # compile-time error; outside of "our" scope Note that "our" is never strictly required. "use strict" always allows you to fully qualify a variable with a package name, even if you haven't declared it with "our": use strict; $MyPackage::foo = 1; # legal anywhere But doing this robs you of the compile-time checking that "use strict" provides. So, we recommend the use of "use strict" and "our" for package variables. The second type of variable is a lexical variable. These are created with the "my" statement. Unlike package variables, lexicals do not have an entry in a package symbol table. lexical variables also have a lexical scope: they have an existence only from the point of declaration until the end of the innermost enclosing block. This makes them similar to "auto" or stack-based variables in other languages. (Actually, the situation is more complex than that; file-scoped lexicals have essentially global lifetime, and if a global reference is taken to a block-scoped lexical, that value can persist beyond the lexical scope, unlike in languages like C). Anyway, you generally want to use the most restrictive scope appropriate for your variables, in order to formalize the interfaces between your program's parts and minimize the use of side effects through global variables. That is why "my" variables are generally recommended over package variables. There are some cases where you need package variables. For example, only package variables can be exported through the Exporter.pm mechanism. Since we always recommend the use of "use strict", you need to use "our" to declare those variables. (However, note that *importing* a symbol via the "use Package qw(list)" mechanism declares the symbols for the purposes of "use strict". "our" only needs to be used in the module that exports the symbols.) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]