From 9299c46fdf9cd4bd5b6d1afb0ebdeb1dac4a86ee Mon Sep 17 00:00:00 2001 From: Thomas Bloor Date: Tue, 10 Sep 2019 09:29:17 +0100 Subject: [PATCH] reduce memory usage importing CSV files --- lib/Pear/LocalLoop/Import/LCCCsv/Postcodes.pm | 4 +--- lib/Pear/LocalLoop/Import/Role/CSV.pm | 5 +++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/Pear/LocalLoop/Import/LCCCsv/Postcodes.pm b/lib/Pear/LocalLoop/Import/LCCCsv/Postcodes.pm index c1efb5f..4068e01 100644 --- a/lib/Pear/LocalLoop/Import/LCCCsv/Postcodes.pm +++ b/lib/Pear/LocalLoop/Import/LCCCsv/Postcodes.pm @@ -15,9 +15,7 @@ has '+csv_required_columns' => ( sub import_csv { my ($self) = @_; - my $rows = $self->csv_data; - - foreach my $row ( @{$rows} ) { + while ( my $row = $self->get_csv_line ) { $self->_row_to_result($row); } } diff --git a/lib/Pear/LocalLoop/Import/Role/CSV.pm b/lib/Pear/LocalLoop/Import/Role/CSV.pm index 110252f..954d986 100644 --- a/lib/Pear/LocalLoop/Import/Role/CSV.pm +++ b/lib/Pear/LocalLoop/Import/Role/CSV.pm @@ -67,6 +67,11 @@ has csv_data => ( } ); +sub get_csv_line { + my $self = shift; + return $self->_text_csv->getline_hr( $self->_csv_filehandle ); +} + sub check_headers { my $self = shift; my $req_headers = $self->csv_required_columns;