ApacheFormat(3pm) | User Contributed Perl Documentation | ApacheFormat(3pm) |
Config::ApacheFormat - use Apache format config files
Config files used with this module are in Apache's format:
# comment here RootDir /path/foo LogDir /path/foo/log Colors red green orange blue \ black teal <Directory /path/foo> # override Colors inside block Colors red blue black </Directory>
Code to use this config file might look like:
use Config::ApacheFormat; # load a conf file my $config = Config::ApacheFormat->new(); $config->read("my.conf"); # access some parameters $root_dir = $config->get("RootDir"); $log_dir = $config->get("LogDir"); @colors = $config->get("colors"); # using the autoloaded methods $config->autoload_support(1); $root_dir = $config->RootDir; $log_dir = $config->logdir; # access parameters inside a block my $block = $config->block(Directory => "/path/foo"); @colors = $block->get("colors"); $root_dir = $block->get("root_dir");
This module is designed to parse a configuration file in the same syntax used by the Apache web server (see http://httpd.apache.org for details). This allows you to build applications which can be easily managed by experienced Apache admins. Also, by using this module, you'll benefit from the support for nested blocks with built-in parameter inheritance. This can greatly reduce the amount or repeated information in your configuration files.
A good reference to the Apache configuration file format can be found here:
http://httpd.apache.org/docs-2.0/configuring.html
To quote from that document, concerning directive syntax:
Apache configuration files contain one directive per line. The back-slash "\" may be used as the last character on a line to indicate that the directive continues onto the next line. There must be no other characters or white space between the back-slash and the end of the line. Directives in the configuration files are case-insensitive, but arguments to directives are often case sensitive. Lines that begin with the hash character "#" are considered comments, and are ignored. Comments may not be included on a line after a configuration directive. Blank lines and white space occurring before a directive are ignored, so you may indent directives for clarity.
And block notation:
Directives placed in the main configuration files apply to the entire server. If you wish to change the configuration for only a part of the server, you can scope your directives by placing them in <Directory>, <DirectoryMatch>, <Files>, <FilesMatch>, <Location>, and <LocationMatch> sections. These sections limit the application of the directives which they enclose to particular filesystem locations or URLs. They can also be nested, allowing for very fine grained configuration.
This module will parse actual Apache configuration files, but you will need to set some options to non-default values. See "Parsing a Real Apache Config File".
$config->get("foo");
You can write:
$config->foo;
Defaults to 0.
For example:
# httpd.conf UseCanonicalName On
Then in Perl:
$config = Config::ApacheFormat->new(fix_booleans => 1); $config->read("httpd.conf"); if ($config->get("UseCanonicalName")) { # this will get executed if set to Yes/On/True }
This option defaults to 0.
Website http://my.own.dom JScript $Website/js Images $Website/images
Undefined variables in your config file will result in an error. To use a literal "$", simply prefix it with a "\" (backslash). Like in Perl, you can use brackets to delimit the variables more precisely:
Nickname Rob Fullname ${Nickname}ert
Since only scalars are supported, if you use a multi-value, you will only get back the first one:
Options Plus Minus "About the Same" Values $Options
In this examples, "Values" will become "Plus". This is seldom a limitation since in most cases, variable subsitution is used like the first example shows. This option defaults to 0.
For example:
# $ENV{PATH} = "/usr/sbin:/usr/bin" SetEnv PATH "/usr/sbin:/usr/bin" # $ENV{MY_SPECIAL_VAR} = 10 SetEnv MY_SPECIAL_VAR 10 # delete $ENV{THIS} UnsetEnv THIS
This option defaults to 0.
$config = Config::ApacheFormat->new( valid_directives => [qw(Bar Bif)], );
$config = Config::ApacheFormat->new( valid_blocks => [qw(Directory Location)], );
AddHandler cgi-script .cgi .sh AddHandler server-parsed .shtml
To parse this correctly, use:
$config = Config::ApacheFormat->new( hash_directives => [qw(AddHandler PerlSetVar)] );
Then, use the two-argument form of "get()":
@values = $config->get(AddHandler => 'cgi-script');
This allows you to access each directive individually, which is needed to correctly handle certain special-case Apache settings.
Port 8080 # ... Port 5053
In this case, the directive "Port" would be set to the last value, 5053. This is useful because it allows you to include other config files, which you can then override:
# default setup Include /my/app/defaults.conf # override port Port 5053
In addition to this default behavior, "Config::ApacheFormat" also supports the following modes:
last - the value from the last one is kept (default) error - duplicate directives result in an error combine - combine values of duplicate directives together
These should be self-explanatory. If set to "error", any duplicates will result in an error. If set to "last" (the default), the last value wins. If set to "combine", then duplicate directives are combined together, just like they had been specified on the same line.
All of the above attributes are also available as accessor methods. Thus, this:
$config = Config::ApacheFormat->new(inheritance_support => 0, include_support => 1);
Is equivalent to:
$config = Config::ApacheFormat->new(); $config->inheritance_support(0); $config->include_support(1);
Calling read() more than once will add the new configuration values from another source, overwriting any conflicting values. Call clear() first if you want to read a new set from scratch.
For example, given this confiuration file:
Foo 1 Bar bif baz bop
The following code would work as expected:
my $foo = $config->get("Foo"); # $foo = 1 my @bar = $config->get("Bar"); # @bar = ("bif", "baz", "bop")
If the name is the name of a block tag in the configuration file then a list of available block specifiers will be returned. For example, given this configuration file:
<Site big> Size 10 </Site> <Site small> Size 1 </Site>
This call:
@sites = $config->get("Site");
Will return "([ Site =" "big"], [ Site => "small" ])>. These arrays can then be used with the block() method described below.
If the directive was included in the file but did not have a value, 1 is returned by get().
Calling get() with no arguments will return the names of all available directives.
Directives declared in "hash_directives" require a key value:
$handler = $config->get("AddHandler", "cgi-script");
"directive()" is available as an alias for "get()".
MaxSize 100 <Site "big"> Size 10 </Site> <Site "small"> Size 1 </Site>
this code:
print "Max: ", $config->get("MaxSize"), "\n"; $block = $config->block(Site => "big"); print "Big: ", $block->get("Size"), " / ", $block->get("MaxSize"), "\n"; $block = $config->block(Site => "small"); print "Small: ", $block->get("Size"), " / ", $block->get("MaxSize"), "\n";
will print:
Max: 100 Big: 10 / 100 Small: 1 / 100
Note that "block()" does not require any particular number of parameters. Any number will work, as long as they uniquely identify a block in the configuration file. To get a list of available blocks, use get() with the name of the block tag.
This method will die() if no block can be found matching the specifier passed in.
print $config->dump;
Or:
for ($config->block(VirtualHost => '10.1.65.1')) { print $_->dump; }
If you want to see any output.
To parse a real Apache config file (ex. "httpd.conf") you'll need to use some non-default options. Here's a reasonable starting point:
$config = Config::ApacheFormat->new( root_directive => 'ServerRoot', hash_directives => [ 'AddHandler' ], include_directives => [ 'Include', 'AccessConfig', 'ResourceConfig' ], setenv_vars => 1, fix_booleans => 1);
Some possible ideas for future development:
I know of no bugs in this software. If you find one, please create a bug report at:
http://rt.cpan.org/
Include the version of the module you're using and a small piece of code that I can run which demonstrates the problem.
Copyright (C) 2002-2003 Sam Tregar
This program is free software; you can redistribute it and/or modify it under the same terms as Perl 5 itself.
Apache::ConfigFile
Apache::ConfigParser
2021-01-06 | perl v5.32.0 |