I am reading a file into a SCALAR
by using:
open FILE, "<", $filename or error_out("Error opening $filename");
read FILE, my $buffer, -s $filename;
close FILE;
I am not sure how to think of the "new" $buffer
/SCALAR
type. Is it a string? An array? How to go through it line-by-line?
Or if you're not specifically attached to reading the whole file into memory all at once:
First, I recommend you use the following to read the file:
Note the removal of the useless and potentially incorrect
-s
. Note the use of a lexical variable for the file handle rather than a global one. Note the inclusion of the underlying error message in the error message.The variable is a scalar. The value is a string.
But why not just read it a line at a time.
It is unusual to see
read
used in a Perl program. Most oftenreadline FILE
(or the equivalent<FILE>
) is more appropriate.If you don't need the entire file in memory at once then you can iterate over the lines using
or if you would rather have the whole file in an array of lines then you can do
The regular method of reading a whole file into a single string is to undefine
$/
- the input file separator - like thisbut that is generally much less useful than the first two options.