The Perl You Need To Know
by Stas BekmanApril 23, 2002
Introduction
Before I delve into the details of mod_perl programming, it's probably a good idea to review some 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
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, then you can still benefit from using the techniques I'm going to talk about.
|
Related Reading Practical mod_perl |
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), then 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 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($scalar %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 our operator instead:
our ($scalar, %hash, @array);
If you want to share package global variables between packages, then here is what you can do.
Using Exporter.pm to Share Global Variables
Assume that you want to share the CGI.pm object (I will use $q)
between your modules. For example, you create it in script.pl, but
you want it to be visible in My::HTML. First, you make $q
global.
script.pl:
----------------
use vars qw($q);
use CGI;
use lib qw(.);
use My::HTML qw($q); # My/HTML.pm is in the same dir as script.pl
$q = CGI->new;
My::HTML::printmyheader();
Note that I have imported $q from My::HTML. And My::HTML does
the export of $q:
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($q);
}
use vars qw($q);
sub printmyheader{
# Whatever you want to do with $q... e.g.
print $q->header();
}
1;
So the $q is shared between the My::HTML package and
script.pl. It will work vice versa as well, if you create the
object in My::HTML but use it in script.pl. You have true
sharing, since if you change $q in script.pl, then it will be changed
in My::HTML as well.
What if you need to share $q between more than two packages? For
example you want My::Doc to share $q as well.
You leave My::HTML untouched, and modify script.pl to include:
use My::Doc qw($q);
Then you add the same Exporter code that I used in My::HTML,
into My::Doc, so that it also exports $q.
One possible pitfall is when you want to use My::Doc in both
My::HTML and script.pl. Only if you add
use My::Doc qw($q);
into My::HTML will $q be shared. Otherwise My::Doc will not
share $q any more. To make things clear here is the code:
script.pl:
----------------
use vars qw($q);
use CGI;
use lib qw(.);
use My::HTML qw($q); # My/HTML.pm is in the same dir as script.pl
use My::Doc qw($q); # Ditto
$q = new CGI;
My::HTML::printmyheader();
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($q);
}
use vars qw($q);
use My::Doc qw($q);
sub printmyheader{
# Whatever you want to do with $q... e.g.
print $q->header();
My::Doc::printtitle('Guide');
}
1;
My/Doc.pm
----------------
package My::Doc;
use strict;
BEGIN {
use Exporter ();
@My::Doc::ISA = qw(Exporter);
@My::Doc::EXPORT = qw();
@My::Doc::EXPORT_OK = qw($q);
}
use vars qw($q);
sub printtitle{
my $title = shift || 'None';
print $q->h1($title);
}
1;


