O'Reilly logo

Writing Apache Modules with Perl and C by Lincoln Stein, Doug MacEachern

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required