Просто используйте следующую функцию.
for $line (@lines) {
if ($line =~ m/ImportantLineNotToBeChanged/){
#break out of the for loop, move onto the next line of the file being processed
#start the loop again
next;
}
if ($line =~ s/SUMMER/WINTER/g){
print ".";
}
}
Аналогично, вы можете использовать «последний» для завершения цикла.Например:
for $line (@lines) {
if ($line =~ m/ImportantLineNotToBeChanged/){
#continue onto the next iteration of the for loop.
#skip everything in the rest of this iteration.
next;
}
if ($line =~ m/NothingImportantAFTERThisLine/){
#break out of the for loop completely.
#continue to code after loop
last;
}
if ($line =~ s/SUMMER/WINTER/g){
print ".";
}
}
#code after loop
Редактировать: 7 вечера 6/13
Я взял ваш код и посмотрел его, переписал некоторые вещи, и вот что я получил:
sub changeSeason2 {
my $file= $_[0];
open (FILE,"<$file");
@lines = <FILE>;
close FILE;
foreach $line (@lines) {
if ($line =~ m/'?Don't touch this line'?/) {
next;
}
if ($line =~ m/'?Or this line'?/){
next;
}
if ($line =~ m/'?Or this line too'?/){
next;
}
if ($line =~ m/\'Or this line as well\'/){
next;
}
if ($line =~ s/(WINTER)/{$1 eq 'winter' ? 'summer' : 'SUMMER'}/gie){
print ".";
}
}
print "\nSeason changed in file $file";
open FILE, ">$file";
print FILE @lines;
close FILE;
}
Надеюсь, это поможет некоторым.