my $get_data_sub = sub { my ($offset, $rows) = @_; my @return_array; my $sth = $dbh->prepare('SELECT id, title FROM resource WHERE id = ?'); for (my $x = 0; $x < $rows; $x++) { my $filename = substr($files[$offset + $x], 0, -4); $sth->execute($filename); # Note I assume that the ID is unique (no need for a while loop - only # one record will be returned) otherwise the paging really doesn't work. $ref = $sth->fetchrow_hashref(); push @return_array, { ID => $ref->{id}, TITLE => $ref->{title} }; $sth->finish; } return \@return_array; }