Perl has lots of handy idioms for doing common things easily, including:
The file-reading operator
<HANDLE>
, if used without a filehandle, will seamlessly open and read all files in@ARGV
, one line at a time:while (<>) { print $., $_; }
By (locally) resetting the input record separator
$/
, I can "slurp" a whole file at once:local $/; $content = <HANDLE>;
But these two idioms don't work quite work together as I expected: After I reset $/
, slurping with <>
stops at end of file: The following gives me the contents of the first file only.
local $/; $content = <>;
Now, I trust that this behavior must be by design, but I don't really care. What's the concise Perl idiom for fetching all content of all files into a string? (This related question mentions a way to slurp a list of files into separate strings, which is a start... but not terribly elegant if you still need to join the strings afterwards.)