http://qs1969.pair.com?node_id=525850

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

I am looking for a method of writing form data to a CSV file. I know of Text::CSV_XS, but I am confused on the matter of

$status = $csv->print(\*OUT, $columns)

assuming I have

 $CSV =Text::CSV_XS->new()

earlier, and an array @columns the CPAN manpage says the $columns perameter must be scalar reference to an array. How can I reference the array when both the array and the above statement are in the same subroutine?

---------------------
Keep your concentration here and now where it belongs - Qui-Gon Jinn

Replies are listed 'Best First'.
Re: Populating a CSV file with Text::CSV_XS (\)
by tye (Sage) on Jan 26, 2006 at 22:27 UTC
    \@columns

    And note that $csv and $CSV aren't the same variable.

    See also perlreftut ?

    - tye        

Re: Populating a CSV file with Text::CSV_XS
by BMaximus (Chaplain) on Jan 26, 2006 at 22:31 UTC
    The docs say that it takes a reference to an array. So:
    my @columns = (qw/what ever the items are for each columns/); $csv = Text::CSV_XS->new(); # $io is a filehandle from IO::File which has a print method. A glob d +oes not. $status = $csv->print($io, \@columns);
    Use IO::File to make the filehandle. Not a glob as from what I understand the handle needs a print method in order for this to work.

    BMaximus
      Thanks BMaximus, I actually used IO::File to write the csv, which looks easier.
      ---------------------
      Keep your concentration here and now where it belongs - Qui-Gon Jinn
Re: Populating a CSV file with Text::CSV_XS
by jZed (Prior) on Jan 26, 2006 at 22:30 UTC
    I'm not sure exactly what you're asking, but these two are the same thing:
    my $columns = [ 1, "foo", 3 ]; $csv->print( \*OUT, $columns); # ... my @columns = ( 1, "foo", 3 ); $csv->print( \*OUT, \@columns);