Granting that none of your perl script files is ridiculously huge, something like this should have worked (though I haven't tested this example):
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"; } }
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.)

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.