record.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
database.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
vsmy
? 什麼是訂單
print "#\n# host file = GENERATED BY $o\n$ DO NOT EDIT BY HAND!\n#\n";
注意:將'local'放在curlies而不是'open'中是沒有意義的。 – ikegami