I created a function to read lines from an file into chunks. My hidden agenda in creation this script was in python the yield function in interaction with chunks.The script works fine, but now i want to know if anyone has improvements?#!/usr/bin/env perluse strict;use warnings;use Data::Dumper;sub read_in_chunks {    my $args = shift;    my $self = {        fd => $args->{fd} || undef,        chunk_size => $args->{chunk_size} || 10,        chunks => [],    };    my $fh = $self->{fd};    return unless defined(my $line=<$fh>);    while(<$fh>){        chomp($_);        # maybe the following line could be written nicer :)        ($self->{chunk_size} == 0) ? return $self->{chunks} : (push @{$self->{chunks}}, $_);        $self->{chunk_size}--;    }    return $self->{chunks};}open my $fh, 'dump.txt' or die $!;my $opts = {    fd => $fh,    chunk_size => 4};while(my $chunk = read_in_chunks($opts)) {    print Dumper($chunk);    # process data}close $fh;
Read lines from a file in chunks
file;iterator;perl;generator
Here's my take. I removed the $self structure, as it gives you no advantage, and fixed the problem with missing lines.#! /usr/bin/perluse warnings;use strict;use Data::Dumper;sub read_in_chunks {    my %args = @_;    my $fh = $args{fh} or die No filehandle given.\n;    my $size = $args{chunk_size} || 10;    my @chunk;    while (@chunk < $size && defined(my $line = <$fh>)) {        chomp $line;        push @chunk, $line;    }    return @chunk}open my $fh, shift or die $!;my %opts = (    fh         => $fh,    chunk_size => 4);while (my @chunk = read_in_chunks(%opts)) {    print Dumper(\@chunk);    # ...}