I have a small text file that I\'d like to read into a scalar variable exactly as it is in the file (preserving line separators and other whitespace).
The equivalent
I'd tweak draegtun's answer like this, to make it do exactly what was being asked:
my $buffer; if ( open my $fh, '<', 'fileName' ) { $buffer = do { local $/; <$fh> }; close $fh; } else { $buffer = 'The file could not be opened.'; }