There might be a few other ways to do it, but for automatic edits of my precious perl code, this is the sort of approach I would prefer. (This could be done more compactly, but again, it's worth typing a little more, just to be clear and simple.)opendir(D,$path); my @perlfiles = grep /\w+\.pl$/, readdir(D); # (or whatever works for +you) closedir D; for my $file (@perlfiles) { my $name = "$path/$file"; unless (open(P,$name)) { warn "what happened to $name? $!"; next; } my @script = <P>; close P; my $newsize = 0; my $badedit = 0; for (@script) { my $oldlen = length(); if ( $oldlen ) { # do your edits here, and: my $newlen = length() unless ( $newlen ) { # maybe you would check other stuff... warn "Oops -- bad edit for $name; check $name.new\n"; $badedit++; } $newsize += $newlen; } } unless (open(P,">$name.new")) { die "can't seem to open a new version of $name: $!"; } print P for (@script); close P; if ( -s "$name.new" == $newsize and not $badedit ) { rename "$name.new", $name; } else { die "couldn't write a complete edited version of $name\n"; } }
If you didn't close the input file before writing the edited output, if you didn't use at least some amount of error checking, etc, well, live and learn...
In reply to Re: Changing lots of files...
by graff
in thread Changing lots of files...
by arrow
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |