【发布时间】:2011-02-01 04:07:12
【问题描述】:
我有一些文档,我想在每行的开头和结尾添加一些内容。原始文档如下所示:
firstLine
secondline
我想把它变成这样:
put 'firstLine';
put 'secondline';
通过使用下面的 Perl 脚本,我只能把它变成这样:
put 'firstLine';
';put 'secondline';
第一行的末尾和第二行的开头好像有一个$。有人可以帮我弄清楚以下 Perl 脚本有什么问题吗?
use File::Find;
use strict;
my ($filename, @lines, $oldterm, $newterm); #,$File::Find::name);
my $dir = ".";
open MYFILE, ">error.txt" or die $!;
find(\&edits, $dir);
sub edits() {
$filename = $File::Find::name;
if (grep(/\.txt$/, $filename)) { #only process the perl files
# open the file and read data
# die with grace if it fails
open(FILE, "<$filename") or die "Can't open $filename: $!\n";
@lines = <FILE>;
close FILE;
# open same file for writing, reusing STDOUT
open(STDOUT, ">$filename") or die "Can't open $filename: $!\n";
# walk through lines, putting into $_, and substitute 2nd away
for (@lines) {
s/(&.+)/' "$1" '/ig;
s/^/put '/ig;
s/$/';/ig;
print;
}
#Finish up
close STDOUT;
}
}
【问题讨论】: