use Text::CSV_XS qw( csv ); : : my $sth = $dbh->prepare ("select * from point where ID_DEVTYP like 'INTELI%' AND ID_POINT like 'AUTO%'"); $sth->execute; csv (out => "AFS.csv", in => sub { $sth->fetchrow_hashref });