my $dumper = Data::Dumper->new(); $dumper->Terse(1); $dumper->Indent(0); my $sth = $dbh->prepare( 'SELECT * FROM table' ); $sth->execute(); # open a file to track all ids my $id_file_name = 'all-ids.txt'; open my $id_fh, "> $id_file_name" or die "opening $id_file_name: $!" # array position of primary key my $id_ix = 0; while ( my $cur = $sth->fetch() ) { my $id = $cur->[$id_ix]; open my $fh, "> $id" or { do warn "opening $id: $!"; next }; print $fh $dumper->($cur); close $fh or warn "closing $id: $!"; print $id_fh "$id\n"; } close $id_fh or die "closing $id_file_name: $!"; $sth->finish();