Hello TonyNY,
Fellow Monks have provided you with a solution to your problem. Just for fun you can use also the module File::Slurp.
Sample code:
#!/usr/bin/perl
use strict;
use warnings;
use File::Slurp qw(read_file write_file);
my $filename = 'in.txt';
my $data = read_file $filename, {binmode => ':utf8'};
$data =~ s/The action failed\./failed_build/g;
write_file $filename, {binmode => ':utf8'}, $data;
__END__
$ cat in.txt
one
two
The action failed.
three
The action failed. twice The action failed.
four
$ cat in.txt
one
two
failed_build
three
failed_build twice failed_build
four
As fellow Monk choroba said File::Slurp is broken and wrong, instead you can use the File::Slurper.
Sample of code:
#!/usr/bin/perl
use strict;
use warnings;
use File::Slurper qw(read_text write_text);
my $filename = 'in.txt';
my $data = read_text($filename);
$data =~ s/The action failed\./failed_build/g;
write_text($filename, $data);
__END__
$ cat in.txt
one
two
The action failed.
three
The action failed. twice The action failed.
four
$ cat in.txt
one
two
failed_build
three
failed_build twice failed_build
four
Or mu personal favorite IO::All, sample of code:
#!/usr/bin/perl
use strict;
use warnings;
use IO::All -utf8; # Turn on utf8 for all io
my $contents = io->file('in.txt')->slurp;
$contents =~ s/The action failed\./failed_build/g;
$contents > io('in.txt');
__END__
$ cat in.txt
one
two
The action failed.
three
The action failed. twice The action failed.
four
$ cat in.txt
one
two
failed_build
three
failed_build twice failed_build
four
BR / Thanos
Seeking for Perl wisdom...on the process of learning...not there...yet!
-
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.