use strict;
use warnings;
my $directory = './zones';
my %zones = getZoneHash($directory);
print "$_ -> $zones{$_}\n" for keys %zones;
sub getZoneHash {
my ($dir) = @_;
my %hash;
local $/;
for my $file ( grep -f, <"$dir/*"> ) {
open my $fh, '<', $file or die $!;
my $data = <$fh>;
close $fh;
$hash{$1} = $2
if $data =~ /zone\s+"([^"]+)".+masters\s+{\s*([^}\s]+)\s*}/s;
}
Kg
return %hash;
}
####
zone "zone.com" {
type slave;
file "path/to/db.zone";
masters {ip.add.ress.here};
};
zone "cnn.com" {
type slave;
file "path/to/db.zone";
masters { cnn.add.ress.here };
};
zone "perlmonks.org" {
type slave;
file "path/to/db.zone";
masters {
ip.perlmonks.ress.here
};
};
####
cnn.com -> cnn.add.ress.here
perlmonks.org -> ip.perlmonks.ress.here
zone.com -> ip.add.ress.here
####
/zone\s+"([^"]+)".+masters\s+{\s*([^}\s]+)\s*}/s
^ ^ ^ ^ ^ ^ ^ ^ ^
| | | | | | | | |
| | | | | | | | + - Treat string as single line
| | | | | | | + - 0+ whitespaces
| | | | | | + - Match anything that's not } or a whitespace
| | | | | + - 0+ whitespaces
| | | | + - 1+ whitespaces
| | | + - End quotation
| | + - Match anything that's not a "
| + - Begin quotation
+ - 1+ whitespaces