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?
First, I recommend you use the following to read the file:
my $buffer = do {
open(my $fh, '<', $filename)
or error_out("Error opening $filename: $!");
local $/;
<$fh>
};
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.
for my $line (split(/^/m, $buffer)) {
...
}
But why not just read it a line at a time.
open(my $fh, '<', $filename)
or error_out("Error opening $filename: $!");
while (my $line = <$fh>) {
...
}
for my $line (split(/\n/, $buffer)
{
# do whatever with $line
}
Or if you're not specifically attached to reading the whole file into memory all at once:
open(FILE, "<", $filename) or error_out("Error opening $filename");
while (<FILE>)
{
# do whatever with $_
}
close(FILE);
It is unusual to see read
used in a Perl program. Most often readline 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
while (my $line = <FILE>) {
# Process $line
}
or if you would rather have the whole file in an array of lines then you can do
my @lines = <FILE>;
The regular method of reading a whole file into a single string is to undefine $/
- the input file separator - like this
local $/;
my $contents = <FILE>;
but that is generally much less useful than the first two options.