use Tie::File::AnyData; my $coderef = sub { ## Code to retrieve one by one the records from a file (one record per call) }; tie my @data, 'Tie::File::AnyData', $file, code => $coderef; ## Use the tied array untie @data;