Tie::Array::File::LazyRead - Read a file record by record using tied array and for()
This document describes version 0.001 of Tie::Array::File::LazyRead (from Perl distribution Tie-Array-File-LazyRead), released on 2019-04-16.
Given FILENAME.txt containing:
line1 line2 line3
Then this Perl script:
use Tie::Array::File::LazyRead; tie my @ary, 'Tie::Array::File::LazyRead', 'FILENAME.txt', {accumulate=>1}; # default for accumulate is 0 for my $line (@ary) { print $line; }
will print:
and @ary containing:
@ary
["line1", "line2", "line3"]
If accumulate is set to 0 (the default), @ary will contain:
accumulate
[undef, undef, "line3"]
(i.e. only the last element/line will be remembered.
EXPERIMENTAL, PROOF-OF-CONCEPT.
When for() is given a tied array:
for()
for (@tied_array) { ... }
it will invoke FETCHSIZE on the tied array to find out the size, then FETCH(0), then FETCHSIZE() again, then FETCH(1), and so on. In other words, FETCHSIZE is called on each iteration. This makes it possible to only fetch new data in FETCHSIZE instead of FETCH.
FETCHSIZE
FETCH
Without using for():
tie my @ary, 'Tie::Array::File::LazyRead', 'FILENAME.txt'; print $ary[0];
will not print anything, and the first line of the file is not fetched. To fetch one more line, you need to do:
my $size = @ary; # then print $ary[0];
and so on.
Please visit the project's homepage at https://metacpan.org/release/Tie-Array-File-LazyRead.
Source repository is at https://github.com/perlancar/perl-Tie-Array-File-LazyRead.
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Tie-Array-File-LazyRead
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
perlancar <perlancar@cpan.org>
This software is copyright (c) 2019 by perlancar@cpan.org.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Tie::Array::File::LazyRead, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Tie::Array::File::LazyRead
CPAN shell
perl -MCPAN -e shell install Tie::Array::File::LazyRead
For more information on module installation, please visit the detailed CPAN module installation guide.