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

Using Non-Hardcoded Configuration Module Names - 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

You have just seen how to use a configuration module for configurationcentralization and an easy access to the information stored in thismodule. However, there is somewhat of a chicken-and-egg problem--howto let your other modules know the name of this file? Hardcoding thename is brittle--if you have only a single project it should be fine,but if you have more projects which use different configurations andyou will want to reuse their code you will have to find all instancesof the hardcoded name and replace it.

Another solution could be to have the same name for a configurationmodule, like My::Config but putting a different copy of it intodifferent locations. But this won't work under mod_perl because of thenamespace collision. You cannot load different modules which uses thesame name, only the first one will be loaded.

Luckily, there is another solution which allows us to stay flexible.PerlSetVar comes to rescue. Just like with environment variables,you can set server's global Perl variables which can be retrieved fromany module and script. Those statements are placed into thehttpd.conf file. For example

PerlSetVar FooBaseDir       /home/httpd/foo
PerlSetVar FooConfigModule  Foo::Config

Now I require() the file where the above configuration will be used.

PerlRequire /home/httpd/perl/startup.pl

In the startup.pl I might have the following code:

# retrieve the configuration module path
use Apache:
my $s             = Apache->server;
my $base_dir      = $s->dir_config('FooBaseDir')      || '';
my $config_module = $s->dir_config('FooConfigModule') || '';
die "FooBaseDir and FooConfigModule aren't set in httpd.conf" 
unless $base_dir and $config_module;
# build the real path to the config module
my $path = "$base_dir/$config_module";
$path =~ s|::|/|;
$path .= ".pm";
# I have something like "/home/httpd/foo/Foo/Config.pm"
# now I can pull in the configuration module
require $path;

Now I know the module name and it's loaded, so for example if I needto use some variables stored in this module to open a databaseconnection, I will do:

Apache::DBI->connect_on_init
("DBI:mysql:${$config_module.'::DB_NAME'}::${$config_module.'::SERVER'}",
${$config_module.'::USER'},
${$config_module.'::USER_PASSWD'},
{
PrintError => 1, # warn() on errors
RaiseError => 0, # don't die on error
AutoCommit => 1, # commit executes immediately
}
);

Where variable like:

${$config_module.'::USER'}

In my example are really:

$Foo::Config::USER

If you want to access these variable from within your code at the runtime, instead accessing to the server object $c, use the requestobject $r:

my $r = shift;
my $base_dir      = $r->dir_config('FooBaseDir')      || '';
my $config_module = $r->dir_config('FooConfigModule') || '';


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