use strict; use warnings; use Text::CSV; my $csv = Text::CSV->new( { binary => 1, eol => "\n" } ); my $file = "records.csv"; open( my $csv_fh, '>', $file ) or die "Unable to open $file for writing: $!\n"; my $AoH_records = [ { foo => 'bar', key => 'value', }, ]; my $columns = []; for my $key ( keys @{$AoH_records}[0] ) { push @{$columns}, $key; last if scalar(@{$columns}) == 10; } $csv->column_names( $columns );