你会如何在perl中执行以下操作:
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 } if ($line =~ s/SUMMER/WINTER/g){ print "."; } }
sub ChangeSeason(){ if (-f and /.log?/) { $file = $_; open FILE,$file; @lines = <FILE>; close FILE; for $line (@lines) { if ($line =~ m/'?Don't touch this line'?/) { last; } if ($line =~ m/'?Or this line'?/){ last; } if ($line =~ m/'?Or this line too'?/){ last; } if ($line +~ m/'?Or this line as well'?/){ last; } if ($line =~ s/(WINTER)/{$1 eq 'winter' ? 'summer' : 'SUMMER'}/gie){ print "."; } } print "\nSeason changed in file $_"; open FILE,">$file"; print FILE @lines; close FILE; } }
解决方法
只需使用
next
for my $line (@lines) { next if ($line =~ m/ImportantLineNotToBeChanged/); if ($line =~ s/SUMMER/WINTER/g){ print "."; } }