man Apache2::Directive () - Perl API for manipulating the Apache configuration tree

NAME

Apache2::Directive - Perl API for manipulating the Apache configuration tree

Synopsis

  use Apache2::Directive ();

  my $tree = Apache2::Directive::conftree();

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

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

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

  my $node = $tree;
  while ($node) {
      print $node->as_string;

      #do something with $node

      my $directive = $node->directive;
      my $args = $node->args;
      my $filename = $node->filename;
      my $line_num = $node->line_num;

      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

CWApache2::Directive provides the Perl API for manipulating the Apache configuration tree

API

CWApache2::Directive provides the following functions and/or methods: Get the arguments for the current directive:

  $args = $node->args();
Arguments are separated by a whitespace in the string.
since: 2.0.00

For example, in httpd.conf:

  PerlSwitches -M/opt/lib -M/usr/local/lib -wT

And later:

  my $tree = Apache2::Directive::conftree();
  my $node = $tree->lookup('PerlSwitches');
  my $args = $node->args;

CW$args now contains the string -M/opt/lib -M/usr/local/lib -wT Get a hash representation of the configuration tree, in a format suitable for inclusion in <Perl> sections.

   $config_hash = $conftree->as_hash();
The config tree to stringify
since: 2.0.00

For example: in httpd.conf:

  <Location /test>
    SetHandler perl-script
    PerlHandler Test::Module
  </Location>

And later:

  my $tree = Apache2::Directive::conftree();
  my $node = $tree->lookup('Location', '/test/');
  my $hash = $node->as_hash;

CW$hash now is:

  {
    'SetHandler'  => 'perl-script',
    'PerlHandler' => 'Test::Module',
  }
Get a string representation of the configuration node, in httpd.conf format.

   $string = $node->as_string();
The config tree to stringify
since: 2.0.00

For example: in httpd.conf:

  <Location /test>
    SetHandler perl-script
    PerlHandler Test::Module
  </Location>

And later:

  my $tree = Apache2::Directive::conftree();
  my $node = $tree->lookup('Location', '/test/');
  my $string = $node->as_string;

CW$string is now:

  SetHandler perl-script
  PerlHandler Test::Module
Get the root of the configuration tree:

  $conftree = Apache2::Directive::conftree();
since: 2.0.00
Get the name of the directive in CW$node:

  $name = $node->directive();
since: 2.0.00
Get the filename the configuration node was created from:

  $filename = $node->filename();
since: 2.0.00

For example:

  my $tree = Apache2::Directive::conftree();
  my $node = $tree->lookup('VirtualHost', 'example.com');
  my $filename = $node->filename;

CW$filename is now the full path to the httpd.conf that VirtualHost was defined in.

If the directive was added with CWCIadd_config()CW, the filename will be the path to the httpd.conf that trigerred that Perl code. Get the first child node of this directive:

  $child_node = $node->first_child;
Returns the first child node of CW$node, CWundef if there is none
since: 2.0.00
Get the line number in a filename this node was created at:

  $lineno = $node->line_num();
since: 2.0.00
Get the node(s) matching a certain value.

  $node  = $conftree->lookup($directive, $args);
  @nodes = $conftree->lookup($directive, $args);
The config tree to stringify The name of the directive to search for Optional args to the directive to filter for In LIST context, it returns all matching nodes. In SCALAR context, it returns only the first matching node. If called with only CW$directive value, this method returns all nodes from that directive. For example:
  @Alias = $conftree->lookup('Alias');
returns all nodes for CWAlias directives. If called with an extra CW$args argument, it returns only nodes where both the directive and the args matched. For example:
  $VHost = $tree->lookup('VirtualHost', '_default_:8000');
since: 2.0.00
Get the next directive node in the tree:

  $next_node = $node->next();
Returns the next sibling of CW$node, CWundef if there is none
since: 2.0.00
Get the parent node of this directive:

  $parent_node = $node->parent();
Returns the parent of CW$node, CWundef if this node is the root node
since: 2.0.00

See Also

mod_perl 2.0 documentation.

Copyright

mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0.

Authors

The mod_perl development team and numerous contributors.