in reply to Re^2: Perl script to comment out lines in named.con file
in thread Perl script to comment out lines in named.conf file

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}