Простое чтение файла в массив, по одной строке на элемент, тривиально:
open my $handle, '<', $path_to_file;
chomp(my @lines = <$handle>);
close $handle;
Теперь строки файла находятся в массиве @lines
.
Если вычтобы убедиться, что есть обработка ошибок для open
и close
, сделайте что-то подобное (в приведенном ниже примере мы открываем файл в режиме UTF-8 тоже):
my $handle;
unless (open $handle, "<:encoding(utf8)", $path_to_file) {
print STDERR "Could not open file '$path_to_file': $!\n";
# we return 'undefined', we could also 'die' or 'croak'
return undef
}
chomp(my @lines = <$handle>);
unless (close $handle) {
# what does it mean if close yields an error and you are just reading?
print STDERR "Don't care error while closing '$path_to_file': $!\n";
}