in reply to Contents of a file into a scalar

As Kenosis said you're doing too much work. File::Slurp does it all for you:

use File::Slurp 'read_file'; my $string = read_file($filename);

In the absence of File::Slurp, the best way is:

my $string = do { open my $fh, '<', $filename; local $/; <$fh>; };

Or even:

my $string = do { local (@ARGV, $/) = $filename; <> };

... though that last one can boggle some readers' minds.

You can read about the magic $/ variable in perlvar, but the short story is: $/ is the string that Perl considers to be the line ending when it's reading a file line-by-line; when $/ is undef, Perl will consider the whole file to be a single line, so <$fh> (which normally reads a single line from $fh) will read the whole file.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name