I am scripting the manipulation of apache virtual host configuration files and seek an appropriate tool for the task.

I've played with Apache::Admin::Config and Config::General both.

I like the former for its use of ordered lists preserving directive order, comments, empty lines and applying tidy rules to the format. Perhaps it is just me, but it leaves much to be desired in the way of clean and informative perldoc. I have a small concern that being 10 years old (apache 1.3 or earlier days), that it might be less relevant, though can't imagine what might have changes in the configuration files themselves. I have yet to figure out how to address and manipulate the depths of the data structure.

The latter I like because it provides an easy to navigate data structure and clear docs. But it fails on all of the benefits mentioned above offered by its competition. Also, I had thought it would provide a ->param() method like Config::Simple, but apparently it does not.

I have found next to nothing in the literature showing working examples and one option I have used failed altogether to garner a single hit here in perlmonks.org.

I would particularly appreciate any pointers to published examples of the successful use of either or both of the these modules; or pointers to others which might permit me to read a template config file, programmatically manipulate it and save it in a new location.

Any feedback would be appreciated.

-- Hugh

Here is what I'm seeing.

Using Apache::Admin::Config:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Test::Most tests => 5; use lib qw ( lib ); use_ok('My::NewModule::Apache'); my $apache_config_path = 't/conf/etc/apache2/sites-available'; my $virtual_host_name = 'aac.example.com.conf'; my $config_file = "$apache_config_path/$virtual_host_name"; is($config_file,'t/conf/etc/apache2/sites-available/aac.example.com.co +nf',"Our config file is: $config_file"); my $conf = new Apache::Admin::Config ( "$config_file", -indent => 2 ) or die $Apache::Admin::Config::ERROR; $conf->save('-reformat'); isa_ok($conf,'Apache::Admin::Config'); my @result = $conf->select( 'section' ); foreach my $result ( @result ){ isa_ok($result,'Apache::Admin::Config::Tree'); is($result->name,'VirtualHost','Found our Virtual Host container for + ' . $result->value); # print 'Name: ' . $result->name . ' : ' . $result->value . "\n"; # warn Dumper( $result ); foreach my $child ( $result->select( 'directive' ) ){ # isa_ok($child,'Apache::Admin::Config::Tree'); print 'Directive: ' . $child->name . ' : ' . $child->value . "\n"; } foreach my $section_child ( $result->select( 'section' ) ){ # isa_ok($section_child,'Apache::Admin::Config::Tree'); print 'Section: ' . $section_child->name . ' : ' . $section_child- +>value . "\n"; warn Dumper( $section_child->select('directive') ); # ->select('di +rective') ); foreach my $x ( @{$section_child->select('directive')} ){ print $x->name . "\n"; } } } # warn Dumper($conf->section('VirtualHost'));

and output like this:

$ perl -T t/11-aac.t 1..6 ok 1 - use My::NewModule::Apache; ok 2 - Our config file is: t/conf/etc/apache2/sites-available/cg.examp +le.com.conf # Looks like you planned 6 tests but ran 2.

Using Config::General:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Config::General; use Test::Most tests => 6; use lib qw ( lib ); use_ok('My::NewModule::Apache'); my $apache_config_path = 't/conf/etc/apache2/sites-available'; my $virtual_host_name = 'cg.example.com'; my $virtual_host_conf_name = $virtual_host_name . '.conf'; my $config_file = "$apache_config_path/$virtual_host_conf_name"; is($config_file,'t/conf/etc/apache2/sites-available/cg.example.com.con +f',"Our config file is: $config_file"); my $conf = Config::General->new( -ConfigFile => "$config_file", 'Apach +eCompatible' => 1 ); # can_ok($conf,'params'); my %config = $conf->getall; # warn Dumper( \%config ); my $new_virtual_host_name = 'www.our_new_site.com'; my $new_config_conf_file = "$apache_config_path/$new_virtual_host_name +"; my $new_virtual_host_conf_name = $new_virtual_host_name . '.conf'; foreach my $key (keys %{$config{'VirtualHost'}->{'*:80'}}){ if(ref $config{'VirtualHost'}->{'*:80'}->{$key} eq 'HASH'){ # skip this for the moment } else { $config{'VirtualHost'}->{'*:80'}->{$key} =~ s/$virtual_host_name/$ +new_virtual_host_name/; } } # warn Dumper( \%config ); my $new_conf = $conf->save_file( "$new_config_conf_file", \%config );

and output like this:

$ perl -T t/11-cg.t 1..6 ok 1 - use My::NewModule::Apache; ok 2 - Our config file is: t/conf/etc/apache2/sites-available/cg.examp +le.com.conf # Looks like you planned 6 tests but ran 2.
if( $lal && $lol ) { $life++; }
if( $insurance->rationing() ) { $people->die(); }

In reply to Seeking module appropriate for manipulating apache config files. by hesco

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.