Stranger95
Stranger95

Reputation: 173

Am I right that Perl will read file content straight into memory?

It can be quite obvious but I'd like to consult with you..

So I have the following perl module:

use strict;
use warnings;
use File::Slurp;


my @list = read_file_subroutine();

my %hash = map {$_ => 1} @list;

sub does_list_contain_smth() {
    my ($self, $data) = @_;
    if ($hash($data)) {
       return 1;
    }
    return 0;
}

sub get_list() {
    return \@list;
}

sub read_file_subroutine {
    my $file = "/some/file/path/file.txt";
    my $content = read_file($file);
    my @list = split "\n", $content;
    return \@list

1;

Am I right that read_file_subroutine will be called only once and file content will be placed into memory?

Upvotes: 0

Views: 170

Answers (1)

ikegami
ikegami

Reputation: 385897

Yes.

read_file_subroutine will only be called once. There's only one call to it, it's not in any kind of loop, and it's not a sub that's called more than once.

read_file does read the entire file into memory. The first sentence of its documentation:

This function reads in an entire file and returns its contents to the caller

Upvotes: 1

Related Questions