my @rows; while (my $row = $sth->fetch()) { push @rows, [ @$row ]; ... } for my $row (@rows) { ... }