my $string = slurp($file1) . slurp($file2); sub slurp { my $file = shift; open $fh, <, $file or do { warn "Failed to open $file for input: $!\n"; return ''; } local $/ = undef; return <$fh>; # $fh goes out of scope, file closed automatically }