CSV_XS Question

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I'm trying to use CSV_XS to parse log files.
Each field is separated by a comma and surround by double quotes.

Within each field, there may be embdedded double and single quotes.
These embedded quotes are escaped by a backslash.

I'm having trouble getting it to work.  I have two questions:

1) The entire script fails at the first line it can't parse.  How can
I make it ignore the current line and continue?
     I'm using getline() to read the file, like this:

    open my $io, "<", $logname or warn("Cannot open file $ for
reading. $!");
    while (my $line = $csv->getline($io)) {


2) How should I instantiate my $csv object to get it to recognize my
escape character?

I tried this:

my $csv = Text::CSV_XS->new( {
    escape_char => '\',
    sep_char => ',',
    binary => 1,
} );

Re: CSV_XS Question

Quoth jeffg@programmer.net:
Quoted text here. Click to load it

You want to die at this point, not warn. There's no point continuing if
the open failed: $io is undef, and the rest of the program will just
give unhelpful errors.

Quoted text here. Click to load it

->getline returns false on EOF or on parse error. You can tell the
difference with ->eof; something like

    while (my $line = $csv->getline($io)) {

    unless ($csv->eof) {
        warn "bad line in CSV file\n";
        redo CSV;

Alternatively you could read the lines in yourself and call ->parse on
them; that way you can show the user what the bad line looked like. Be
careful to make sure you get complete records, if you do this.

Quoted text here. Click to load it

It looks to me as though you need the allow_loose_escapes option, to
allow you to escape ' when it doesn't really need it.


Site Timeline