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

Using the Perl Aliasing Feature 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

As the title says you can import a variable into a script or modulewithout using Exporter.pm. I have found it useful to keep all theconfiguration variables in one module My::Config. But then I haveto export all the variables in order to use them in other modules,which is bad for two reasons: polluting other packages' name spaceswith extra tags which increases the memory requirements; and addingthe overhead of keeping track of what variables should be exportedfrom the configuration module and what imported, for some particularpackage. I solve this problem by keeping all the variables in onehash %c and exporting that. Here is an example of My::Config:

package My::Config;
use strict;
use vars qw(%c);
%c = (
# All the configs go here
scalar_var => 5,
array_var  => [qw(foo bar)],
hash_var   => {
foo => 'Foo',
bar => 'BARRR',
},
);
1;

Now in packages that want to use the configuration variables I haveeither to use the fully qualified names like $My::Config::test,which I dislike or import them as described in the previous section.But hey, since I have only one variable to handle, I can make thingseven simpler and save the loading of the Exporter.pm package. Iwill use the Perl aliasing feature for exporting and saving thekeystrokes:

package My::HTML;
use strict;
use lib qw(.);
# Global Configuration now aliased to global %c
use My::Config (); # My/Config.pm in the same dir as script.pl
use vars qw(%c);
*c = \%My::Config::c;
# Now you can access the variables from the My::Config
print $c{scalar_var};
print $c{array_var}[0];
print $c{hash_var}{foo};

Of course $c is global everywhere you use it as described above, andif you change it somewhere it will affect any other packages you havealiased $My::Config::c to.

Note that aliases work either with global or local() vars - youcannot write:

my *c = \%My::Config::c; # ERROR!

Which is an error. But you can write:

local *c = \%My::Config::c;

For more information about aliasing, refer to the Camel book, secondedition, pages 51-52.



 
 
>>> 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: