人们一直在问这个问题,我一直用perlfaq5的相同答案回答这个问题.现在,我们可以在Stackoverflow上指出这一点.
我想在文件的顶部和底部添加一行.我可以按照以下方式做到.
open (DATA, "</usr/old") || die "cant open old\n"; #file to which line has to be added
my @body=<DATA>;
close(DATA);
open (FILE, ">/usr/new") || die "cant open new\n"; #file after stuff has been added
print FILE "9 431";
print FILE "\n";
my $body=@body;
for (my $i=0; $i<$body;$i++){
print FILE "$body[$i]";#not using for loop leads to addition of spaces in new file
}
print FILE "(3,((((1,4),(7,6)),(2,8)),5),9)";
Run Code Online (Sandbox Code Playgroud)
由于我运行大量文件,因此这个过程非常耗时.Perl是否具有用于在文件顶部和底部添加行的任何特定功能?