我正在尝试修改配置文件。
我首先将它读入@buffer
,这取决于正则表达式匹配。修改后的缓冲区被写回到磁盘上,以防文件变小,截断完成。
不幸的是,这不起作用,它已经崩溃了fseek
,但据我所知,我对 fseek 的使用符合 perl doc。
open (my $file, "+<", "somefilethatexists.txt");
flock ($file, LOCK_EX);
foreach my $line (<$file>) {
if ($line =~ m/(something)*/) {
push (@buffer, $line);
}
}
print "A\n";
seek($file,0,0); #seek to the beginning, we read some data already
print "B\n"; # never appears
write($file, join('\n',@buffer)); #write new data
truncate($file, tell($file)); #get rid of everything beyond the just written data
flock($file, LOCK_UN);
close ($file);