This is difficult to make efficient, simply because you have to read in the full contents of every file to perform the replacement operation. Not to mention that you might wind up replacing the wrong thing! Here are some guidlines you can use to get you started, at least:

  1. Use File::Find to get a list of files. This verily automates the process of recursing through a directory tree and extracting a specific list of filenames. You can even read this for an example I wrote earlier today.
  2. Use `local()' to modify the value of $/ (the input line separator), ala local($/) = undef. This will allow you to slurp in a whole file into a scalar, then you can `study()' that scalar, perform a regex, and dump it back out to a file.
  3. Make backups. All the decent "Cross-File Find & Replace" engines at least give you the options of making backups. A simple system('cp', $filename, $filename .".bak") should do the trick.
  4. Good luck!

    Alakaboo


    In reply to Re: Replacing one string within a file with another string by mwp
    in thread Replacing one string within a file with another string by SuedeBlur

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



  5. Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  6. Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  7. Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  8. Please read these before you post! —
  9. 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
  10. 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;
  11. Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  12. See Writeup Formatting Tips and other pages linked from there for more info.