[LA.pm] C-style Perl vs. regex
Robin Rowe
rower at movieeditor.com
Wed Feb 8 08:35:51 PST 2006
Without ever seeing any of my Perl code, some here have stated that it
would be significantly more concise if I would code the traditional Perl
way with regex instead of writing my Perl in a C idiom as I prefer.
Below is real code I'm working on now. It's almost line for line the
same logic that I would write in C (and therefore the same # of lines of
code). Instead of C-style it could have been implemented using tr, but I
don't see any advantage -- size or otherwise. This is typical Perl code
for me. I'm not suggesting I never use regex or that there aren't in the
world awk-type problems where regex would be the obvious better choice.
Please let me know how this code could be improved.
Thank you,
Robin
#!/usr/bin/perl -w
# conf.pl: creates DNS zone files for add-on domains
# Copyright 2/7/2006 Robin.Rowe at MovieEditor.com
# License BSD
use strict;
my $info = "** conf.pl creates DNS zone files for add-on domains **\n";
my $stamp = q(2006020700);
my $sysadmin = q(Robin.Rowe at MovieEditor.com);
my $postmaster = q(postmaster.movieeditor.com.);
my $ip = q(12.158.190.77);
my $local2_file = q(named.conf.local2);
my $hosts_addon_file = q(hosts.add-on);
my $ns1 = q(ns1.movieeditor.com.);
my $ns2 = q(ns2.movieeditor.com.);
my @domains =
('cinepaint.org','linuxmovies.org','risingcast.com','screenplaylab.com','smashphone.com');
my $now = localtime(time);
sub AppendHosts
{ my %param = @_;
my $domain = $param{domain};
my $zone = join "", $domain, ".zone";
print " appending $domain to $hosts_addon_file\n";
open(FILE,">>$hosts_addon_file") or die("can't open $hosts_addon_file");
my $hosts = join "", " mail.", $domain, " www.", $domain;
print FILE $hosts
}
sub AppendLocal2
{ my %param = @_;
my $domain = $param{domain};
my $zone = join "", $domain, ".zone";
print " appending $domain to $local2_file\n";
open(FILE,">>$local2_file") or die("can't open $local2_file");
print FILE <<ZZZ;
// $sysadmin
// $now
zone "$domain" {
type master;
notify no;
allow-query { any; };
file "/etc/$zone";
};
ZZZ
close (FILE);
}
sub CreateZoneFile
{ my %param = @_;
my $domain = $param{domain};
my $zone = $domain . q(.zone);
print " creating file $zone\n";
open(FILE,">$zone") or die ("can't open $zone");
print FILE <<ZZZ;
; $zone
; $sysadmin
; $now
\$TTL 3D
\$ORIGIN $domain\.
@ IN SOA ns1.$domain\. $postmaster (
$stamp ; YYYYMMDD##
3H ; refresh
1H ; retry
1W ; expire
1D ; cache
)
IN NS $ns1
IN NS $ns2
@ IN A $ip
IN MX 10 mail.$domain\.
IN TXT "v=spf1 a mx -all"
mail IN A $ip
www IN A $ip
ZZZ
}
sub CreateZone
{ my %param = @_;
my $domain = $param{domain};
AppendHosts(domain => $domain);
AppendLocal2(domain => $domain);
CreateZoneFile(domain => $domain);
}
sub CreateZoneList
{ print "Creating zone files from list:\n";
foreach (@domains)
{ CreateZone(domain => $_);
} }
sub RemoveZoneList
{ print "Removing zone files from list:\n";
print " removing $hosts_addon_file, $local2_file, and zone files\n";
unlink($hosts_addon_file);
unlink($local2_file);
foreach (@domains)
{ my $domain = $_;
my $zone = $domain . q(.zone);
unlink($zone);
} }
sub AppendZoneFromCmdLine
{ print "Appending zone from command line:\n";
CreateZone(domain => $ARGV[0]);
}
sub main
{ print $info;
RemoveZoneList();
CreateZoneList();
# AppendZoneFromCmdLine();
print "Done!\n";
print " Don't forget 'rndc reload'\n";
print " Don't forget 'cat hosts.add-on >> /etc/hosts'\n";
}
main();
###
More information about the Losangeles-pm
mailing list