How to use SCALAR to read line-by-line after readi

2019-07-26 17:25发布

问题:

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?

回答1:

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.


  1. The variable is a scalar. The value is a string.

  2. 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>) {
        ...
    }
    


回答2:

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);


回答3:

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.