Save yourself effort and use one of the "Slurp" modules, given that Perl 6 is to support even smarter things. These
let you read all file content into a single variable.
Less code to go wrong.
Thus, one fragment might read:
use File::Slurp:
# get one file here, as $file
my($bakfile, $file, $text);
# maybe set up file renaming with something like this...
($bakfile = $file ) =~ s/\.([^.]+)$/\.bak/;
# then, the main bit
$text = read_file($file);
$text =~ s/this/that/smg;
rename($file, $bakfile);
write_file($file, $text);
Another possible version something like:
use Perl6::Slurp; #<= only handles input at present
use File::Slurp; # permits writing.
# and then....
....
$text = slurp($file);
$text =~ s/$this/$that/smg;
... with renaming here ? (see above).
write_file($file, $text);
And BTW do you really need that chomp and/or intend to remove newlines?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.