- $comment is a switch, which is turned on when the sought zone is found, and
turned of if the associated block is processed. If $comment is true (i.e. not '0' and not ''),
the current line is commented out.
- $block is a counter for the curly bracket level. It is increased with each
opening bracket, and decreased with each closed bracket. If the last closing bracket
of the zone block is found, it is zero, we are done and can unset $comment.
The empty () in the assignments force list context on the right hand side (global match),
so we get all matches; the left hand side is a scalar ($block), so we get the count of those matches:
a list evaluated in scalar context returns the number of elements of the list.
To edit the file, it makes sense to backup the file and write it:
#!/usr/bin/perl -w
use strict;
print " please enter the domain name: ";
my $targetdomain = <STDIN>;
chomp $targetdomain;
my $file = "/home/blackice/hello";
rename $file, "$file.bak" or die "Can't rename file '$file': $!\n";
open my $in, '<', "$file.bak" or die "Can't read file '$file': $!\n";
open my $out, '>', $file or die "Can't write file '$file': $!\n";
my $comment = 0;
my $block = 0;
while(<$in>) {
if (/^zone\s+"$targetdomain"/) {
$comment++;
$block += () = /(\{)/g;
print $out '// '.$_; next;
}
if($comment) {
$block += () = /(\{)/g;
s!^!// ! if $comment or $block;
$block -= () = /(\})/g;
$comment = 0 unless $block;
}
print $out $_;
}
This version works also with zone entries where the opening curly is on the next line:
zone "foo"
{
type "master";
...
};
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}