Is_quoted () in Text :: CSV perl module

It may not be something obvious to me, but I have a very simple perl script in which the is_quoted () method in the Text :: CSV module does not work as expected. Here is the code:

# cat ./testcsv.pl #!/usr/bin/perl use strict; use Text::CSV; my $csv = Text::CSV->new ( { quote_char => '"' } ) or die "Cannot use CSV: ".Text::CSV->error_diag (); print "Text::CSV version = " . $csv->version() . "\n\n"; my $line = '"text field 111",222,"text field 333",444'; my $status = $csv->parse($line); if ($status) { my $column_idx = 0; my @fields = $csv->fields (); foreach my $field (@fields) { my $quoted = $csv->is_quoted ($column_idx); $column_idx++; print "field #$column_idx: '$field'; quoted = " . ($quoted ? "YES\n" : "NO\n"); } } 

And this is what I get when I run the script:

  # perl -v |  grep "is perl"
     This is perl, v5.10.1 (*) built for PA-RISC2.0
     # ./testcsv.pl
     Text :: CSV version = 1.29

     field # 1: 'text field 111';  quoted = NO
     field # 2: '222';  quoted = NO
     field # 3: 'text field 333';  quoted = NO
     field # 4: '444';  quoted = NO
     # 

As we can see, the parse () method correctly splits the source string into fields, so I know that Text :: CSV is installed and working correctly. As I understand it, after reading the documentation for Text :: CSV , it is assumed that the is_quoted () method returns true if the data in the specified column is enclosed in quotation marks quote_char. Therefore, I expected to see "YES" after fields 1 and 3, since they are explicitly specified in the initialization for the variable $ line. But this does not happen.

Am I doing something wrong or text: Is CSV broken?

+6
source share
1 answer

You need to specify keep_meta_info => 1 .


By the way, I don't like two iterators, so I will iterate over the indexes.

 my @fields = $csv->fields(); for my $column_idx (0..$#fields) { my $field = $fields[$column_idx]; ... } 
+7
source

All Articles