Skip Menu |

This queue is for tickets about the Text-CSV CPAN distribution.

Report information
The Basics
Id: 76606
Status: resolved
Priority: 0/
Queue: Text-CSV

People
Owner: Nobody in particular
Requestors: timh [...] dirtymonday.net
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: No option to only quote empty but not undef fields
Date: Mon, 16 Apr 2012 11:14:20 -0700
To: bug-Text-CSV [...] rt.cpan.org
From: Tim Howe <timh [...] dirtymonday.net>
It would be nice if there were some way to only quote empty fields when they aren't undef. For example, if I don't have always_quote defined, some output might look like this: "Column One","Column 2",dog,cat, However, the 5th column might be undef, or it could just be an empty string. I would suggest that when blank_is_undef is set, empty fields get quoted like so: "Column One","Column 2",dog,cat,"" this way we know it is an empty string and not undef. undef in column 5 would look like: "Column One","Column 2",dog,cat, The only way I have found to differentiate empty strings from undef in output is to set always_quote and blank_is_undef, but the output then looks like this if the 5th column is undef: "Column One","Column 2","dog","cat", and like this if it is an empty string: "Column One","Column 2","dog","cat","" This "works", but it isn't how I would choose to do this. --TimH
On Mon Apr 16 14:14:29 2012, timh@dirtymonday.net wrote: Show quoted text
> It would be nice if there were some way to only quote empty fields when > they aren't undef.
This can be achieved with the "blank_is_undef" attribute: blank_is_undef my $csv = Text::CSV_XS->new ({ blank_is_undef => 1 }); $csv->blank_is_undef (0); my $f = $csv->blank_is_undef; Under normal circumstances, "CSV" data makes no distinction between quoted- and unquoted empty fields. These both end up in an empty string field once read, thus 1,"",," ",2 is read as ("1", "", "", " ", "2") When writing "CSV" files with "always_quote" set, the unquoted empty field is the result of an undefined value. To enable this distinction when reading "CSV" data, the "blank_is_undef" attribute will cause unquoted empty fields to be set to "undef", causing the above to be parsed as ("1", "", undef, " ", "2")
On Wed Oct 08 16:09:22 2014, HMBRAND wrote: Show quoted text
> On Mon Apr 16 14:14:29 2012, timh@dirtymonday.net wrote:
> > It would be nice if there were some way to only quote empty fields > > when > > they aren't undef.
> > This can be achieved with the "blank_is_undef" attribute: > > blank_is_undef > my $csv = Text::CSV_XS->new ({ blank_is_undef => 1 }); > $csv->blank_is_undef (0); > my $f = $csv->blank_is_undef; > > Under normal circumstances, "CSV" data makes no distinction between > quoted- and unquoted empty fields. These both end up in an empty > string field once read, thus > > 1,"",," ",2 > > is read as > > ("1", "", "", " ", "2") > > When writing "CSV" files with "always_quote" set, the unquoted > empty field is the result of an undefined value. To enable this > distinction when reading "CSV" data, the "blank_is_undef" > attribute will cause unquoted empty fields to be set to "undef", > causing the above to be parsed as > > ("1", "", undef, " ", "2")
I suppose this ticket can be closed now. If not, please reopen this. Thanks.