in reply to Searching and Replacing file content within directory

Even on Windows, you can use / instead of \ for path separators. Makes things juts a bit more readable.

That said, you should not be modifying files in-place. It's dangerous and not easy to do right. (Tie::File might make it doable.)

Normal behaviour is: a) rename file to $_.old; b) open $_.old for read and $_ for write; c) loop through read-handle : modify line, write line; d) close filehandles; e) possibly delete old file (if you don't, then deleting it before (a) is a good thing, just in case it's still around).

Note that as far as I can see, you're not writing anything back to the file, which, as I said, would be difficult to get correct anyway, so don't do it. But I wouldn't expect ANY files to be updated with your code.

  • Comment on Re: Searching and Replacing file content within directory

Replies are listed 'Best First'.
Re^2: Searching and Replacing file content within directory
by MishaMoose (Scribe) on Nov 12, 2010 at 16:15 UTC

    and just for completeness .. and becuase I, have made this oversight ...

    c.1) : read AND write the remaining lines from the input file. d) Then close the filehandles

    I expect that most otherfolk are more attnetive than this most of the time. BUT using myslef as a bench mark ... better cautionary than assumptive.

    Misha/Michael - Russian student, grognard, bemused observer of humanity and self professed programmer with delusions of relevance