![]() |
Practical mod_perl / HTML Book / | ![]() |
||
![]() |
||||
![]() |
![]() |
|||
![]() |
![]() |
|||
![]() |
||||
|
|
||||
![]() |
![]() |
|||
![]() |
4.2.11. PerlSetVar and PerlAddVar |
![]() |
||
![]() |
||||
![]() |
![]() |
![]() |
||
![]() |
||||
PerlSetVar is another directive introduced by mod_perl. It is very similar to PerlSetEnv, but the key/value pairs are stored in an Apache::Table object and retrieved using the dir_config( ) method.
There are two ways to use PerlSetVar. The first is the usual way, as a configuration directive. For example:
PerlSetVar foo bar
The other way is via Perl code in <Perl> sections:
<Perl>
push @{ $Location{"/"}->{PerlSetVar} }, [ foo => 'bar' ];
</Perl>
Now we can retrieve the value of foo using the dir_config( ) method:
$foo = $r->dir_config('foo');
Note that you cannot use the following code in <Perl>sections, which we discuss later in this chapter:
<Perl>
my %foo = (a => 0, b => 1);
push @{ $Location{"/"}->{PerlSetVar} }, [ foo => \%foo ];
</Perl>
All values are passed to Apache::Table as strings, so you will get a stringified reference to a hash as a value (such as "HASH(0x87a5108)"). This cannot be turned back into the original hash upon retrieval.
However, you can use the PerlAddVar directive to push more values into the variable, emulating arrays. For example:
PerlSetVar foo bar PerlAddVar foo bar1 PerlAddVar foo bar2
or the equivalent:
PerlAddVar foo bar PerlAddVar foo bar1 PerlAddVar foo bar2
To retrieve the values, use the $r->dir_config->get( ) method:
my @foo = $r->dir_config->get('foo');
Obviously, you can always turn an array into a hash with Perl, so you can use this directive to pass hashes as well. Consider this example:
PerlAddVar foo key1 PerlAddVar foo value1 PerlAddVar foo key2 PerlAddVar foo value2
You can then retrieve the hash in this way:
my %foo = $r->dir_config->get('foo');
Make sure that you use an even number of elements if you store the retrieved values in a hash.
Passing a list or a hash via the PerlAddVar directive in a <Perl>section should be coded in this way:
<Perl>
my %foo = (a => 0, b => 1);
for (%foo) {
push @{ $Location{"/"}->{PerlAddVar} }, [ foo => $_ ];
}
</Perl>
Now you get back the hash as before:
my %foo = $r->dir_config->get('foo');
This might not seem very practical; if you have more complex needs, think about having dedicated configuration files.
Customized configuration directives can also be created for the specific needs of a Perl module. To learn how to create these, please refer to Chapter 8 of Writing Apache Modules with Perl and C (O'Reilly), which covers this topic in great detail.
 
Continue to:
mod_perl, modperl, Apache, perl, cgi, html, mod_perl, e-commerce, scalability, free, open source, OSS, apache, squid, high availability, modperl, linux, unix, Web, www, mod_perl, webserver, admin, apache, book, webmaster, tools, modperl, guide, docs, documentation, help, mod_perl, perl, information, apache, script, errata, eric cholet, perl, apache, mod-perl, stas bekman, mod_perl, cool, perl, Apache, performance, speed, choice
![]() |
Other projects to check out: meta-religion.com is for those interested in Religious, Spiritual and Esoteric Phenomena. i-want-a-better.com is a community of people discussing what they would like to be improved in their lives and things they use and interact with. You may also want to find a healer in your area or read articles on variety of topics.