Yes, even you can use CPAN.
'golf score' is not something to be concerned with if you are aiming for production code. Correctness and maintainability on the other hand should be of concern.
True laziness is hard work
| [reply] |
How about the following. Quick, clean, compatible to your future migration to Text::CSV_XS, and it came from PerlMonks instead of CPAN, so it must be ok!
package Text::CSV_PP;
######################################################################
+##########
#
# Text::CSV_PP - Text::CSV_XS compatible pure-Perl module
#
######################################################################
+##########
require 5.005;
use strict;
use vars qw($VERSION);
use Carp ();
$VERSION = '1.18';
=head1 AUTHOR
Makamaka Hannyaharamitu, E<lt>makamaka[at]cpan.orgE<gt>
Text::CSV_XS was written by E<lt>joe[at]ispsoft.deE<gt>
and maintained by E<lt>h.m.brand[at]xs4all.nlE<gt>.
Text::CSV was written by E<lt>alan[at]mfgrtl.comE<gt>.
=head1 COPYRIGHT AND LICENSE
Copyright 2005-2008 by Makamaka Hannyaharamitu, E<lt>makamaka[at]cpan.
+orgE<gt>
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 SEE ALSO
L<Text::CSV_XS>, L<Text::CSV>
I got many regexp bases from L<http://www.din.or.jp/~ohzaki/perl.htm>
=cut
| [reply] [d/l] [select] |
sub csv_join(\@) {
join ',', map {s/"/""/g || /[\s,]/ ? qq{"$_"} : $_} (my @x=@{$_[0]});
}
With revisions (passing the entire array):
sub csv_join {
join ',', map { s/"/""/g || /[\s,]/ ? qq{"$_"} : $_} (my @x=@_);
}
Best, beth
Update: Re-added my @x. Thanks, jwkrahn. I had overlooked the effect of s/// on the elements of @_.
Update 2: Added an alternative if the function needs to be called with hard coded lists, e.g. csv_join('a','b','c') | [reply] [d/l] [select] |
| [reply] [d/l] [select] |
Re: my @x...
if you do not pass the arguments as an array-ref, then something like my @x is necessary. Without the my @x =, perl will complain with:
Modification of a read-only value attempted at ....
unless these is another way around this problem...
| [reply] [d/l] [select] |
Hmmm... my test code (without my @x) didn't trigger that warning, but then I was passing arrays and not hard-coded lists of data. I think you would get that problem if you were calling csv_join('a', 'b', 'c') for example.
Hard coded lists would also be a problem with the \@ prototype since it needs to take a reference to the array that was passed to csv_join('a','b','c') and hard coded lists
don't have references. In fact, you would get something like this if the prototype is \@ and you called csv_join('a', 'b', 'c')
Type of arg 1 to main::csv_join must be array (not constant item) at M
+onks/QuickCsvJoin.pm line 33, near "'c') "
Too many arguments for main::csv_join at Monks/QuickCsvJoin.pm line 33
+, near "'c') "
If you need to make calls like csv_join('a','b','c') rather than csv_join(@aFields), then you will have to pass the array rather than just a reference to it. I've updated the original reply with the alternative. See above.
Best, beth | [reply] [d/l] [select] |
| [reply] |