my @_STORAGE; sub recursive_store { $query[$level] = "SELECT ..."; my $sth = $dbh->prepare($query[$level]); $sth->execute; $_STORAGE[$level]= $sth->fetchall_arrayref; $sth->finish(); } &recursive_store($level); # Example access: print _STORAGE[$level][$row][$col]; # Show what we have: use Data::Dumper; print Dumper \@_STORAGE;