next up previous contents
Next: Module Examples Up: Perl Modules Previous: The Standard Modules

strict, my() and Modules

In order to use the strict pragma with modules, you need to know a bit more about the my() function about how it creates lexical variables instead of local variables. You may be tempted to think that variables declared with my() are local to a package, especially since you can have more than one package statement per file. However, my() does the exact opposite; in fact, variables that are declared with my() are never stored inside the symbol table.

If you need to declare variables that are local to a package, fully qualify your variable name in the declaration or initialization statement, like this:

use strict;



$main::foo = '';



package Math;

    $Math::PI = 3.1415 && $Math::PI;

This code snippet declares two variables: $foo in the main namespace and $PI in the Math namespace. The && $Math::PI part of the second declaration is used to avoid getting error messages from the -w command line option. Since the variable is inside a package, there is no guarantee that it will be used by the calling script and the -w command line option generates a warning about any variable that is only used once. By adding the harmless logical and to the declaration, the warning messages are avoided.


next up previous contents
Next: Module Examples Up: Perl Modules Previous: The Standard Modules
dave@cs.cf.ac.uk