The Perl Basics You Need To Know
Before I delve into the details of mod_perl programming in the future articles it's probably a very good idea to review some very important Perl basics. You will discover these invaluable when you start coding for mod_perl. I will start with pure Perl notes and gradually move to explaining the peculiarities of coding for mod_perl, presenting the traps one might fall into and explaining things obvious for some of us but may be not for the others.
Using Global Variables and Sharing Them Between Modules/Packages
Anyone interested in using mod_perl with the Apache Web server will need to know the basics of the Perl programming language. In this column, Stas Bekman explains some common pitfalls beginners face when initially tackling Perl.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 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 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.
Making Variables Global With Strict Pragma On
First you use:
use strict;
Then you use:
use vars qw( %hash @array);This declares the named variables as package globals in the current package. They may be referred to within the same file and package with their unqualified names; and in different files/packages with their fully qualified names.
With perl5.6 you can use the
ouroperator instead:our qw( %hash @array);If you want to share package global variables between packages, here is what you can do.
Using Exporter.pm to Share Global Variables
Assume that you want to share the
CGI.pmobject (I will use) between your modules. For example, you create it inscript.pl, but you want it to be visible inMy::HTML. First, you makeglobal.script.pl: ---------------- use vars qw(); use CGI; use lib qw(.); use My::HTML qw(); # My/HTML.pm is in the same dir as script.pl = CGI->new; My::HTML::printmyheader();Note that I have imported
fromMy::HTML. AndMy::HTMLdoes the export of:My/HTML.pm ---------------- package My::HTML; use strict; BEGIN { use Exporter (); @My::HTML::ISA = qw(Exporter); @My::HTML::EXPORT = qw(); @My::HTML::EXPORT_OK = qw(); } use vars qw(); sub printmyheader{ # Whatever you want to do with ... e.g. print ->header(); } 1;
