7 use POSIX qw(strftime);
10 my $fh = IO::File->new($file,'r') or die "Unable to open $file for reading: $!";
11 my $fh_new = IO::File->new($file.".new",'w') or die "Unable to open ${file}.new for writing: $!";
13 my $fh_contents = <$fh>;
15 my ($old_serial) = $fh_contents =~ /IN\s+SOA\s+\S+\s+\S+\s*(?:\s*\;.+\n)*\((?:\s*\;.+\n)*\s*(\d+)/;
16 if (not defined $old_serial) {
17 print STDERR "problem dealing with $file\n";
18 unlink("${file}.new") or die "Unable to unlink ${file}.new: $!";
21 my ($ymd,$iter) = $old_serial =~ /(\d{4}\d{2}\d{2})(\d{1,2})/;
22 my $new_serial = strftime("%Y%m%d",gmtime())."01";
23 if ($ymd eq strftime("%Y%m%d",gmtime())) {
24 $new_serial = $ymd.sprintf('%02d',$iter+1);
26 $fh_contents =~ s/(IN\s+SOA\s+\S+\s+\S+\s*(?:\s*\;.+\n)*\((?:\s*\;.+\n)*\s*)(\d+)/${1}${new_serial}/;
27 print {$fh_new} $fh_contents;
30 rename($file,"${file}.bak") or die "Unable to rename $file to ${file}.bak: $!";
31 rename("${file}.new",$file) or die "Unable to rename ${file}.new to ${file}: $!";
32 unlink("${file}.bak") or die "Unable to unlink ${file}.bak: $!";