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:
in reply to Corrector
Another possible version something like:
# 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;
And BTW do you really need that chomp and/or intend to remove newlines?
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).