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

Perl Gurus: Has anyone ran into the issue of some not being quoted in the combine function. The only time it puts quotes is if there is a space in the field. When I assign the variable ($doc_type) to letters, it does the same thing. Please see the following code and sample.
#!/usr/bin/perl -w use strict; use warnings; use XML::Simple; use Text::CSV_XS; #open DIPFILE, ">> C:/director/DIPFILE.TXT"; #open LOGFILE, ">> C:/director/DIPLOG.LOG"; #Pull in all the xml files for each report. my @dipfile = glob('C:/director/*.xml'); my $csv = Text::CSV_XS->new({always_quote => 0}); foreach my $xml_file (@dipfile) { my $ref = XML::Simple::XMLin($xml_file, SuppressEmpty => ''); my ($fileName) = "//prime/ReportExtract/Director/" . $ref->{fileNa +me}; my ($default_name) = $ref->{defaultName}; my ($doc_type) = '1'; #Default set to unidentified Document Type. my ($default_description) = $ref->{defaultDescription}; my ($report_time) = $ref->{reportExecutionTime}; my ($reportSearchPath) = $ref->{reportSearchPath}; if (substr($reportSearchPath,23,10) eq "Operations"){$doc_type = 1 +719}; $csv->combine($fileName,$doc_type, $default_name,$default_descript +ion,$report_time); print $csv->string, "\n"; }
Thanks. Dave

Replies are listed 'Best First'.
Re: Text::CSV_XS Issue
by Tux (Canon) on May 28, 2009 at 18:46 UTC

    Text::CSV_XS and Text::CSV only quote when needed in combine () and print (). That is by design. If you want all strings quoted, there is an option to do so: always_quote, which you explicitely set to 0

    always_quote By default the generated fields are quoted only, if they need to, fo +r example, if they contain the separator. If you set this attribute to a TRUE v +alue, then all fields will be quoted. This is typically easier to handle in ext +ernal applications. (Poor creatures who aren't using Text::CSV_XS. :-)
    my $csv = Text::CSV_XS->new ({ binary => 1, always_quote => 1, eol => "\r\n" });

    And I would advise you to use

    $csv->print (*STDOUT, [ $fileName,$doc_type, $default_name, $default_description, $report_time ]);

    instead of using combine () and string ().

    edit: code => quote typo fixed


    Enjoy, Have FUN! H.Merijn
      Why are you resistant to using combine or string?
        • Efficiency. Both in speed and memory use
        • Readability. It's more terse and easier in use
        • More in line with getline (), which is safer and faster than parse ()

        Enjoy, Have FUN! H.Merijn
Re: Text::CSV_XS Issue
by ikegami (Patriarch) on May 28, 2009 at 18:46 UTC

    What's the problem? You didn't ask a question.

    The only time it puts quotes is if there is a space in the field.

    Are you implying you want quotes to always be used? You specifically indicate that's the behaviour you want (always_quote => 0) even though that's the default. Specify a true value (always_quote => 1) if you want values to always be quoted.

      Yes, I want quotes for each field. I tried always_quote but I got the same result.
        Again, set it to something true (like 1), not something false (like 0). Demonstrate that it doesn't work if it doesn't.