in reply to Script Hanging w/Data::Dumper and Text::CSV_XS

You asked in the CB if Text::CSV_XS could parse from a string, rather than a file. I see you've used the $csv->parse() method to do that here by using <$fh> to read the file. That works ok because you apparently don't have any embedded newlines. If you did have embedded newlines you should use $csv->getline() instead and use it directly on the $fh. You can also use getline() on a string with embedded junk without any file. This correctly shows two records of four fields each:
#!perl -w use strict; use IO::Scalar; use Text::CSV_XS; my $csv = Text::CSV_XS->new( {binary=>1} ); my $fh = new IO::Scalar; my $str = qq{aa,"b\nb","c,c",7\ndd,ee,ff,8}; $fh->open(\$str); while (my $cols = getline($fh)) { print "<$_>" for @$cols; print "\n"; } sub getline { my $cols = $csv->getline($_[0]); if (!$cols) { die $! if $!; return undef; } return (@$cols) ? $cols : undef; }

Replies are listed 'Best First'.
Re: Using Text::CSV_XS to parse a string
by saberworks (Curate) on Nov 11, 2004 at 01:06 UTC
    I'm getting my $fh directly from CGI.pm (it's an uploaded file). Last time I tried to pass it directly to a module (in that case was GD::Image), it failed miserably because apparently they use their own kind.