Introduction to mod_perl (part 4): Perl Basics - Using Global Variables and Sharing Them Between Modules/Packages (
Page 2 of 9 )
It helps when you code your application in a structured way, using the
perl packages, but as you probably know once you start using packages
it's much harder to share the variables between the various
packagings. A configuration package comes to mind as a good example of
the package that will want its variables to be accessible from the
other modules.
Of course using the Object Oriented (OO) programming is the best way
to provide an access to variables through the access methods. But if
you are not yet ready for OO techniques you can still benefit from
using the techniques I'm going to talk about.
Making Variables Global
When you first wrote $x in your code you created a (package) global
variable. It is visible everywhere in your program, although if used
in a package other than the package in which it was declared
(main:: by default), it must be referred to with its fully
qualified name, unless you have imported this variable with
import(). This will work only if you do not use strict pragma; but
it's very important to use this pragma if you want to run your scripts
under mod_perl.