Home arrow Perl Programming arrow Page 4 - Introduction to mod_perl (part 4): Perl Basics

Using Exporter.pm to Share Global Variables - Perl

Now that you have mod_perl installed, you can move on to learning how to use it. This article will run you through some basic Perl concepts to start with.

TABLE OF CONTENTS:
  1. Introduction to mod_perl (part 4): Perl Basics
  2. Using Global Variables and Sharing Them Between Modules/Packages
  3. Making Variables Global With strict Pragma On
  4. Using Exporter.pm to Share Global Variables
  5. Using the Perl Aliasing Feature to Share Global Variables
  6. Using Non-Hardcoded Configuration Module Names
  7. The Scope of the Special Perl Variables
  8. Compiled Regular Expressions
  9. References
By: Stas Bekman
Rating: starstarstarstarstar / 6
January 03, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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, butyou want it to be visible in My::HTML. First, you make $qglobal.

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 doesthe 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 andscript.pl. It will work vice versa as well, if you create theobject in My::HTML but use it in script.pl. You have truesharing, since if you change $q in script.pl, it will be changedin My::HTML as well.

What if you need to share $q between more than two packages? Forexample 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 bothMy::HTML and script.pl. Only if you add

use My::Doc qw($q);

into My::HTML will $q be shared. Otherwise My::Doc will notshare $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;


 
 
>>> More Perl Programming Articles          >>> More By Stas Bekman
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: