Congratulations! Your start-up company was just bought by super large corporation. You now need to replace Copyright Start-Up by Copyright Large Corporation in the README.txt file

If you need to do this as part of a larger application then go on reading this article. On the other hand, if this is a stand-alone work then you can do this with a one-liner replacing a string in a file.

Using Path::Tiny

If you can install Path::Tiny and if the file is not too large to fit in the memory of your computer, then this can be the solution:

use strict;
use warnings;

use Path::Tiny qw(path);

my $filename = 'README.txt';

my $file = path($filename);

my $data = $file->slurp_utf8;
$data =~ s/Copyright Start-Up/Copyright Large Corporation/g;
$file->spew_utf8( $data );

The path function imported from Path::Tiny accepts a path to a file and returns an object that can be used for all kinds of interesting things. The slurp_utf8 method will read in the content of the file (after opening it with UTF8 encoding) and return all the content as a single string.

The s/// substitution does the string replacement. It uses the /g global flag to replace all the occurrences.

The spew_utf8 method will write out the string passed to it to the underlying file, replacing all the content.


This is an older version of it. It is less preferable than the Path::Tiny one, but if you already have File::Slurp installed and then this can be the solution:

use strict;
use warnings;

use File::Slurp qw(read_file write_file);

my $filename = 'README.txt';

my $data = read_file $filename, {binmode => ':utf8'};
$data =~ s/Copyright Start-Up/Copyright Large Corporation/g;
write_file $filename, {binmode => ':utf8'}, $data;

The read_file function of File::Slurp will read the whole file into a single scalar variable. This assumes the file is not too big.

We set binmode => ':utf8' to correctly handle Unicode characters. Then a regex substitution is used with the /g modifier to globally replace all the occurrences of the old text by the new text.

Then we save the content in the same file, again using binmode => ':utf8' to handle Unicode characters correctly.

Replace content with pure Perl

If you cannot install File::Slurp you can implement a limited version of its function. In this case, the main body of the code is almost the same, except that we don't pass the parameters to open the file in Unicode mode. We have that coded in the functions themselves. You can see how it is done in the calls to open.

use strict;
use warnings;

my $filename = 'README.txt';

my $data = read_file($filename);
$data =~ s/Copyright Start-Up/Copyright Large Corporation/g;
write_file($filename, $data);

sub read_file {
    my ($filename) = @_;

    open my $in, '<:encoding(UTF-8)', $filename or die "Could not open '$filename' for reading $!";
    local $/ = undef;
    my $all = <$in>;
    close $in;

    return $all;

sub write_file {
    my ($filename, $content) = @_;

    open my $out, '>:encoding(UTF-8)', $filename or die "Could not open '$filename' for writing $!";;
    print $out $content;
    close $out;


The read_file function we set the $/ variable (which is also called $INPUT_RECORD_SEPARATOR) to undef. This is what is usually referred to as slurp mode. It tells the "read-line" operator of Perl to read in the content of all the file into the scalar variable on the left-hand-side of the assignment: my $all = <$in>;. We even used the local keyword when we set $/ so this change will be reverted once we exit the enclosing block - in this case, once we leave the read_file function.

The write_file function is much more straight forward and we put it in a function only to make the main body of the code similar to the previous solution.