
하나의 파일에 대해서만 제대로 작동하는 xml 파일을 수정하기 위해 Perl 스크립트를 작성했습니다. 내 스크립트가 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";
}
}