8.1. Simple Configuration with the PerlSetVar Directive
The Apache Perl API provides a simple mechanism for passing information from configuration files to Perl modules using the PerlSetVar directive. As we've seen, the directive takes two arguments, the name of a variable and its value:
PerlSetVar FoodForThought apples
Because Perl is such a whiz at parsing text, it's trivial to pass an array or even a hash in this way. For example, here's one way (out of a great many) to pass an array:
# in configuration file
PerlSetVar FoodForThought apples:oranges:kiwis:mangos
# in Perl module
@foodForThought = split ":", $r->dir_config('FoodForThought');
And here's a way to pass a hash:
# in configuration file
PerlSetVar FoodForThought apples=>23,kiwis=>12
# in Perl module
%foodForThought = split /\s*(?:=>|,)\s*/, $r->dir_config('FoodForThought);
Notice that the pattern match allows whitespace to come before or after the comma or arrow operators, just as Perl does.
By modifying the pattern match appropriately, you can pass more complex configuration information. The only trick is to remember to put double quotes around the configuration value if it contains whitespace and not to allow your text editor to wrap it to another line. You can use backslash as a continuation character if you find long lines a pain to read:
PerlSetVar FoodForThought "apples => 23,\
kiwis => 12,\
rutabagas => 0"
If you have a really complex configuration, then you are probably better off using a separate configuration ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access