How to use SCALAR to read line-by-line after reading in a large file (Perl) ?

浪尽此生 提交于 2019-12-13 02:24:40

问题


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.



来源:https://stackoverflow.com/questions/22155010/how-to-use-scalar-to-read-line-by-line-after-reading-in-a-large-file-perl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!