Perl Language Reading a file's content into a variable Path::Tiny


Example

Using the idiom from The Manual Way several times in a script soon gets tedious so you might want to try a module.

use Path::Tiny;
my $contents = path($filename)->slurp;

You can pass a binmode option if you need control over file encodings, line endings etc. - see man perlio:

my $contents = path($filename)->slurp( {binmode => ":encoding(UTF-8)"} );

Path::Tiny also has a lot of other functions for dealing with files so it may be a good choice.