Hello everybody is it possible to create in perl an file generator like the conecpt in python.
Actuall i have the following code snippet to read chunks from an file:
#!/usr/bin/perl
use strict;
use warnings;
sub getChunck {
my $file = shift;
my @chunck;
open my $fh, '<', $file or die "Unable to open '$file': $!";
while(<$fh>) {
push @chunck, $_;
push @chunck, "\n" unless $. % 4;
}
return @chunck;
}
my @chunck = getChunck('DUMP');
I read the hole file in chunks into an array. But i want to know if it is possible to read an chunk or data from file and then process it (like in python with yield).
I hope someone could help me to create an "yield" function in perl. I know there are some libs to work with generators in perl, but i dont want to include an lib. (maybe it is not possible?)
Thanks to everybody.
Maybe it could works so:
- open file and read line for line
- yield line
- process the line with another function.
Thanks.