U
usenet
In "Perl Best Practices," (great book, BTW!) Damian Conway recommends:
He supports his recommendation (in part) with this narrative:
However, in the usage examples he provides, he doesn't show how the
Text::CSV_XS module helps simplify that particular vagarity (ignoring
whitespace around commas), and I don't see it discussed in the module's
perldocs.
I thought maybe I could tell the module that the quote_char could be a
whitespace, but this actually throws a runtime error.
Kindly consider this code (adapted from Damian's book); how would I
make efficient use of the Text::CSV_XS module to ignore the whitespace
around the second line of __DATA__, as Damian suggests I can do?
#!/usr/bin/perl
use strict; use warnings;
use Text::CSV_XS;
my $csv_format = Text::CSV_XS->new({
sep_char => q{:}, #fields are double-point delimited
#quote_char => q{ }, #whitespace throws runtime error!!!
});
while (my $record = <DATA>) {
$csv_format -> parse($record); #error-checking omitted
my ($user, $uid, $gid) = $csv_format -> fields();
print map {"'$_'\n"} ($user, $uid, $gid);
}
__DATA__
sshd:123:321::/var/empty:/usr/bin/ksh
apache : 789 : 987:: /home/apache: /usr/bin/ksh
Thanks!
He supports his recommendation (in part) with this narrative:
Using split to extract variable-width fields is efficient and easy,
provided those fields _really_are_ always delimited by a simple
separator. More often, though... it becomes necessary to
extend the format rules to cope with human vagaries (such as
ignoring whitespace around commas) [and he goes on to discuss
how ugly the regexp can get as the requirements morph]
However, in the usage examples he provides, he doesn't show how the
Text::CSV_XS module helps simplify that particular vagarity (ignoring
whitespace around commas), and I don't see it discussed in the module's
perldocs.
I thought maybe I could tell the module that the quote_char could be a
whitespace, but this actually throws a runtime error.
Kindly consider this code (adapted from Damian's book); how would I
make efficient use of the Text::CSV_XS module to ignore the whitespace
around the second line of __DATA__, as Damian suggests I can do?
#!/usr/bin/perl
use strict; use warnings;
use Text::CSV_XS;
my $csv_format = Text::CSV_XS->new({
sep_char => q{:}, #fields are double-point delimited
#quote_char => q{ }, #whitespace throws runtime error!!!
});
while (my $record = <DATA>) {
$csv_format -> parse($record); #error-checking omitted
my ($user, $uid, $gid) = $csv_format -> fields();
print map {"'$_'\n"} ($user, $uid, $gid);
}
__DATA__
sshd:123:321::/var/empty:/usr/bin/ksh
apache : 789 : 987:: /home/apache: /usr/bin/ksh
Thanks!