Read the entire contents of the file
How can I read the contents of a file in bulk with Perl? By convention, the contents of a file are often read at once, with the following description:
my $file = 'a.txt'; open my $fh, '<', $file or die "Can't open $file:$!"; # Read the contents of the file at once my $content = do {local $/; <$fh>};
do block returns the last evaluated value.
local undefined predefined variable "$/" that represents a line break in the file.
This causes file input operator "<$fh>" to return the contents of the entire file.
And when the scope ends, "$/" returns to its original state.
Keep in mind that Perl, which doesn't have a builtin functions to read a file at once, may read the entire contents of the file in this way.