Read lines from a file in chunks 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 perl
use strict;
use warnings;
use Data::Dumper;


sub read_in_chunks {
    my $args = shift;
    my $self = {
        fd =&gt; $args-&gt;{fd} || undef,
        chunk_size =&gt; $args-&gt;{chunk_size} || 10,
        chunks =&gt; [],
    };
    my $fh = $self-&gt;{fd};
    return unless defined(my $line=&lt;$fh&gt;);
    while(&lt;$fh&gt;){
        chomp($_);
        # maybe the following line could be written nicer :)
        ($self-&gt;{chunk_size} == 0) ? return $self-&gt;{chunks} : (push @{$self-&gt;{chunks}}, $_);
        $self-&gt;{chunk_size}--;
    }
    return $self-&gt;{chunks};
}

open my $fh, 'dump.txt' or die $!;

my $opts = {
    fd =&gt; $fh,
    chunk_size =&gt; 4
};

while(my $chunk = read_in_chunks($opts)) {
    print Dumper($chunk);
    # process data
}

close $fh;
Here's my take. I removed the $self structure, as it gives you no advantage, and fixed the problem with missing lines.

#! /usr/bin/perl
use 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 &lt; $size &amp;&amp; defined(my $line = &lt;$fh&gt;)) {
        chomp $line;
        push @chunk, $line;
    }
    return @chunk
}

open my $fh, shift or die $!;

my %opts = (
    fh         =&gt; $fh,
    chunk_size =&gt; 4
);

while (my @chunk = read_in_chunks(%opts)) {
    print Dumper(\@chunk);
    # ...
}