Perl Notes/Perl Templating Example
From Federal Burro of Information
Jump to navigationJump to search
Template:
<VirtualHost *:80> ServerName OLDHOST DocumentRoot /var/www/html <Directory "/var/www/html"> Order allow,deny Allow from all </Directory> ProxyPass / http://NEWHOST/ </VirtualHost>
Script:
#!/usr/bin/perl -w use strict; my $template ; { local $/=undef; open FILE, "virtualtemplate.tmpl" or die "Couldn't open virtualtemplate.tmpl: $!"; # binmode FILE; $template = <FILE>; close FILE; } while (<>){ if ( /(\S+):(\d+) (\S+)/ ) { my $old = $1; my $port = $2; my $new = $3; my $host = $template; $host =~ s/OLDHOST/$old/g; $host =~ s/NEWHOST/$new/g; print $host; } }