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 theperl packages, but as you probably know once you start using packagesit's much harder to share the variables between the variouspackagings. A configuration package comes to mind as a good example ofthe package that will want its variables to be accessible from theother modules.
Of course using the Object Oriented (OO) programming is the best wayto provide an access to variables through the access methods. But ifyou are not yet ready for OO techniques you can still benefit fromusing the techniques I'm going to talk about.
Making Variables Global
When you first wrote $x in your code you created a (package) globalvariable. It is visible everywhere in your program, although if usedin a package other than the package in which it was declared(main:: by default), it must be referred to with its fullyqualified name, unless you have imported this variable withimport(). This will work only if you do not use strict pragma; butit's very important to use this pragma if you want to run your scriptsunder mod_perl.
Next: Making Variables Global With strict Pragma On >>
More Perl Articles
More By Stas Bekman