NAME

Apache::Directive -- A Perl API for manipulating Apache configuration tree

Synopsis

use Apache::Directive;

my $tree = Apache::Directive->conftree;

my $documentroot = $tree->lookup('DocumentRoot');

my $vhost = $tree->lookup('VirtualHost', 'localhost:8000');
my $servername = $vhost->{'ServerName'};

print $tree->as_string;

use Data::Dumper;
print Dumper($tree->as_hash);

my $node = $tree;
while ($node) {

    #do something with $node

    if (my $kid = $node->first_child) {
        $node = $kid;
    } 
    elsif (my $next = $node->next) {
        $node = $next;
    }
    else {
        if (my $parent = $node->parent) {
            $node = $parent->next;
        }
        else {
            $node = undef;
        }
    }  
}

Description

Apache::Directive allows its users to search and navigate the internal Apache configuration.

Internally, this information is stored in a tree structure. Each node in the tree has a reference to its parent (if it's not the root), its first child (if any), and to its next sibling.

Class Methods

Function arguments (if any) and return values are shown in the function's synopsis.

conftree()

$tree = Apache::Directive->conftree();

Returns the root of the configuration tree.

Object Methods

Function arguments (if any) and return values are shown in the function's synopsis.

next()

$node = $node->next;

Returns the next sibling of $node, undef otherwise

first_child()

$subtree = $node->first_child;

Returns the first child node of $node, undef otherwise

parent()

$parent = $node->parent;

Returns the parent of $node, undef if this node is the root node

directive()

$name = $node->directive;

Returns the name of the directive in $node.

args()

$args = $node->args;

Returns the arguments to this $node

filename()

$fname = $node->filename;

Returns the filename this $node was created from

line_number()

$lineno = $node->line_number;

Returns the line number in filename this $node was created from

as_string()

print $tree->as_string();

Returns a string representation of the configuration tree, in httpd.conf format.

as_hash()

$config = $tree->as_hash();

Returns a hash representation of the configuration tree, in a format suitable for inclusion in the <Perl> sections.

lookup()

lookup($directive, [$args])

Returns node(s) matching a certain value. In list context, it will return all matching nodes. In scalar context, it will return only the first matching node.

If called with only one $directive value, this will return all nodes from that directive:

@Alias = $tree->lookup('Alias');

Would return all nodes for Alias directives.

If called with an extra $args argument, this will return only nodes where both the directive and the args matched:

$VHost = $tree->lookup('VirtualHosts', '_default_:8000');

Authors

Copyright