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>
In reply to Clean up httpd.conf virtual host mess with perl by redhotpenguin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |