patgas has asked for the wisdom of the Perl Monks concerning the following question:
(Seeing how this module was written by tilly, let me take this opportunity to show my appreciation for all the work and help he's given me and everyone in the community. Thank you. :)
I'm encountering an infinite loop while using Text::xSV to parse my CSV files. I chose to use Text::xSV because a lot of records may contain a newline in their fields. I should also caution that the file may not be proper CSV, and I wouldn't know any better, although I've looked around, and they look fine to me.
Here's the section of xSV.pm that I believe it's getting stuck on:
sub _get_row { my @row; my $q_sep = quotemeta($self->{sep}); my $match_sep = qr/\G$q_sep/; my $start_field = qr/\G(")|([^"$q_sep]*)/; # This loop is the heart of the engine while ($line =~ /$start_field/g) { if ($1) { push @row, _get_quoted(); } else { # *** ALWAYS FALLS HERE *** push @row, $2; } my $pos = pos($line); # *** THIS ENTIRE IF BLOCK IS NEVER EXECUTED *** if ($line !~ /$match_sep/g) { if ($pos == length($line)) { return @row; } else { my $expected = "Expected '$self->{sep}'"; confess("$expected at $self->{filename}, line $., char $pos" +); } } } confess("I have no idea how parsing $self->{filename} left me here +!"); }
So inside the while loop, $1 is never true, and I'm always running into the else clause. Also, the entire second if-block is never executed, which I assume is why it's stuck in the loop. Now seems like a good time to show my sample data and test code:
sample.csv
Date,SessionID,Category,Number,Description,File,Line,Column,Source,ASP +Code,ASPDescription "3/25/2002 10:25:18 AM","971874429","Orders.GetHTML(#12).GetAllUserOrd +ers(#28)msxml3.dll","0x80004005","Unknown method. tracking[-->last()<--]","/Default.asp","14","-1",,, "3/25/2002 10:25:47 AM","971874360","Orders.GetHTML(#12).GetAllUserOrd +ers(#28)msxml3.dll","0x80004005","Unknown method. tracking[-->last()<--]","/Default.asp","14","-1",,, "3/25/2002 10:29:49 AM","971874312","Active Server Pages","0x8002802B" +,"Create object failed","?","0","-1",,,"An error occurred while creat +ing object 'oCheckOut'." "3/25/2002 10:45:24 AM","971874360","Orders.GetHTML(#20)msxml3.dll","0 +x80004005","The stylesheet does not contain a document element. The +stylesheet may be empty, or it may not be a well-formed XML document. +","/Default.asp","14","-1",,, "3/25/2002 10:46:04 AM","971874509","Microsoft OLE DB Provider for SQL + Server","0x80040E14","Line 1: Incorrect syntax near ','.","/Default. +asp","169","-1",,, "3/25/2002 12:01:00 PM","980227415","Receipt.GetHTML(#19).GetOrder(#26 +)Account","0x800A0005","Invalid procedure call or argument","/Default +.asp","15","-1",,,
test.pl (The real application is a big CGI graph producer, but after bogging down the server a couple times, I figured I'd better just test it locally :) Both the bind_header() and get_row() subs use the aforementioned _get_row(), so commenting out one or the other shouldn't matter.
#!/usr/bin/perl -w use strict; use Text::xSV; print "Creating object...\n"; my $csv = Text::xSV->new; print "Opening file...\n"; $csv->open_file( 'sample.csv' ); print "Binding header...\n"; $csv->bind_header(); print "Entering loop...\n"; while ( $csv->get_row() ) { print " found row\n"; } print "Finished loop.\n";
"As information travels faster in the modern age, as our days are crawling by so slowly." -- DCFC
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Infinite loop with Text::xSV
by jmcnamara (Monsignor) on Mar 25, 2002 at 23:52 UTC |