sonicscott9041 has asked for the wisdom of the Perl Monks concerning the following question:

I use the below sub to write data into CSV format (using CSV::Text). I would like to write it to a file instead of the \*STDOUT. I have tried various ways to do this, all to no avail. I am sure there is a simple answer, but still being new to Perl, it is escaping me. Any suggestions?
sub write_columns { my $columns = shift; if($columns->{$fields[0]}) { $csv->print(\*STDOUT, [ map { $columns->{$_} } @fields ] ); } }
Thanks!

Replies are listed 'Best First'.
Re: File output instead of STDOUT
by ikegami (Patriarch) on Dec 03, 2009 at 19:44 UTC
    sub write_columns { my ($fh, $columns) = @_; if (defined($columns->{$fields[0]})) { $csv->print($fh, [ @{$columns}{ @fields } ]); } } open(my $fh, '>', $qfn) or die("Can't create output file \"$qfn\": $!\n"); write_columns($fh, $columns);
Re: File output instead of STDOUT
by Fletch (Bishop) on Dec 03, 2009 at 19:27 UTC

    So, erm, pass it a filehandle other than STDOUT. See perlopentut and open. Or even just the synopsis of the Text::CSV (which is what I presume you meant rather than "CSV::Text" above) docs themselves which give an example.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.