http://www.perlmonks.org?node_id=912503

hesco has asked for the wisdom of the Perl Monks concerning the following question:

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(); }