How do I read the contents of a small text file into a scalar in Perl?

后端 未结 8 777
日久生厌
日久生厌 2021-01-17 17:39

I have a small text file that I\'d like to read into a scalar variable exactly as it is in the file (preserving line separators and other whitespace).

The equivalent

相关标签:
8条回答
  • 2021-01-17 17:55

    I'd tweak draegtun's answer like this, to make it do exactly what was being asked:

    my $buffer;
    if ( open my $fh, '<', 'fileName' ) {
        $buffer = do { local $/; <$fh> };
        close $fh;
    } else {
        $buffer = 'The file could not be opened.';
    }
    
    0 讨论(0)
  • 2021-01-17 17:59

    I don't have enough reputation to comment, so I apologize for making this another post.

    @ Harold Bamford: $/ should not be an obscure variable to a Perl programmer. A beginner may not know it, but he or she should learn it. The join method is a poor choice for the reasons stated in the article linked by hackingwords above. Here's the relevant quotation from the article:

    That needlessly splits the input file into lines (join provides a list context to ) and then joins up those lines again. The original coder of this idiom obviously never read perlvar and learned how to use $/ to allow scalar slurping.

    0 讨论(0)
  • 2021-01-17 17:59

    You could do something like:

    $data_file="somefile.txt";
    open(DAT, $data_file);
    @file_data = <DAT>;
    close(DAT);
    

    That'll give you the file contents in an array, that you can use for whatever you want, for example, if you wanted each individual line, you could do something like:

    foreach $LINE (@file_data)
    {
        dosomethingwithline($LINE);
    }
    

    For a full usage example:

    my $result;
    $data_file = "somefile.txt";
    my $opened = open(DAT, $data_file);
    if (!$opened)
    {
        $result = "Error.";
    }
    else
    {
        @lines = <DAT>;
        foreach $LINE (@lines)
        {
            $result .= $LINE;
        }
        close(DAT);
    }
    

    Then you can use $result however you need. Note: This code is untested, but it should give you an idea.

    0 讨论(0)
  • 2021-01-17 18:02

    From the Perl Cookbook:

    my $filename = 'file.txt';
    open( FILE, '<', $filename ) or die 'Could not open file:  ' . $!;
    
    undef $/;
    my $whole_file = <FILE>;
    

    I would localize the changes though:

    my $whole_file = '';
    {
        local $/;
        $whole_file = <FILE>;
    }
    
    0 讨论(0)
  • 2021-01-17 18:06

    As an alternative to what Alex said, you can install the File::Slurp module (cpan -i File::Slurp from the command line) and use this:

    use File::Slurp;
    # Read data into a variable
    my $buffer = read_file("fileName");
    # or read data into an array
    my @buffer = read_file("fileName");
    

    Note that this dies (well... croaks, but that's just the proper way to call die from a module) on errors, so you may need to run this in an eval block to catch any errors.

    0 讨论(0)
  • 2021-01-17 18:07

    If I don't have Slurp or Perl6::Slurp near by then I normally go with....

    open my $fh, '<', 'file.txt' or die $!;
    
    my $whole_file = do { local $/; <$fh> };
    
    0 讨论(0)
提交回复
热议问题