记录.txt
name: shimmer
addr: 192.168.1.11
owner: David Davis
building: main
-=-
name: bendir
addr: 192.168.1.3
owner: cindy Coltrane
building: west
-=-
name: sulawesi
addr: 192.168.1.12
owner: Ellen Monk
building: main
-=-
name: sander
addr: 192.168.1.55
owner: Alex rollins
building: east
数据库.pl
my $datafile = 'record.txt';
my $recordsep = "-=-\n";
open my $DATAFILE, '<', "$datafile" or die "unable to open datafile:$!\n";
{
local $/= $recordsep; #prepare to read in database file one record at a time
print "#\n# host file = GENERATED BY $o\n$ DO NOT EDIT BY HAND!\n#\n";
my %record;
while(<$DATAFILE>) {
chomp; #remove the record separator
#split into key1,value1, ....bingo, hash of record
%record = split /:\s*|\n/;
print "$record{addr}\t$record{name} $record{building} \n";
}
close $DATAFILE;
}
我有几个关于这个 perl 代码的问题。
{ }
将代码放在行后的大括号中有什么意义open
?- 这样做有什么意义
local $/= $recordsep;
?local
与my
? $o\n$
行中的意思是什么print "#\n# host file = GENERATED BY $o\n$ DO NOT EDIT BY HAND!\n#\n";