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 =
; 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"; } }