
我編寫了一個 perl 腳本來修改 xml 文件,該腳本僅適用於一個文件。我希望我的腳本獲取 xml 資料夾並修改其中的 xml 檔案。就像這樣,它應該針對 xml 資料夾中的所有 xml 資料運行。那我怎麼才能實現它呢。
這是我的程式碼。
open(FILE, "/home/AP/abc.xml") || die "File not found";
my @lines = <FILE>;
close(FILE);
my @newlines;
foreach(@lines) {
$_ =~ s/<abc>/$&\n<!--a-->\n<!--b-->\n<!--c-->/g ;
s/hai/bye/g;
s/---/--/g;
s/***/**/g;
push(@newlines,$_);
}
open(FILE, "/home/AP/abc.xml") || die "File not found";
print FILE @newlines;
close(FILE);
答案1
實際上,您可以透過多種方式實現這一目標......只需嘗試以下程式碼,並讓我知道是否有任何說明或出現任何錯誤。
sub fileprocessor{
(my $file_name)=@_;
my $tmp_filename="/home/AP/$file_name";
open(FILE, "$tmp_filename") || die "File not found";
my @lines = <FILE>;
close(FILE);
my @newlines;
foreach(@lines) {
$_ =~ s/<abc>/$&\n<!--a-->\n<!--b-->\n<!--c-->/g ;
s/hai/bye/g;
s/---/--/g;
s/***/**/g;
push(@newlines,$_);
}
open(FILE, "$tmp_filename") || die "File not found";
print FILE @newlines;
close(FILE);
}
foreach(<*>){
if(-f $_){
fileprocessor($_);
}else{
print "directory\n";
}
}