Perl Notes/Perl Templating Example

From Federal Burro of Information
Revision as of 16:25, 22 March 2013 by David (talk | contribs) (Created page with "Template: <pre> <VirtualHost *:80> ServerName OLDHOST DocumentRoot /var/www/html <Directory "/var/www/html"> Order allow,deny ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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;
        }
}