redhotpenguin has asked for the wisdom of the Perl Monks concerning the following question:
I seek to increase my regular expression foo and ask for your guidance in streamlining this snippet I wrote to split up a file I have containing VirtualHost entries for Apache. In agreeing to host my friends' websites I have grown lazy and accumulated many VirtualHosts in one single file. Now is the time to clean up my act. Here's what I have which works, I ask your teachings in how to make this code more elegant.
#!/usr/bin/perl use strict; use warnings; open(FH, '< vhosts.conf'); my $content = do {local $/; <FH> }; my $start = q{<VirtualHost \*:80>}; my $finish = q{</VirtualHost>}; my @vhosts = $content =~ m!$start(.*?)$finish!sg; foreach my $vhost (@vhosts) { my ($name) = $vhost =~ /ServerName\s(\S+)/; open(OUT, "> $name.conf") or die "Can't open outfile $name: $!\n"; print OUT "<VirtualHost *:80>"; print OUT $vhost; print OUT "</VirtualHost>"; close(OUT); } 1;
For those not familiar with the data I'm trying to split up here's an example:
<VirtualHost *:80> ServerName www.foobar.com ServerAlias foobar.com DocumentRoot /var/www/foobar.com <Directory /var/www/foobar.com> Order allow,deny Allow from all </Directory> LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i -> %U\" \"%{Use +r-Agent}i\ "" combined ErrorLog /var/log/apache2/foobar.com.error_log CustomLog /var/log/apache2/foobar.com.access_log combined Include conf/deflate.conf </VirtualHost>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Clean up httpd.conf virtual host mess with perl
by Cody Pendant (Prior) on Apr 06, 2005 at 10:51 UTC | |
|
Re: Clean up httpd.conf virtual host mess with perl
by tlm (Prior) on Apr 06, 2005 at 09:39 UTC | |
|
Re: Clean up httpd.conf virtual host mess with perl
by reasonablekeith (Deacon) on Apr 06, 2005 at 10:30 UTC | |
|
Re: Clean up httpd.conf virtual host mess with perl
by nobull (Friar) on Apr 06, 2005 at 19:05 UTC | |
by redhotpenguin (Deacon) on Apr 06, 2005 at 20:37 UTC |