Text-CSV-1.33/0000755000175000017500000000000012475371020011506 5ustar reonreonText-CSV-1.33/files/0000755000175000017500000000000012475371020012610 5ustar reonreonText-CSV-1.33/files/macosx.csv0000644000175000017500000000164312475365663014643 0ustar reonreon"'\'\\'\\\'""\""\\""\\\""",,,,,,,,,,,,,, Exported 12/16/2008 10:30 AM,,,,,,Category,Category name,,,,,Category name 2,, Username,Last Name,First Name M.,Section/Group,Status,Notes,Assignment,Category name 1,Category name 2,Category name 3,woot!,dqwdqwd,Category name 2 1,Total Score,Class Grade ,,,,,,Grading scale,Points,Points,Points,Points,Points,Points,, ,,,,,,Points possible,11,11,11,11,11,11,, dcwalker,,,,Dropped,,,1,34,1,,,,109, jdr99,,,devs,Active,"qwd qwd qwd",,12,0,1,,,,39, jlaney,,,devs,Active,,,,2,23,,,,114, mcrawfor,,,devs,Active,"line 1 line 2 line 3 XX fwe and so on yea!",,,,,,,,, ,,,,,,,,,,,,,, ,,,,,,Mean,6.5,12.0,8.33,#DIV/0!,#DIV/0!,#DIV/0!,87.33, ,,,,,,Median,6.5,2.0,1.0,#NUM!,#NUM!,#NUM!,109.0, ,,,,,,Mode,#N/A,#N/A,1.0,#N/A,#N/A,#N/A,#N/A, ,,,,,,Min,1.0,0.0,1.0,0.0,0.0,0.0,39.0, ,,,,,,Max,12.0,34.0,23.0,0.0,0.0,0.0,114.0, ,,,,,,Std. Dev.,7.78,19.08,12.7,#DIV/0!,#DIV/0!,#DIV/0!,41.93,Text-CSV-1.33/files/utf8.csv0000644000175000017500000000001412475365663014226 0ustar reonreon"Øl/Vin",0 Text-CSV-1.33/META.yml0000664000175000017500000000120612475371020012760 0ustar reonreon--- abstract: 'comma-separated values manipulator (using XS or PurePerl)' author: - 'Makamaka Hannyaharamitu, Emakamaka[at]cpan.orgE' build_requires: ExtUtils::MakeMaker: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 6.98, CPAN::Meta::Converter version 2.140640' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Text-CSV no_index: directory: - t - inc requires: IO::Handle: '0' Test::Harness: '0' Test::More: '0' resources: repository: http://github.com/makamaka/Text-CSV version: '1.33' Text-CSV-1.33/Changes0000644000175000017500000001640412475365663013026 0ustar reonreonRevision history for Perl extension Text::CSV. 1.33 Tue Mar 3 14:13:41 2015 - Fix some reported bugs with handling fields with zeros. https://rt.cpan.org/Public/Bug/Display.html?id=93518 https://rt.cpan.org/Public/Bug/Display.html?id=92509 patched by shlomif - catch up Text::CSV_XS 1.02 * add decode_utf8 attribute by ktat - fix warnings: $. is undefined by ktat - typo fixes in doc by anirvan, dsteinbrunner and pdl. - parse() supports bind_columns() * TODO: Updating documents and adding diag_verbose in the next version. Catching up CSV_XS 1.04. 1.32 - fix t/rt71_pp.t 1.31 Thu Jun 13 14:06:49 2013 - fix handling UTF8 in parse method. - fix getline with allow_loose_quotes (rt#83705) - add allow_unquoted_escape - fix parsing escapted sep char (found in Text::CSV_XS rt#81295) 1.30 Tue Jun 11 00:06:02 2013 - catch up Text::CSV_XS 0.99 (except for diag_verbose and allow_unquoted_escape) 1.21 Mon Dec 27 12:35:35 2010 - updated the compatibility for Text::CSV_XS version 0.80 * added getline_all() and getaline_hr_all() * added missing test file 1.20 Wed Oct 20 13:53:59 2010 - couldn't parse the csv containing the column starting with '0'. (hiratara) * patched by hiratara * enhanced getline regexp (makamaka) * resolved a bug in bleadperl-fb85c04 - added tests into t/71_pp.t (hiratara) 1.19 Sat Oct 2 14:15:59 2010 - getline didn't work correctly with 0 or null containing lines (pointed by Bernhard Prott) - updated the compatibility for Text::CSV_XS version 0.74 * real eol support for parsing streams (beyond \n, \r and \r\n) * clarify doc for always_quote to not quote undef fields and XS and PP incompatibility of UTF8 process for print and combine 1.18 Sat Jun 19 10:34:07 2010 - fixed a combine bug with quote_space reported and patched by rt#58356 - updated test files compatible to CSV_XS 0.73 1.17 Tue Mar 16 15:20:34 2010 - fixed parse working when setting quote_char undef. - made Text::CSV_XS compat 0.71 * Text::CSV->error_diag() in void context warns instead of doing nothing * auto_diag also used for new () itself - added quote_null (introduced in Text::CSV_XS 0.72) 1.16 Tue Dec 8 19:02:58 2009 - updated the compatibility for Text::CSV_XS version 0.70 * Added quote_space attribute * Forbad \r and \n in sep_char, quote_char, and escape_char 1.15 Thu Oct 15 17:23:39 2009 - updated the compatibility for Text::CSV_XS version 0.69 * Auto detection of eol => "\r" in streams (but incomplete correspondence. I will rewrite CSV_PP in the future) 1.14 Thu Oct 8 15:02:24 2009 - updated the compatibility for Text::CSV_XS version 0.68 * fail if first arg to new () is not a hash ref * added empty_is_undef * error_diag () uses warn () in void context instead of STDERR * added auto_diag attribute (not yet implemented localization feature for autodie) * updated documents same as Text::CSV_XS 1.13 Fri Jul 31 12:02:53 2009 - getline() didn't handle '0' starting multi line data (pointed by Diego Santa Cruz). 1.12 Sat May 16 10:46:38 2009 - updated the compatibility for Text::CSV_XS version 0.65 * new()ing errors can be checked on number (1002) * modified doc for error_diag() return value in case of constructor failure - parse() didn't set the given string ref into {_STRING}. (rt#45215) - getline() didn't handle a line having null (ex. "0) 1.11 Sat Mar 21 16:07:29 2009 - updated the compatibility for Text::CSV_XS version 0.63 * added error 1002 - updated the compatibility for Text::CSV_XS version 0.58 * fixed allow_loose_escapes bug 1.10 Wed Oct 22 02:34:03 2008 - updated the compatibility for Text::CSV_XS version 0.56 * updated docs * setting eol with undef are treated as "" * in print method, don't print $\ twice * undef treated as 0 for boolean attributes 1.09 Fri Sep 5 11:34:00 2008 - updated the compatibility for Text::CSV_XS version 0.54 * default eol for print is $\ * fixed SetDiag(0) * IO failure in print **** Text::CSV_PP doesn't support the error msg 2023 **** 1.08 Fri Aug 22 11:21:38 2008 - fixed a bug in parsing tab separated values with allow_whitespace pointed by and thanks a patch to Mike O'Sullivan 1.07 Fri Aug 1 11:13:06 2008 - updated the compatibility for Text::CSV_XS version 0.52 modified column_names() - fixed a parsing bug with quote_char being undef pointed by Matt (rt#38083) 1.06 Wed Jun 18 14:35:40 2008 - updated the compatibility for Text::CSV_XS version 0.51 * set binary => 1 when UTF8 flag marked. * removed the max of 255 for bind_columns. - made the value type of $csv->{_STRING} corresponding to XS. 1.05 Fri May 2 13:15:49 2008 - fixed quoting process in combine. because of using bytes.pm, added a dummy for Perl 5.005 - renamed combine, parse, string and fields internally. (for coming Text::CSV::Encoded) - removed dynamic mode. 1.04 Tue Apr 22 16:01:19 2008 - updated the compatibility for Text::CSV_XS version 0.43 * parse errors try to remember failing position (but in using PP version backend, it is usually helpless) - fix a serious bug with setter methods. all setter methods couldn't take undef value... 1.03 Fri Apr 11 17:39:12 2008 - updated the compatibility for Text::CSV_XS version 0.41 getline_hr, column_names, bind_columns - Makefile.PL requires Perl versoin from 5.005 to 5.00503. - fixed combine() for quoting binary chars. - updated the document as same as Text::CSV_XS 0.41. 1.02 Fri Mar 7 07:37:11 2008 - updated the compatibility for Text::CSV_XS version 0.36 * auto-load IO::Handle when needed - fixed version() and its doc (Text::CSV and Text::CSV_PP). Text::CSV->version returns the worker module version. Thanks to Robin Barker's patche. 1.01 Tue Mar 4 02:33:28 2008 - updated the compatibilities for Text::CSV_XS version 0.35 * diagnostics for failed new () * 'blank_is_undef' option * enhanced the error messages - updated docs - fixed allow_whitespace() - modifiled t/80_diag.t for overloaded error object. 1.00 Wed Nov 28 14:28:30 2007 - renamed $ENV{TEXT_CSV_XS} to $ENV{PERL_TEXT_CSV} - added $ENV{PERL_TEXT_CSV} acceptable values - deleted the dynamic mode section from the pod doc - deleted t/exp01-dynamic.t (the feature still remains.) 0.99_06 Thu Nov 8 13:24:31 2007 - modified _set_methods for Pod::Coverage test - added t/exp01-dynamic.t 0.99_05 Wed Nov 7 16:15:34 2007 (Text::CSV_PP 1.08) - added 'dynamic mode' which can specify the worker module in new()ing This is so experimental that may be removed. - modified prototypes of Text::CSV_PP::NV, IV, PV 0.99_04 Tue Nov 6 13:40:47 2007 (Text::CSV_PP 1.07) - re-rename Text::CSV_PP - added TODO 0.99_03 Mon Nov 5 16:00:00 2007 - CPAN released but beta version. - test suits are from Text::CSV_XS 0.32 and modfied for Text::CSV tests. 0.99_02 Fri Nov 2 14:37:30 2007 - pre release version. - passed the all Text::CSV_XS 0.32 test suits 0.99 Sat Jun 23 17:10:47 2007 - maintainer was changed. - rewritten to make a wrapper to Text::CSV_XS and Text::CSV_PP 0.01 06/05/1997 - original version by Alan Citterman Text-CSV-1.33/lib/0000755000175000017500000000000012475371020012254 5ustar reonreonText-CSV-1.33/lib/Text/0000755000175000017500000000000012475371020013200 5ustar reonreonText-CSV-1.33/lib/Text/CSV.pm0000644000175000017500000010647712475365663014230 0ustar reonreonpackage Text::CSV; use strict; use Carp (); use vars qw( $VERSION $DEBUG ); BEGIN { $VERSION = '1.33'; $DEBUG = 0; } # if use CSV_XS, requires version my $Module_XS = 'Text::CSV_XS'; my $Module_PP = 'Text::CSV_PP'; my $XS_Version = '1.02'; my $Is_Dynamic = 0; # used in _load_xs and _load_pp my $Install_Dont_Die = 1; # When _load_xs fails to load XS, don't die. my $Install_Only = 2; # Don't call _set_methods() my @PublicMethods = qw/ version types quote_char escape_char sep_char eol always_quote binary allow_whitespace keep_meta_info allow_loose_quotes allow_loose_escapes verbatim meta_info is_quoted is_binary eof getline print parse combine fields string error_diag error_input status blank_is_undef empty_is_undef getline_hr column_names bind_columns auto_diag quote_space quote_null getline_all getline_hr_all is_missing quote_binary record_number print_hr PV IV NV /; # my @UndocumentedXSMethods = qw/Combine Parse SetDiag/; my @UndocumentedPPMethods = qw//; # Currently empty # Check the environment variable to decide worker module. unless ($Text::CSV::Worker) { $Text::CSV::DEBUG and Carp::carp("Check used worker module..."); if ( exists $ENV{PERL_TEXT_CSV} ) { if ($ENV{PERL_TEXT_CSV} eq '0' or $ENV{PERL_TEXT_CSV} eq 'Text::CSV_PP') { _load_pp(); } elsif ($ENV{PERL_TEXT_CSV} eq '1' or $ENV{PERL_TEXT_CSV} =~ /Text::CSV_XS\s*,\s*Text::CSV_PP/) { _load_xs($Install_Dont_Die) or _load_pp(); } elsif ($ENV{PERL_TEXT_CSV} eq '2' or $ENV{PERL_TEXT_CSV} eq 'Text::CSV_XS') { _load_xs(); } else { Carp::croak "The value of environmental variable 'PERL_TEXT_CSV' is invalid."; } } else { _load_xs($Install_Dont_Die) or _load_pp(); } } sub import { my ($class, $option) = @_; } sub new { # normal mode my $proto = shift; my $class = ref($proto) || $proto; unless ( $proto ) { # for Text::CSV_XS/PP::new(0); return eval qq| $Text::CSV::Worker\::new( \$proto ) |; } #if (ref $_[0] and $_[0]->{module}) { # Carp::croak("Can't set 'module' in non dynamic mode."); #} if ( my $obj = $Text::CSV::Worker->new(@_) ) { $obj->{_MODULE} = $Text::CSV::Worker; bless $obj, $class; return $obj; } else { return; } } sub require_xs_version { $XS_Version; } sub module { my $proto = shift; return !ref($proto) ? $Text::CSV::Worker : ref($proto->{_MODULE}) ? ref($proto->{_MODULE}) : $proto->{_MODULE}; } *backend = *module; sub is_xs { return $_[0]->module eq $Module_XS; } sub is_pp { return $_[0]->module eq $Module_PP; } sub is_dynamic { $Is_Dynamic; } sub AUTOLOAD { my $self = $_[0]; my $attr = $Text::CSV::AUTOLOAD; $attr =~ s/.*:://; return if $attr =~ /^[A-Z]+$/; Carp::croak( "Can't locate method $attr" ) unless $attr =~ /^_/; my $pkg = $Text::CSV::Worker; my $method = "$pkg\::$attr"; $Text::CSV::DEBUG and Carp::carp("'$attr' is private method, so try to autoload..."); local $^W; no strict qw(refs); *{"Text::CSV::$attr"} = *{"$pkg\::$attr"}; goto &$attr; } sub _load_xs { my $opt = shift; $Text::CSV::DEBUG and Carp::carp "Load $Module_XS."; eval qq| use $Module_XS $XS_Version |; if ($@) { if (defined $opt and $opt & $Install_Dont_Die) { $Text::CSV::DEBUG and Carp::carp "Can't load $Module_XS...($@)"; return 0; } Carp::croak $@; } push @Text::CSV::ISA, 'Text::CSV_XS'; unless (defined $opt and $opt & $Install_Only) { _set_methods( $Text::CSV::Worker = $Module_XS ); } return 1; }; sub _load_pp { my $opt = shift; $Text::CSV::DEBUG and Carp::carp "Load $Module_PP."; eval qq| require $Module_PP |; if ($@) { Carp::croak $@; } push @Text::CSV::ISA, 'Text::CSV_PP'; unless (defined $opt and $opt & $Install_Only) { _set_methods( $Text::CSV::Worker = $Module_PP ); } }; sub _set_methods { my $class = shift; #return; local $^W; no strict qw(refs); for my $method (@PublicMethods) { *{"Text::CSV::$method"} = \&{"$class\::$method"}; } if ($Text::CSV::Worker eq $Module_XS) { for my $method (@UndocumentedXSMethods) { *{"Text::CSV::$method"} = \&{"$Module_XS\::$method"}; } } if ($Text::CSV::Worker eq $Module_PP) { for my $method (@UndocumentedPPMethods) { *{"Text::CSV::$method"} = \&{"$Module_PP\::$method"}; } } } 1; __END__ =pod =head1 NAME Text::CSV - comma-separated values manipulator (using XS or PurePerl) =head1 SYNOPSIS use Text::CSV; my @rows; my $csv = Text::CSV->new ( { binary => 1 } ) # should set binary attribute. or die "Cannot use CSV: ".Text::CSV->error_diag (); open my $fh, "<:encoding(utf8)", "test.csv" or die "test.csv: $!"; while ( my $row = $csv->getline( $fh ) ) { $row->[2] =~ m/pattern/ or next; # 3rd field should match push @rows, $row; } $csv->eof or $csv->error_diag(); close $fh; $csv->eol ("\r\n"); open $fh, ">:encoding(utf8)", "new.csv" or die "new.csv: $!"; $csv->print ($fh, $_) for @rows; close $fh or die "new.csv: $!"; # # parse and combine style # $status = $csv->combine(@columns); # combine columns into a string $line = $csv->string(); # get the combined string $status = $csv->parse($line); # parse a CSV string into fields @columns = $csv->fields(); # get the parsed fields $status = $csv->status (); # get the most recent status $bad_argument = $csv->error_input (); # get the most recent bad argument $diag = $csv->error_diag (); # if an error occured, explains WHY $status = $csv->print ($io, $colref); # Write an array of fields # immediately to a file $io $colref = $csv->getline ($io); # Read a line from file $io, # parse it and return an array # ref of fields $csv->column_names (@names); # Set column names for getline_hr () $ref = $csv->getline_hr ($io); # getline (), but returns a hashref $eof = $csv->eof (); # Indicate if last parse or # getline () hit End Of File $csv->types(\@t_array); # Set column types =head1 DESCRIPTION Text::CSV provides facilities for the composition and decomposition of comma-separated values using L or its pure Perl version. An instance of the Text::CSV class can combine fields into a CSV string and parse a CSV string into fields. The module accepts either strings or files as input and can utilize any user-specified characters as delimiters, separators, and escapes so it is perhaps better called ASV (anything separated values) rather than just CSV. =head1 VERSION 1.33 This module is compatible with Text::CSV_XS B<1.02> and later. (except for diag_verbose) =head2 Embedded newlines B: The default behavior is to only accept ASCII characters. This means that fields can not contain newlines. If your data contains newlines embedded in fields, or characters above 0x7e (tilde), or binary data, you *must* set C<< binary => 1 >> in the call to C. To cover the widest range of parsing options, you will always want to set binary. But you still have the problem that you have to pass a correct line to the C method, which is more complicated from the usual point of usage: my $csv = Text::CSV->new ({ binary => 1, eol => $/ }); while (<>) { # WRONG! $csv->parse ($_); my @fields = $csv->fields (); will break, as the while might read broken lines, as that does not care about the quoting. If you need to support embedded newlines, the way to go is either my $csv = Text::CSV->new ({ binary => 1, eol => $/ }); while (my $row = $csv->getline (*ARGV)) { my @fields = @$row; or, more safely in perl 5.6 and up my $csv = Text::CSV->new ({ binary => 1, eol => $/ }); open my $io, "<", $file or die "$file: $!"; while (my $row = $csv->getline ($io)) { my @fields = @$row; =head2 Unicode (UTF8) On parsing (both for C and C), if the source is marked being UTF8, then all fields that are marked binary will also be be marked UTF8. For complete control over encoding, please use L: use Text::CSV::Encoded; my $csv = Text::CSV::Encoded->new ({ encoding_in => "iso-8859-1", # the encoding comes into Perl encoding_out => "cp1252", # the encoding comes out of Perl }); $csv = Text::CSV::Encoded->new ({ encoding => "utf8" }); # combine () and print () accept *literally* utf8 encoded data # parse () and getline () return *literally* utf8 encoded data $csv = Text::CSV::Encoded->new ({ encoding => undef }); # default # combine () and print () accept UTF8 marked data # parse () and getline () return UTF8 marked data On combining (C and C), if any of the combining fields was marked UTF8, the resulting string will be marked UTF8. Note however if the backend module is Text::CSV_XS, that all fields C the first field that was marked UTF8 and contained 8-bit characters that were not upgraded to UTF8, these will be bytes in the resulting string too, causing errors. If you pass data of different encoding, or you don't know if there is different encoding, force it to be upgraded before you pass them on: # backend = Text::CSV_XS $csv->print ($fh, [ map { utf8::upgrade (my $x = $_); $x } @data ]); =head1 SPECIFICATION See to L. =head1 FUNCTIONS These methods are common between the XS and the pure Perl version. Most of the document was shamelessly copied and replaced from Text::CSV_XS. =head2 version () (Class method) Returns the current backend module version. If you want the module version, you can use the C method, print Text::CSV->VERSION; # This module version print Text::CSV->version; # The version of the worker module # same as Text::CSV->backend->version =head2 new (\%attr) (Class method) Returns a new instance of Text::CSV_XS. The objects attributes are described by the (optional) hash ref C<\%attr>. Currently the following attributes are available: =over 4 =item eol An end-of-line string to add to rows. C is replaced with an empty string. The default is C<$\>. Common values for C are C<"\012"> (Line Feed) or C<"\015\012"> (Carriage Return, Line Feed). Cannot be longer than 7 (ASCII) characters. If both C<$/> and C equal C<"\015">, parsing lines that end on only a Carriage Return without Line Feed, will be Cd correct. Line endings, whether in C<$/> or C, other than C, C<"\n">, C<"\r\n">, or C<"\r"> are not (yet) supported for parsing. =item sep_char The char used for separating fields, by default a comma. (C<,>). Limited to a single-byte character, usually in the range from 0x20 (space) to 0x7e (tilde). The separation character can not be equal to the quote character. The separation character can not be equal to the escape character. See also L =item allow_whitespace When this option is set to true, whitespace (TAB's and SPACE's) surrounding the separation character is removed when parsing. If either TAB or SPACE is one of the three major characters C, C, or C it will not be considered whitespace. So lines like: 1 , "foo" , bar , 3 , zapp are now correctly parsed, even though it violates the CSV specs. Note that B whitespace is stripped from start and end of each field. That would make it more a I than a way to be able to parse bad CSV lines, as 1, 2.0, 3, ape , monkey will now be parsed as ("1", "2.0", "3", "ape", "monkey") even if the original line was perfectly sane CSV. =item blank_is_undef Under normal circumstances, CSV data makes no distinction between quoted- and unquoted empty fields. They both end up in an empty string field once read, so 1,"",," ",2 is read as ("1", "", "", " ", "2") When I CSV files with C set, the unquoted empty field is the result of an undefined value. To make it possible to also make this distinction when reading CSV data, the C option will cause unquoted empty fields to be set to undef, causing the above to be parsed as ("1", "", undef, " ", "2") =item empty_is_undef Going one step further than C, this attribute converts all empty fields to undef, so 1,"",," ",2 is read as (1, undef, undef, " ", 2) Note that this only effects fields that are I empty, not fields that are empty after stripping allowed whitespace. YMMV. =item quote_char The char used for quoting fields containing blanks, by default the double quote character (C<">). A value of undef suppresses quote chars. (For simple cases only). Limited to a single-byte character, usually in the range from 0x20 (space) to 0x7e (tilde). The quote character can not be equal to the separation character. =item allow_loose_quotes By default, parsing fields that have C characters inside an unquoted field, like 1,foo "bar" baz,42 would result in a parse error. Though it is still bad practice to allow this format, we cannot help there are some vendors that make their applications spit out lines styled like this. In case there is B bad CSV data, like 1,"foo "bar" baz",42 or 1,""foo bar baz"",42 there is a way to get that parsed, and leave the quotes inside the quoted field as-is. This can be achieved by setting C B making sure that the C is I equal to C. =item escape_char The character used for escaping certain characters inside quoted fields. Limited to a single-byte character, usually in the range from 0x20 (space) to 0x7e (tilde). The C defaults to being the literal double-quote mark (C<">) in other words, the same as the default C. This means that doubling the quote mark in a field escapes it: "foo","bar","Escape ""quote mark"" with two ""quote marks""","baz" If you change the default quote_char without changing the default escape_char, the escape_char will still be the quote mark. If instead you want to escape the quote_char by doubling it, you will need to change the escape_char to be the same as what you changed the quote_char to. The escape character can not be equal to the separation character. =item allow_loose_escapes By default, parsing fields that have C characters that escape characters that do not need to be escaped, like: my $csv = Text::CSV->new ({ escape_char => "\\" }); $csv->parse (qq{1,"my bar\'s",baz,42}); would result in a parse error. Though it is still bad practice to allow this format, this option enables you to treat all escape character sequences equal. =item binary If this attribute is TRUE, you may use binary characters in quoted fields, including line feeds, carriage returns and NULL bytes. (The latter must be escaped as C<"0>.) By default this feature is off. If a string is marked UTF8, binary will be turned on automatically when binary characters other than CR or NL are encountered. Note that a simple string like C<"\x{00a0}"> might still be binary, but not marked UTF8, so setting C<{ binary =E 1 }> is still a wise option. =item decode_utf8 This attributes defaults to TRUE. While I, fields that are valid UTF-8, are automatically set to be UTF-8, so that $csv->parse ("\xC4\xA8\n"); results in PV("\304\250"\0) [UTF8 "\x{128}"] Sometimes it might not be a desired action. To prevent those upgrades, set this attribute to false, and the result will be PV("\304\250"\0) =item types A set of column types; this attribute is immediately passed to the I method below. You must not set this attribute otherwise, except for using the I method. For details see the description of the I method below. =item always_quote By default the generated fields are quoted only, if they need to, for example, if they contain the separator. If you set this attribute to a TRUE value, then all defined fields will be quoted. This is typically easier to handle in external applications. =item quote_space By default, a space in a field would trigger quotation. As no rule exists this to be forced in CSV, nor any for the opposite, the default is true for safety. You can exclude the space from this trigger by setting this option to 0. =item quote_null By default, a NULL byte in a field would be escaped. This attribute enables you to treat the NULL byte as a simple binary character in binary mode (the C<{ binary =E 1 }> is set). The default is true. You can prevent NULL escapes by setting this attribute to 0. =item quote_binary By default, all "unsafe" bytes inside a string cause the combined field to be quoted. By setting this attribute to 0, you can disable that trigger for bytes >= 0x7f. =item keep_meta_info By default, the parsing of input lines is as simple and fast as possible. However, some parsing information - like quotation of the original field - is lost in that process. Set this flag to true to be able to retrieve that information after parsing with the methods C, C, and C described below. Default is false. =item verbatim This is a quite controversial attribute to set, but it makes hard things possible. The basic thought behind this is to tell the parser that the normally special characters newline (NL) and Carriage Return (CR) will not be special when this flag is set, and be dealt with as being ordinary binary characters. This will ease working with data with embedded newlines. When C is used with C, C auto-chomp's every line. Imagine a file format like M^^Hans^Janssen^Klas 2\n2A^Ja^11-06-2007#\r\n where, the line ending is a very specific "#\r\n", and the sep_char is a ^ (caret). None of the fields is quoted, but embedded binary data is likely to be present. With the specific line ending, that should not be too hard to detect. By default, Text::CSV' parse function however is instructed to only know about "\n" and "\r" to be legal line endings, and so has to deal with the embedded newline as a real end-of-line, so it can scan the next line if binary is true, and the newline is inside a quoted field. With this attribute however, we can tell parse () to parse the line as if \n is just nothing more than a binary character. For parse () this means that the parser has no idea about line ending anymore, and getline () chomps line endings on reading. =item auto_diag Set to true will cause C to be automatically be called in void context upon errors. If set to a value greater than 1, it will die on errors instead of warn. To check future plans and a difference in XS version, please see to L. =back To sum it up, $csv = Text::CSV->new (); is equivalent to $csv = Text::CSV->new ({ quote_char => '"', escape_char => '"', sep_char => ',', eol => $\, always_quote => 0, quote_space => 1, quote_null => 1, binary => 0, decode_utf8 => 0, keep_meta_info => 0, allow_loose_quotes => 0, allow_loose_escapes => 0, allow_whitespace => 0, blank_is_undef => 0, empty_is_undef => 0, verbatim => 0, auto_diag => 0, }); For all of the above mentioned flags, there is an accessor method available where you can inquire for the current value, or change the value my $quote = $csv->quote_char; $csv->binary (1); It is unwise to change these settings halfway through writing CSV data to a stream. If however, you want to create a new stream using the available CSV object, there is no harm in changing them. If the C constructor call fails, it returns C, and makes the fail reason available through the C method. $csv = Text::CSV->new ({ ecs_char => 1 }) or die "" . Text::CSV->error_diag (); C will return a string like "INI - Unknown attribute 'ecs_char'" =head2 print $status = $csv->print ($io, $colref); Similar to C, but more efficient. It expects an array ref as input (not an array!) and the resulting string is not really created (XS version), but immediately written to the I<$io> object, typically an IO handle or any other object that offers a I method. Note, this implies that the following is wrong in perl 5.005_xx and older: open FILE, ">", "whatever"; $status = $csv->print (\*FILE, $colref); as in perl 5.005 and older, the glob C<\*FILE> is not an object, thus it does not have a print method. The solution is to use an IO::File object or to hide the glob behind an IO::Wrap object. See L and L for details. For performance reasons the print method doesn't create a result string. (If its backend is PP version, result strings are created internally.) In particular the I<$csv-Estring ()>, I<$csv-Estatus ()>, I<$csv->fields ()> and I<$csv-Eerror_input ()> methods are meaningless after executing this method. =head2 combine $status = $csv->combine (@columns); This object function constructs a CSV string from the arguments, returning success or failure. Failure can result from lack of arguments or an argument containing an invalid character. Upon success, C can be called to retrieve the resultant CSV string. Upon failure, the value returned by C is undefined and C can be called to retrieve an invalid argument. =head2 string $line = $csv->string (); This object function returns the input to C or the resultant CSV string of C, whichever was called more recently. =head2 getline $colref = $csv->getline ($io); This is the counterpart to print, like parse is the counterpart to combine: It reads a row from the IO object $io using $io->getline () and parses this row into an array ref. This array ref is returned by the function or undef for failure. When fields are bound with C, the return value is a reference to an empty list. The I<$csv-Estring ()>, I<$csv-Efields ()> and I<$csv-Estatus ()> methods are meaningless, again. =head2 getline_all $arrayref = $csv->getline_all ($io); $arrayref = $csv->getline_all ($io, $offset); $arrayref = $csv->getline_all ($io, $offset, $length); This will return a reference to a list of C results. In this call, C is disabled. If C<$offset> is negative, as with C, only the last C records of C<$io> are taken into consideration. Given a CSV file with 10 lines: lines call ----- --------------------------------------------------------- 0..9 $csv->getline_all ($io) # all 0..9 $csv->getline_all ($io, 0) # all 8..9 $csv->getline_all ($io, 8) # start at 8 - $csv->getline_all ($io, 0, 0) # start at 0 first 0 rows 0..4 $csv->getline_all ($io, 0, 5) # start at 0 first 5 rows 4..5 $csv->getline_all ($io, 4, 2) # start at 4 first 2 rows 8..9 $csv->getline_all ($io, -2) # last 2 rows 6..7 $csv->getline_all ($io, -4, 2) # first 2 of last 4 rows =head2 parse $status = $csv->parse ($line); This object function decomposes a CSV string into fields, returning success or failure. Failure can result from a lack of argument or the given CSV string is improperly formatted. Upon success, C can be called to retrieve the decomposed fields . Upon failure, the value returned by C is undefined and C can be called to retrieve the invalid argument. You may use the I method for setting column types. See the description below. =head2 getline_hr The C and C methods work together to allow you to have rows returned as hashrefs. You must call C first to declare your column names. $csv->column_names (qw( code name price description )); $hr = $csv->getline_hr ($io); print "Price for $hr->{name} is $hr->{price} EUR\n"; C will croak if called before C. =head2 getline_hr_all $arrayref = $csv->getline_hr_all ($io); $arrayref = $csv->getline_hr_all ($io, $offset); $arrayref = $csv->getline_hr_all ($io, $offset, $length); This will return a reference to a list of C results. In this call, C is disabled. =head2 print_hr $csv->print_hr ($io, $ref); Provides an easy way to print a C<$ref> as fetched with L provided the column names are set with L. It is just a wrapper method with basic parameter checks over $csv->print ($io, [ map { $ref->{$_} } $csv->column_names ]); =head2 column_names Set the keys that will be used in the C calls. If no keys (column names) are passed, it'll return the current setting. C accepts a list of scalars (the column names) or a single array_ref, so you can pass C $csv->column_names ($csv->getline ($io)); C does B checking on duplicates at all, which might lead to unwanted results. Undefined entries will be replaced with the string C<"\cAUNDEF\cA">, so $csv->column_names (undef, "", "name", "name"); $hr = $csv->getline_hr ($io); Will set C<$hr->{"\cAUNDEF\cA"}> to the 1st field, C<$hr->{""}> to the 2nd field, and C<$hr->{name}> to the 4th field, discarding the 3rd field. C croaks on invalid arguments. =head2 bind_columns Takes a list of references to scalars to store the fields fetched C in. When you don't pass enough references to store the fetched fields in, C will fail. If you pass more than there are fields to return, the remaining references are left untouched. $csv->bind_columns (\$code, \$name, \$price, \$description); while ($csv->getline ($io)) { print "The price of a $name is \x{20ac} $price\n"; } =head2 eof $eof = $csv->eof (); If C or C was used with an IO stream, this method will return true (1) if the last call hit end of file, otherwise it will return false (''). This is useful to see the difference between a failure and end of file. =head2 types $csv->types (\@tref); This method is used to force that columns are of a given type. For example, if you have an integer column, two double columns and a string column, then you might do a $csv->types ([Text::CSV::IV (), Text::CSV::NV (), Text::CSV::NV (), Text::CSV::PV ()]); Column types are used only for decoding columns, in other words by the I and I methods. You can unset column types by doing a $csv->types (undef); or fetch the current type settings with $types = $csv->types (); =over 4 =item IV Set field type to integer. =item NV Set field type to numeric/float. =item PV Set field type to string. =back =head2 fields @columns = $csv->fields (); This object function returns the input to C or the resultant decomposed fields of C successful , whichever was called more recently. Note that the return value is undefined after using C, which does not fill the data structures returned by C. =head2 meta_info @flags = $csv->meta_info (); This object function returns the flags of the input to C or the flags of the resultant decomposed fields of C, whichever was called more recently. For each field, a meta_info field will hold flags that tell something about the field returned by the C method or passed to the C method. The flags are bit-wise-or'd like: =over 4 =item 0x0001 The field was quoted. =item 0x0002 The field was binary. =back See the C methods below. =head2 is_quoted my $quoted = $csv->is_quoted ($column_idx); Where C<$column_idx> is the (zero-based) index of the column in the last result of C. This returns a true value if the data in the indicated column was enclosed in C quotes. This might be important for data where C<,20070108,> is to be treated as a numeric value, and where C<,"20070108",> is explicitly marked as character string data. =head2 is_binary my $binary = $csv->is_binary ($column_idx); Where C<$column_idx> is the (zero-based) index of the column in the last result of C. This returns a true value if the data in the indicated column contained any byte in the range [\x00-\x08,\x10-\x1F,\x7F-\xFF] =head2 is_missing my $missing = $csv->is_missing ($column_idx); Where C<$column_idx> is the (zero-based) index of the column in the last result of L. while (my $hr = $csv->getline_hr ($fh)) { $csv->is_missing (0) and next; # This was an empty line } When using L for parsing, it is impossible to tell if the fields are C because they where not filled in the CSV stream or because they were not read at all, as B the fields defined by L are set in the hash-ref. If you still need to know if all fields in each row are provided, you should enable C so you can check the flags. =head2 status $status = $csv->status (); This object function returns success (or failure) of C or C, whichever was called more recently. =head2 error_input $bad_argument = $csv->error_input (); This object function returns the erroneous argument (if it exists) of C or C, whichever was called more recently. =head2 error_diag Text::CSV->error_diag (); $csv->error_diag (); $error_code = 0 + $csv->error_diag (); $error_str = "" . $csv->error_diag (); ($cde, $str, $pos) = $csv->error_diag (); If (and only if) an error occurred, this function returns the diagnostics of that error. If called in void context, it will print the internal error code and the associated error message to STDERR. If called in list context, it will return the error code and the error message in that order. If the last error was from parsing, the third value returned is the best guess at the location within the line that was being parsed. It's value is 1-based. Note: C<$pos> returned by the backend Text::CSV_PP does not show the error point in many cases (see to the below line). It is for conscience's sake in using Text::CSV_PP. If called in scalar context, it will return the diagnostics in a single scalar, a-la $!. It will contain the error code in numeric context, and the diagnostics message in string context. Depending on the used worker module, returned diagnostics is different. Text::CSV_XS parses csv strings by dividing one character while Text::CSV_PP by using the regular expressions. That difference makes the different cause of the failure. When called as a class method or a direct function call, the error diag is that of the last C call. =head2 record_number $recno = $csv->record_number (); Returns the records parsed by this csv instance. This value should be more accurate than C<$.> when embedded newlines come in play. Records written by this instance are not counted. =head2 SetDiag $csv->SetDiag (0); Use to reset the diagnostics if you are dealing with errors. =head2 Some methods are Text::CSV only. =over =item backend Returns the backend module name called by Text::CSV. C is an alias. =item is_xs Returns true value if Text::CSV or the object uses XS module as worker. =item is_pp Returns true value if Text::CSV or the object uses pure-Perl module as worker. =back =head1 DIAGNOSTICS If an error occurred, $csv->error_diag () can be used to get more information on the cause of the failure. Note that for speed reasons, the internal value is never cleared on success, so using the value returned by error_diag () in normal cases - when no error occurred - may cause unexpected results. This function changes depending on the used module (XS or PurePerl). See to L and L. =head2 HISTORY AND WORKER MODULES This module, L was firstly written by Alan Citterman which could deal with B. Then, Jochen Wiedmann wrote L which has the B. This XS version is maintained by H.Merijn Brand and L written by Makamaka was pure-Perl version of Text::CSV_XS. Now, Text::CSV was rewritten by Makamaka and become a wrapper to Text::CSV_XS or Text::CSV_PP. Text::CSV_PP will be bundled in this distribution. When you use Text::CSV, it calls a backend worker module - L or L. By default, Text::CSV tries to use Text::CSV_XS which must be complied and installed properly. If this call is fail, Text::CSV uses L. The required Text::CSV_XS version is I<0.41> in Text::CSV version 1.03. If you set an environment variable C, The calling action will be changed. =over =item PERL_TEXT_CSV = 0 =item PERL_TEXT_CSV = 'Text::CSV_PP' Always use Text::CSV_PP =item PERL_TEXT_CSV = 1 =item PERL_TEXT_CSV = 'Text::CSV_XS,Text::CSV_PP' (The default) Use compiled Text::CSV_XS if it is properly compiled & installed, otherwise use Text::CSV_PP =item PERL_TEXT_CSV = 2 =item PERL_TEXT_CSV = 'Text::CSV_XS' Always use compiled Text::CSV_XS, die if it isn't properly compiled & installed. =back These ideas come from L mechanism. example: BEGIN { $ENV{PERL_TEXT_CSV} = 0 } use Text::CSV; # always uses Text::CSV_PP In future, it may be able to specify another module. =head1 TODO =over =item Wrapper mechanism Currently the wrapper mechanism is to change symbolic table for speed. for my $method (@PublicMethods) { *{"Text::CSV::$method"} = \&{"$class\::$method"}; } But how about it - calling worker module object? sub parse { my $self = shift; $self->{_WORKER_OBJECT}->parse(@_); # XS or PP CSV object } =back See to L and L. =head1 SEE ALSO L, L and L. =head1 AUTHORS and MAINTAINERS Alan Citterman Falan[at]mfgrtl.comE> wrote the original Perl module. Please don't send mail concerning Text::CSV to Alan, as he's not a present maintainer. Jochen Wiedmann Fjoe[at]ispsoft.deE> rewrote the encoding and decoding in C by implementing a simple finite-state machine and added the variable quote, escape and separator characters, the binary mode and the print and getline methods. See ChangeLog releases 0.10 through 0.23. H.Merijn Brand Fh.m.brand[at]xs4all.nlE> cleaned up the code, added the field flags methods, wrote the major part of the test suite, completed the documentation, fixed some RT bugs. See ChangeLog releases 0.25 and on. Makamaka Hannyaharamitu, Emakamaka[at]cpan.orgE wrote Text::CSV_PP which is the pure-Perl version of Text::CSV_XS. New Text::CSV (since 0.99) is maintained by Makamaka. =head1 COPYRIGHT AND LICENSE Text::CSV Copyright (C) 1997 Alan Citterman. All rights reserved. Copyright (C) 2007-2015 Makamaka Hannyaharamitu. Text::CSV_PP: Copyright (C) 2005-2015 Makamaka Hannyaharamitu. Text:CSV_XS: Copyright (C) 2007-2015 H.Merijn Brand for PROCURA B.V. Copyright (C) 1998-2001 Jochen Wiedmann. All rights reserved. Portions Copyright (C) 1997 Alan Citterman. All rights reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut Text-CSV-1.33/lib/Text/CSV_PP.pm0000644000175000017500000016226212475365663014621 0ustar reonreonpackage 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.33'; sub PV { 0 } sub IV { 1 } sub NV { 2 } sub IS_QUOTED () { 0x0001; } sub IS_BINARY () { 0x0002; } sub IS_MISSING () { 0x0010; } my $ERRORS = { # PP and XS 1000 => "INI - constructor failed", 1001 => "sep_char is equal to quote_char or escape_char", 1002 => "INI - allow_whitespace with escape_char or quote_char SP or TAB", 1003 => "INI - \r or \n in main attr not allowed", 2010 => "ECR - QUO char inside quotes followed by CR not part of EOL", 2011 => "ECR - Characters after end of quoted field", 2021 => "EIQ - NL char inside quotes, binary off", 2022 => "EIQ - CR char inside quotes, binary off", 2025 => "EIQ - Loose unescaped escape", 2026 => "EIQ - Binary character inside quoted field, binary off", 2027 => "EIQ - Quoted field not terminated", 2030 => "EIF - NL char inside unquoted verbatim, binary off", 2031 => "EIF - CR char is first char of field, not part of EOL", 2032 => "EIF - CR char inside unquoted, not part of EOL", 2034 => "EIF - Loose unescaped quote", 2037 => "EIF - Binary character in unquoted field, binary off", 2110 => "ECB - Binary character in Combine, binary off", 2200 => "EIO - print to IO failed. See errno", # PP Only Error 4002 => "EIQ - Unescaped ESC in quoted field", 4003 => "EIF - ESC CR", 4004 => "EUF - ", # Hash-Ref errors 3001 => "EHR - Unsupported syntax for column_names ()", 3002 => "EHR - getline_hr () called before column_names ()", 3003 => "EHR - bind_columns () and column_names () fields count mismatch", 3004 => "EHR - bind_columns () only accepts refs to scalars", 3006 => "EHR - bind_columns () did not pass enough refs for parsed fields", 3007 => "EHR - bind_columns needs refs to writable scalars", 3008 => "EHR - unexpected error in bound fields", 3009 => "EHR - print_hr () called before column_names ()", 3010 => "EHR - print_hr () called with invalid arguments", 0 => "", }; my $last_new_error = ''; my $last_new_err_num; my %def_attr = ( quote_char => '"', escape_char => '"', sep_char => ',', eol => defined $\ ? $\ : '', always_quote => 0, binary => 0, keep_meta_info => 0, allow_loose_quotes => 0, allow_loose_escapes => 0, allow_unquoted_escape => 0, allow_whitespace => 0, chomp_verbatim => 0, types => undef, verbatim => 0, blank_is_undef => 0, empty_is_undef => 0, auto_diag => 0, quote_space => 1, quote_null => 1, quote_binary => 1, diag_verbose => 0, decode_utf8 => 1, _EOF => 0, _RECNO => 0, _STATUS => undef, _FIELDS => undef, _FFLAGS => undef, _STRING => undef, _ERROR_INPUT => undef, _ERROR_DIAG => undef, _COLUMN_NAMES => undef, _BOUND_COLUMNS => undef, ); BEGIN { if ( $] < 5.006 ) { $INC{'bytes.pm'} = 1 unless $INC{'bytes.pm'}; # dummy no strict 'refs'; *{"utf8::is_utf8"} = sub { 0; }; *{"utf8::decode"} = sub { }; } elsif ( $] < 5.008 ) { no strict 'refs'; *{"utf8::is_utf8"} = sub { 0; }; *{"utf8::decode"} = sub { }; } elsif ( !defined &utf8::is_utf8 ) { require Encode; *utf8::is_utf8 = *Encode::is_utf8; } eval q| require Scalar::Util |; if ( $@ ) { eval q| require B |; if ( $@ ) { Carp::croak $@; } else { *Scalar::Util::readonly = sub (\$) { my $b = B::svref_2object( $_[0] ); $b->FLAGS & 0x00800000; # SVf_READONLY? } } } } ################################################################################ # version ################################################################################ sub version { return $VERSION; } ################################################################################ # new ################################################################################ sub _check_sanity { my ( $self ) = @_; for ( qw( sep_char quote_char escape_char ) ) { ( exists $self->{$_} && defined $self->{$_} && $self->{$_} =~ m/[\r\n]/ ) and return 1003; } if ( $self->{allow_whitespace} and ( defined $self->{quote_char} && $self->{quote_char} =~ m/^[ \t]$/ ) || ( defined $self->{escape_char} && $self->{escape_char} =~ m/^[ \t]$/ ) ) { #$last_new_error = "INI - allow_whitespace with escape_char or quote_char SP or TAB"; #$last_new_err_num = 1002; return 1002; } return 0; } sub new { my $proto = shift; my $attr = @_ > 0 ? shift : {}; $last_new_error = 'usage: my $csv = Text::CSV_PP->new ([{ option => value, ... }]);'; $last_new_err_num = 1000; return unless ( defined $attr and ref($attr) eq 'HASH' ); my $class = ref($proto) || $proto or return; my $self = { %def_attr }; for my $prop (keys %$attr) { # if invalid attr, return undef unless ($prop =~ /^[a-z]/ && exists $def_attr{$prop}) { $last_new_error = "INI - Unknown attribute '$prop'"; error_diag() if $attr->{ auto_diag }; return; } $self->{$prop} = $attr->{$prop}; } my $ec = _check_sanity( $self ); if ( $ec ) { $last_new_error = $ERRORS->{ $ec }; $last_new_err_num = $ec; return; #$class->SetDiag ($ec); } $last_new_error = ''; defined $\ and $self->{eol} = $\; bless $self, $class; $self->types( $self->{types} ) if( exists( $self->{types} ) ); return $self; } ################################################################################ # status ################################################################################ sub status { $_[0]->{_STATUS}; } ################################################################################ # error_input ################################################################################ sub error_input { $_[0]->{_ERROR_INPUT}; } ################################################################################ # error_diag ################################################################################ sub error_diag { my $self = shift; my @diag = (0, $last_new_error, 0); unless ($self and ref $self) { # Class method or direct call $last_new_error and $diag[0] = defined $last_new_err_num ? $last_new_err_num : 1000; } elsif ( $self->isa (__PACKAGE__) and defined $self->{_ERROR_DIAG} ) { @diag = ( 0 + $self->{_ERROR_DIAG}, $ERRORS->{ $self->{_ERROR_DIAG} } ); exists $self->{_ERROR_POS} and $diag[2] = 1 + $self->{_ERROR_POS}; } my $context = wantarray; my $diagobj = bless \@diag, 'Text::CSV::ErrorDiag'; unless (defined $context) { # Void context if ( $diag[0] ) { my $msg = "# CSV_PP ERROR: " . $diag[0] . " - $diag[1]\n"; ref $self ? ( $self->{auto_diag} > 1 ? die $msg : warn $msg ) : warn $msg; } return; } return $context ? @diag : $diagobj; } sub record_number { return shift->{_RECNO}; } ################################################################################ # string ################################################################################ *string = \&_string; sub _string { defined $_[0]->{_STRING} ? ${ $_[0]->{_STRING} } : undef; } ################################################################################ # fields ################################################################################ *fields = \&_fields; sub _fields { ref($_[0]->{_FIELDS}) ? @{$_[0]->{_FIELDS}} : undef; } ################################################################################ # combine ################################################################################ *combine = \&_combine; sub _combine { my ($self, @part) = @_; # at least one argument was given for "combining"... return $self->{_STATUS} = 0 unless(@part); $self->{_FIELDS} = \@part; $self->{_ERROR_INPUT} = undef; $self->{_STRING} = ''; $self->{_STATUS} = 0; my ($always_quote, $binary, $quot, $sep, $esc, $empty_is_undef, $quote_space, $quote_null, $quote_binary ) = @{$self}{qw/always_quote binary quote_char sep_char escape_char empty_is_undef quote_space quote_null quote_binary/}; if(!defined $quot){ $quot = ''; } return $self->_set_error_diag(1001) if ($sep eq $esc or $sep eq $quot); my $re_esc = $self->{_re_comb_escape}->{$quot}->{$esc} ||= qr/(\Q$quot\E|\Q$esc\E)/; my $re_sp = $self->{_re_comb_sp}->{$sep}->{$quote_space} ||= ( $quote_space ? qr/[\s\Q$sep\E]/ : qr/[\Q$sep\E]/ ); my $must_be_quoted; for my $column (@part) { unless (defined $column) { $column = ''; next; } elsif ( !$binary ) { $binary = 1 if utf8::is_utf8 $column; } if (!$binary and $column =~ /[^\x09\x20-\x7E]/) { # an argument contained an invalid character... $self->{_ERROR_INPUT} = $column; $self->_set_error_diag(2110); return $self->{_STATUS}; } $must_be_quoted = 0; if($quot ne '' and $column =~ s/$re_esc/$esc$1/g){ $must_be_quoted++; } if($column =~ /$re_sp/){ $must_be_quoted++; } if( $binary and $quote_null ){ use bytes; $must_be_quoted++ if ( $column =~ s/\0/${esc}0/g || ($quote_binary && $column =~ /[\x00-\x1f\x7f-\xa0]/) ); } if($always_quote or $must_be_quoted){ $column = $quot . $column . $quot; } } $self->{_STRING} = \do { join($sep, @part) . ( defined $self->{eol} ? $self->{eol} : '' ) }; $self->{_STATUS} = 1; return $self->{_STATUS}; } ################################################################################ # parse ################################################################################ my %allow_eol = ("\r" => 1, "\r\n" => 1, "\n" => 1, "" => 1); *parse = \&_parse; sub _parse { my ($self, $line) = @_; @{$self}{qw/_STRING _FIELDS _STATUS _ERROR_INPUT/} = ( \do{ defined $line ? "$line" : undef }, undef, 0, $line ); return 0 if(!defined $line); my ($binary, $quot, $sep, $esc, $types, $keep_meta_info, $allow_whitespace, $eol, $blank_is_undef, $empty_is_undef, $unquot_esc, $decode_utf8) = @{$self}{ qw/binary quote_char sep_char escape_char types keep_meta_info allow_whitespace eol blank_is_undef empty_is_undef allow_unquoted_escape decode_utf8/ }; $sep = ',' unless (defined $sep); $esc = "\0" unless (defined $esc); $quot = "\0" unless (defined $quot); my $quot_is_null = $quot eq "\0"; # in this case, any fields are not interpreted as quoted data. return $self->_set_error_diag(1001) if (($sep eq $esc or $sep eq $quot) and $sep ne "\0"); my $meta_flag = $keep_meta_info ? [] : undef; my $re_split = $self->{_re_split}->{$quot}->{$esc}->{$sep} ||= _make_regexp_split_column($esc, $quot, $sep); my $re_quoted = $self->{_re_quoted}->{$quot} ||= qr/^\Q$quot\E(.*)\Q$quot\E$/s; my $re_in_quot_esp1 = $self->{_re_in_quot_esp1}->{$esc} ||= qr/\Q$esc\E(.)/; my $re_in_quot_esp2 = $self->{_re_in_quot_esp2}->{$quot}->{$esc} ||= qr/[\Q$quot$esc$sep\E0]/; my $re_quot_char = $self->{_re_quot_char}->{$quot} ||= qr/\Q$quot\E/; my $re_esc = $self->{_re_esc}->{$quot}->{$esc} ||= qr/\Q$esc\E(\Q$quot\E|\Q$esc\E|\Q$sep\E|0)/; my $re_invalid_quot = $self->{_re_invalid_quot}->{$quot}->{$esc} ||= qr/^$re_quot_char|[^\Q$re_esc\E]$re_quot_char/; if ($allow_whitespace) { $re_split = $self->{_re_split_allow_sp}->{$quot}->{$esc}->{$sep} ||= _make_regexp_split_column_allow_sp($esc, $quot, $sep); } if ($unquot_esc) { $re_split = $self->{_re_split_allow_unqout_esc}->{$quot}->{$esc}->{$sep} ||= _make_regexp_split_column_allow_unqout_esc($esc, $quot, $sep); } my $palatable = 1; my @part = (); my $i = 0; my $flag; if (defined $eol and $eol eq "\r") { $line =~ s/[\r ]*\r[ ]*$//; } if ($self->{verbatim}) { $line .= $sep; } else { if (defined $eol and !$allow_eol{$eol}) { $line .= $sep; } else { $line =~ s/(?:\x0D\x0A|\x0A)?$|(?:\x0D\x0A|\x0A)[ ]*$/$sep/; } } my $pos = 0; my $utf8 = 1 if $decode_utf8 and utf8::is_utf8( $line ); # if decode_utf8 is true(default) and UTF8 marked, flag on. for my $col ( $line =~ /$re_split/g ) { if ($keep_meta_info) { $flag = 0x0000; $flag |= IS_BINARY if ($col =~ /[^\x09\x20-\x7E]/); } $pos += length $col; if ( ( !$binary and !$utf8 ) and $col =~ /[^\x09\x20-\x7E]/) { # Binary character, binary off if ( not $quot_is_null and $col =~ $re_quoted ) { $self->_set_error_diag( $col =~ /\n([^\n]*)/ ? (2021, $pos - 1 - length $1) : $col =~ /\r([^\r]*)/ ? (2022, $pos - 1 - length $1) : (2026, $pos -2) # Binary character inside quoted field, binary off ); } else { $self->_set_error_diag( $col =~ /\Q$quot\E(.*)\Q$quot\E\r$/ ? (2010, $pos - 2) : $col =~ /\n/ ? (2030, $pos - length $col) : $col =~ /^\r/ ? (2031, $pos - length $col) : $col =~ /\r([^\r]*)/ ? (2032, $pos - 1 - length $1) : (2037, $pos - length $col) # Binary character in unquoted field, binary off ); } $palatable = 0; last; } if ( ($utf8 and !$binary) and $col =~ /\n|\0/ ) { # \n still needs binary (Text::CSV_XS 0.51 compat) $self->_set_error_diag(2021, $pos); $palatable = 0; last; } if ( not $quot_is_null and $col =~ $re_quoted ) { $flag |= IS_QUOTED if ($keep_meta_info); $col = $1; my $flag_in_quot_esp; while ( $col =~ /$re_in_quot_esp1/g ) { my $str = $1; $flag_in_quot_esp = 1; if ($str !~ $re_in_quot_esp2) { unless ($self->{allow_loose_escapes}) { $self->_set_error_diag( 2025, $pos - 2 ); # Needless ESC in quoted field $palatable = 0; last; } unless ($self->{allow_loose_quotes}) { $col =~ s/\Q$esc\E(.)/$1/g; } } } last unless ( $palatable ); unless ( $flag_in_quot_esp ) { if ($col =~ /(?_set_error_diag( 4002, $pos - 1 ); # No escaped ESC in quoted field $palatable = 0; last; } } $col =~ s{$re_esc}{$1 eq '0' ? "\0" : $1}eg; if ( $empty_is_undef and length($col) == 0 ) { $col = undef; } if ($types and $types->[$i]) { # IV or NV _check_type(\$col, $types->[$i]); } } # quoted but invalid elsif ( not $quot_is_null and $col =~ $re_invalid_quot ) { unless ($self->{allow_loose_quotes} and $col =~ /$re_quot_char/) { $self->_set_error_diag( $col =~ /^\Q$quot\E(.*)\Q$quot\E.$/s ? (2011, $pos - 2) : $col =~ /^$re_quot_char/ ? (2027, $pos - 1) : (2034, $pos - length $col) # Loose unescaped quote ); $palatable = 0; last; } } elsif ($types and $types->[$i]) { # IV or NV _check_type(\$col, $types->[$i]); } # unquoted else { if (!$self->{verbatim} and $col =~ /\r\n|\n/) { $col =~ s/(?:\r\n|\n).*$//sm; } if ($col =~ /\Q$esc\E\r$/) { # for t/15_flags : test 165 'ESC CR' at line 203 $self->_set_error_diag( 4003, $pos ); $palatable = 0; last; } if ($col =~ /.\Q$esc\E$/) { # for t/65_allow : test 53-54 parse('foo\') at line 62, 65 $self->_set_error_diag( 4004, $pos ); $palatable = 0; last; } if ( $col eq '' and $blank_is_undef ) { $col = undef; } if ( $empty_is_undef and length($col) == 0 ) { $col = undef; } if ( $unquot_esc ) { $col =~ s/\Q$esc\E(.)/$1/g; } } utf8::encode($col) if $utf8; if ( $decode_utf8 && defined $col && _is_valid_utf8($col) ) { utf8::decode($col); } push @part,$col; push @{$meta_flag}, $flag if ($keep_meta_info); $self->{ _RECNO }++; $i++; } if ($palatable and ! @part) { $palatable = 0; } if ($palatable) { $self->{_ERROR_INPUT} = undef; $self->{_FIELDS} = \@part; if ( $self->{_BOUND_COLUMNS} ) { my @vals = @part; my ( $max, $count ) = ( scalar @vals, 0 ); if ( @{ $self->{_BOUND_COLUMNS} } < $max ) { $self->_set_error_diag(3006); return; } for ( my $i = 0; $i < $max; $i++ ) { my $bind = $self->{_BOUND_COLUMNS}->[ $i ]; if ( Scalar::Util::readonly( $$bind ) ) { $self->_set_error_diag(3008); return; } $$bind = $vals[ $i ]; } } } $self->{_FFLAGS} = $keep_meta_info ? $meta_flag : []; return $self->{_STATUS} = $palatable; } sub _make_regexp_split_column { my ($esc, $quot, $sep) = @_; if ( $quot eq '' ) { return qr/([^\Q$sep\E]*)\Q$sep\E/s; } return qr/( \Q$quot\E [^\Q$quot$esc\E]*(?:\Q$esc\E[\Q$quot$esc\E0][^\Q$quot$esc\E]*)* \Q$quot\E | # or \Q$quot\E (?:\Q$esc\E[\Q$quot$esc$sep\E0]|[^\Q$quot$esc$sep\E])* \Q$quot\E | # or [^\Q$sep\E]* ) \Q$sep\E /xs; } sub _make_regexp_split_column_allow_unqout_esc { my ($esc, $quot, $sep) = @_; return qr/( \Q$quot\E [^\Q$quot$esc\E]*(?:\Q$esc\E[\Q$quot$esc\E0][^\Q$quot$esc\E]*)* \Q$quot\E | # or \Q$quot\E (?:\Q$esc\E[\Q$quot$esc$sep\E0]|[^\Q$quot$esc$sep\E])* \Q$quot\E | # or (?:\Q$esc\E[\Q$quot$esc$sep\E0]|[^\Q$quot$esc$sep\E])* | # or [^\Q$sep\E]* ) \Q$sep\E /xs; } sub _make_regexp_split_column_allow_sp { my ($esc, $quot, $sep) = @_; # if separator is space or tab, don't count that separator # as whitespace --- patched by Mike O'Sullivan my $ws = $sep eq ' ' ? '[\x09]' : $sep eq "\t" ? '[\x20]' : '[\x20\x09]' ; if ( $quot eq '' ) { return qr/$ws*([^\Q$sep\E]?)$ws*\Q$sep\E$ws*/s; } qr/$ws* ( \Q$quot\E [^\Q$quot$esc\E]*(?:\Q$esc\E[\Q$quot\E][^\Q$quot$esc\E]*)* \Q$quot\E | # or [^\Q$sep\E]*? ) $ws*\Q$sep\E$ws* /xs; } ################################################################################ # print ################################################################################ sub print { my ($self, $io, $cols) = @_; require IO::Handle; if(ref($cols) ne 'ARRAY'){ Carp::croak("Expected fields to be an array ref"); } $self->_combine(@$cols) or return ''; local $\ = ''; $io->print( $self->_string ) or $self->_set_error_diag(2200); } sub print_hr { my ($self, $io, $hr) = @_; $self->{_COLUMN_NAMES} or $self->_set_error_diag(3009); ref $hr eq "HASH" or $self->_set_error_diag(3010); $self->print ($io, [ map { $hr->{$_} } $self->column_names ]); } ################################################################################ # getline ################################################################################ sub getline { my ($self, $io) = @_; require IO::Handle; $self->{_EOF} = $io->eof ? 1 : ''; my $quot = $self->{quote_char}; my $sep = $self->{sep_char}; my $re = defined $quot ? qr/(?:\Q$quot\E)/ : undef; my $eol = $self->{eol}; local $/ = $eol if ( defined $eol and $eol ne '' ); my $line = $io->getline(); # AUTO DETECTION EOL CR if ( defined $line and defined $eol and $eol eq '' and $line =~ /[^\r]\r[^\r\n]/ and eof ) { $self->{_AUTO_DETECT_CR} = 1; $self->{eol} = "\r"; seek( $io, 0, 0 ); # restart return $self->getline( $io ); } if ( $re and defined $line ) { LOOP: { my $is_continued = scalar(my @list = $line =~ /$re/g) % 2; # if line is valid, quot is even if ( $self->{allow_loose_quotes } ) { $is_continued = 0; } elsif ( $line =~ /${re}0/ ) { # null suspicion case $is_continued = $line =~ qr/ ^ ( (?: $re # $quote (?: $re$re # escaped $quote | ${re}0 # or escaped zero | [^$quot] # or exceptions of $quote )* $re # $quote [^0$quot] # non zero or $quote ) | (?:[^$quot]*) # exceptions of $quote )+ $ /x ? 0 : 1; } if ( $is_continued and !$io->eof) { $line .= $io->getline(); goto LOOP; } } } $line =~ s/\Q$eol\E$// if ( defined $line and defined $eol and $eol ne '' ); $self->_parse($line); if ( eof ) { $self->{_AUTO_DETECT_CR} = 0; } return unless $self->{_STATUS}; return $self->{_BOUND_COLUMNS} ? [] : [ $self->_fields() ]; } ################################################################################ # getline_all ################################################################################ sub getline_all { my ( $self, $io, $offset, $len ) = @_; my @list; my $tail; my $n = 0; $offset ||= 0; if ( $offset < 0 ) { $tail = -$offset; $offset = 0; } while ( my $row = $self->getline($io) ) { next if $offset && $offset-- > 0; # skip last if defined $len && !$tail && $n >= $len; # exceeds limit size push @list, $row; ++$n; if ( $tail && $n > $tail ) { shift @list; } } if ( $tail && defined $len && $n > $len ) { @list = splice( @list, 0, $len); } return \@list; } ################################################################################ # getline_hr ################################################################################ sub getline_hr { my ( $self, $io) = @_; my %hr; unless ( $self->{_COLUMN_NAMES} ) { $self->SetDiag( 3002 ); } my $fr = $self->getline( $io ) or return undef; if ( ref $self->{_FFLAGS} ) { $self->{_FFLAGS}[$_] = IS_MISSING for ($#{$fr} + 1) .. $#{$self->{_COLUMN_NAMES}}; } @hr{ @{ $self->{_COLUMN_NAMES} } } = @$fr; \%hr; } ################################################################################ # getline_hr_all ################################################################################ sub getline_hr_all { my ( $self, $io, @args ) = @_; my %hr; unless ( $self->{_COLUMN_NAMES} ) { $self->SetDiag( 3002 ); } my @cn = @{$self->{_COLUMN_NAMES}}; return [ map { my %h; @h{ @cn } = @$_; \%h } @{ $self->getline_all( $io, @args ) } ]; } ################################################################################ # column_names ################################################################################ sub column_names { my ( $self, @columns ) = @_; @columns or return defined $self->{_COLUMN_NAMES} ? @{$self->{_COLUMN_NAMES}} : undef; @columns == 1 && ! defined $columns[0] and return $self->{_COLUMN_NAMES} = undef; if ( @columns == 1 && ref $columns[0] eq "ARRAY" ) { @columns = @{ $columns[0] }; } elsif ( join "", map { defined $_ ? ref $_ : "" } @columns ) { $self->SetDiag( 3001 ); } if ( $self->{_BOUND_COLUMNS} && @columns != @{$self->{_BOUND_COLUMNS}} ) { $self->SetDiag( 3003 ); } $self->{_COLUMN_NAMES} = [ map { defined $_ ? $_ : "\cAUNDEF\cA" } @columns ]; @{ $self->{_COLUMN_NAMES} }; } ################################################################################ # bind_columns ################################################################################ sub bind_columns { my ( $self, @refs ) = @_; @refs or return defined $self->{_BOUND_COLUMNS} ? @{$self->{_BOUND_COLUMNS}} : undef; @refs == 1 && ! defined $refs[0] and return $self->{_BOUND_COLUMNS} = undef; if ( $self->{_COLUMN_NAMES} && @refs != @{$self->{_COLUMN_NAMES}} ) { $self->SetDiag( 3003 ); } if ( grep { ref $_ ne "SCALAR" } @refs ) { # why don't use grep? $self->SetDiag( 3004 ); } $self->{_is_bound} = scalar @refs; #pack("C", scalar @refs); $self->{_BOUND_COLUMNS} = [ @refs ]; @refs; } ################################################################################ # eof ################################################################################ sub eof { $_[0]->{_EOF}; } ################################################################################ # type ################################################################################ sub types { my $self = shift; if (@_) { if (my $types = shift) { $self->{'_types'} = join("", map{ chr($_) } @$types); $self->{'types'} = $types; } else { delete $self->{'types'}; delete $self->{'_types'}; undef; } } else { $self->{'types'}; } } ################################################################################ sub meta_info { $_[0]->{_FFLAGS} ? @{ $_[0]->{_FFLAGS} } : undef; } sub is_quoted { return unless (defined $_[0]->{_FFLAGS}); return if( $_[1] =~ /\D/ or $_[1] < 0 or $_[1] > $#{ $_[0]->{_FFLAGS} } ); $_[0]->{_FFLAGS}->[$_[1]] & IS_QUOTED ? 1 : 0; } sub is_binary { return unless (defined $_[0]->{_FFLAGS}); return if( $_[1] =~ /\D/ or $_[1] < 0 or $_[1] > $#{ $_[0]->{_FFLAGS} } ); $_[0]->{_FFLAGS}->[$_[1]] & IS_BINARY ? 1 : 0; } sub is_missing { my ($self, $idx, $val) = @_; ref $self->{_FFLAGS} && $idx >= 0 && $idx < @{$self->{_FFLAGS}} or return; $self->{_FFLAGS}[$idx] & IS_MISSING ? 1 : 0; } ################################################################################ # _check_type # take an arg as scalar reference. # if not numeric, make the value 0. otherwise INTEGERized. ################################################################################ sub _check_type { my ($col_ref, $type) = @_; unless ($$col_ref =~ /^[+-]?(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) { Carp::carp sprintf("Argument \"%s\" isn't numeric in subroutine entry",$$col_ref); $$col_ref = 0; } elsif ($type == NV) { $$col_ref = sprintf("%G",$$col_ref); } else { $$col_ref = sprintf("%d",$$col_ref); } } ################################################################################ # _set_error_diag ################################################################################ sub _set_error_diag { my ( $self, $error, $pos ) = @_; $self->{_ERROR_DIAG} = $error; if (defined $pos) { $_[0]->{_ERROR_POS} = $pos; } $self->error_diag() if ( $error and $self->{auto_diag} ); return; } ################################################################################ BEGIN { for my $method ( qw/always_quote binary keep_meta_info allow_loose_quotes allow_loose_escapes verbatim blank_is_undef empty_is_undef quote_space quote_null quote_binary allow_unquoted_escape/ ) { eval qq| sub $method { \$_[0]->{$method} = defined \$_[1] ? \$_[1] : 0 if (\@_ > 1); \$_[0]->{$method}; } |; } } sub sep_char { my $self = shift; if ( @_ ) { $self->{sep_char} = $_[0]; my $ec = _check_sanity( $self ); $ec and Carp::croak( $self->SetDiag( $ec ) ); } $self->{sep_char}; } sub decode_utf8 { my $self = shift; if ( @_ ) { $self->{decode_utf8} = $_[0]; my $ec = _check_sanity( $self ); $ec and Carp::croak( $self->SetDiag( $ec ) ); } $self->{decode_utf8}; } sub quote_char { my $self = shift; if ( @_ ) { $self->{quote_char} = $_[0]; my $ec = _check_sanity( $self ); $ec and Carp::croak( $self->SetDiag( $ec ) ); } $self->{quote_char}; } sub escape_char { my $self = shift; if ( @_ ) { $self->{escape_char} = $_[0]; my $ec = _check_sanity( $self ); $ec and Carp::croak( $self->SetDiag( $ec ) ); } $self->{escape_char}; } sub allow_whitespace { my $self = shift; if ( @_ ) { my $aw = shift; $aw and (defined $self->{quote_char} && $self->{quote_char} =~ m/^[ \t]$/) || (defined $self->{escape_char} && $self->{escape_char} =~ m/^[ \t]$/) and Carp::croak ($self->SetDiag (1002)); $self->{allow_whitespace} = $aw; } $self->{allow_whitespace}; } sub eol { $_[0]->{eol} = defined $_[1] ? $_[1] : '' if ( @_ > 1 ); $_[0]->{eol}; } sub SetDiag { if ( defined $_[1] and $_[1] == 0 ) { $_[0]->{_ERROR_DIAG} = undef; $last_new_error = ''; return; } $_[0]->_set_error_diag( $_[1] ); Carp::croak( $_[0]->error_diag . '' ); } sub auto_diag { my $self = shift; if (@_) { my $v = shift; !defined $v || $v eq "" and $v = 0; $v =~ m/^[0-9]/ or $v = $v ? 1 : 0; # default for true/false $self->{auto_diag} = $v; } $self->{auto_diag}; } sub diag_verbose { my $self = shift; if (@_) { my $v = shift; !defined $v || $v eq "" and $v = 0; $v =~ m/^[0-9]/ or $v = $v ? 1 : 0; # default for true/false $self->{diag_verbose} = $v; } $self->{diag_verbose}; } sub _is_valid_utf8 { return ( $_[0] =~ /^(?: [\x00-\x7F] |[\xC2-\xDF][\x80-\xBF] |[\xE0][\xA0-\xBF][\x80-\xBF] |[\xE1-\xEC][\x80-\xBF][\x80-\xBF] |[\xED][\x80-\x9F][\x80-\xBF] |[\xEE-\xEF][\x80-\xBF][\x80-\xBF] |[\xF0][\x90-\xBF][\x80-\xBF][\x80-\xBF] |[\xF1-\xF3][\x80-\xBF][\x80-\xBF][\x80-\xBF] |[\xF4][\x80-\x8F][\x80-\xBF][\x80-\xBF] )+$/x ) ? 1 : 0; } ################################################################################ package Text::CSV::ErrorDiag; use strict; use overload ( '""' => \&stringify, '+' => \&numeric, '-' => \&numeric, '*' => \&numeric, '/' => \&numeric, ); sub numeric { my ($left, $right) = @_; return ref $left ? $left->[0] : $right->[0]; } sub stringify { $_[0]->[1]; } ################################################################################ 1; __END__ =head1 NAME Text::CSV_PP - Text::CSV_XS compatible pure-Perl module =head1 SYNOPSIS use Text::CSV_PP; $csv = Text::CSV_PP->new(); # create a new object # If you want to handle non-ascii char. $csv = Text::CSV_PP->new({binary => 1}); $status = $csv->combine(@columns); # combine columns into a string $line = $csv->string(); # get the combined string $status = $csv->parse($line); # parse a CSV string into fields @columns = $csv->fields(); # get the parsed fields $status = $csv->status (); # get the most recent status $bad_argument = $csv->error_input (); # get the most recent bad argument $diag = $csv->error_diag (); # if an error occured, explains WHY $status = $csv->print ($io, $colref); # Write an array of fields # immediately to a file $io $colref = $csv->getline ($io); # Read a line from file $io, # parse it and return an array # ref of fields $csv->column_names (@names); # Set column names for getline_hr () $ref = $csv->getline_hr ($io); # getline (), but returns a hashref $eof = $csv->eof (); # Indicate if last parse or # getline () hit End Of File $csv->types(\@t_array); # Set column types =head1 DESCRIPTION Text::CSV_PP has almost same functions of L which provides facilities for the composition and decomposition of comma-separated values. As its name suggests, L is a XS module and Text::CSV_PP is a Puer Perl one. =head1 VERSION 1.31 This module is compatible with Text::CSV_XS B<0.99>. (except for diag_verbose and allow_unquoted_escape) =head2 Unicode (UTF8) On parsing (both for C and C), if the source is marked being UTF8, then parsing that source will mark all fields that are marked binary will also be marked UTF8. On combining (C and C), if any of the combining fields was marked UTF8, the resulting string will be marked UTF8. =head1 FUNCTIONS These methods are almost same as Text::CSV_XS. Most of the documentation was shamelessly copied and replaced from Text::CSV_XS. See to L. =head2 version () (Class method) Returns the current backend module version. If you want the module version, you can use the C method, print Text::CSV->VERSION; # This module version print Text::CSV->version; # The version of the worker module # same as Text::CSV->backend->version =head2 new (\%attr) (Class method) Returns a new instance of Text::CSV_XS. The objects attributes are described by the (optional) hash ref C<\%attr>. Currently the following attributes are available: =over 4 =item eol An end-of-line string to add to rows. C is replaced with an empty string. The default is C<$\>. Common values for C are C<"\012"> (Line Feed) or C<"\015\012"> (Carriage Return, Line Feed). Cannot be longer than 7 (ASCII) characters. If both C<$/> and C equal C<"\015">, parsing lines that end on only a Carriage Return without Line Feed, will be Cd correct. Line endings, whether in C<$/> or C, other than C, C<"\n">, C<"\r\n">, or C<"\r"> are not (yet) supported for parsing. =item sep_char The char used for separating fields, by default a comma. (C<,>). Limited to a single-byte character, usually in the range from 0x20 (space) to 0x7e (tilde). The separation character can not be equal to the quote character. The separation character can not be equal to the escape character. See also L =item allow_whitespace When this option is set to true, whitespace (TAB's and SPACE's) surrounding the separation character is removed when parsing. If either TAB or SPACE is one of the three major characters C, C, or C it will not be considered whitespace. So lines like: 1 , "foo" , bar , 3 , zapp are now correctly parsed, even though it violates the CSV specs. Note that B whitespace is stripped from start and end of each field. That would make it more a I than a way to be able to parse bad CSV lines, as 1, 2.0, 3, ape , monkey will now be parsed as ("1", "2.0", "3", "ape", "monkey") even if the original line was perfectly sane CSV. =item blank_is_undef Under normal circumstances, CSV data makes no distinction between quoted- and unquoted empty fields. They both end up in an empty string field once read, so 1,"",," ",2 is read as ("1", "", "", " ", "2") When I CSV files with C set, the unquoted empty field is the result of an undefined value. To make it possible to also make this distinction when reading CSV data, the C option will cause unquoted empty fields to be set to undef, causing the above to be parsed as ("1", "", undef, " ", "2") =item empty_is_undef Going one step further than C, this attribute converts all empty fields to undef, so 1,"",," ",2 is read as (1, undef, undef, " ", 2) Note that this only effects fields that are I empty, not fields that are empty after stripping allowed whitespace. YMMV. =item quote_char The char used for quoting fields containing blanks, by default the double quote character (C<">). A value of undef suppresses quote chars. (For simple cases only). Limited to a single-byte character, usually in the range from 0x20 (space) to 0x7e (tilde). The quote character can not be equal to the separation character. =item allow_loose_quotes By default, parsing fields that have C characters inside an unquoted field, like 1,foo "bar" baz,42 would result in a parse error. Though it is still bad practice to allow this format, we cannot help there are some vendors that make their applications spit out lines styled like this. In case there is B bad CSV data, like 1,"foo "bar" baz",42 or 1,""foo bar baz"",42 there is a way to get that parsed, and leave the quotes inside the quoted field as-is. This can be achieved by setting C B making sure that the C is I equal to C. =item escape_char The character used for escaping certain characters inside quoted fields. Limited to a single-byte character, usually in the range from 0x20 (space) to 0x7e (tilde). The C defaults to being the literal double-quote mark (C<">) in other words, the same as the default C. This means that doubling the quote mark in a field escapes it: "foo","bar","Escape ""quote mark"" with two ""quote marks""","baz" If you change the default quote_char without changing the default escape_char, the escape_char will still be the quote mark. If instead you want to escape the quote_char by doubling it, you will need to change the escape_char to be the same as what you changed the quote_char to. The escape character can not be equal to the separation character. =item allow_loose_escapes By default, parsing fields that have C characters that escape characters that do not need to be escaped, like: my $csv = Text::CSV->new ({ escape_char => "\\" }); $csv->parse (qq{1,"my bar\'s",baz,42}); would result in a parse error. Though it is still bad practice to allow this format, this option enables you to treat all escape character sequences equal. =item binary If this attribute is TRUE, you may use binary characters in quoted fields, including line feeds, carriage returns and NULL bytes. (The latter must be escaped as C<"0>.) By default this feature is off. If a string is marked UTF8, binary will be turned on automatically when binary characters other than CR or NL are encountered. Note that a simple string like C<"\x{00a0}"> might still be binary, but not marked UTF8, so setting C<{ binary =E 1 }> is still a wise option. =item types A set of column types; this attribute is immediately passed to the I method below. You must not set this attribute otherwise, except for using the I method. For details see the description of the I method below. =item always_quote By default the generated fields are quoted only, if they need to, for example, if they contain the separator. If you set this attribute to a TRUE value, then all defined fields will be quoted. This is typically easier to handle in external applications. =item quote_space By default, a space in a field would trigger quotation. As no rule exists this to be forced in CSV, nor any for the opposite, the default is true for safety. You can exclude the space from this trigger by setting this option to 0. =item quote_null By default, a NULL byte in a field would be escaped. This attribute enables you to treat the NULL byte as a simple binary character in binary mode (the C<{ binary =E 1 }> is set). The default is true. You can prevent NULL escapes by setting this attribute to 0. =item keep_meta_info By default, the parsing of input lines is as simple and fast as possible. However, some parsing information - like quotation of the original field - is lost in that process. Set this flag to true to be able to retrieve that information after parsing with the methods C, C, and C described below. Default is false. =item verbatim This is a quite controversial attribute to set, but it makes hard things possible. The basic thought behind this is to tell the parser that the normally special characters newline (NL) and Carriage Return (CR) will not be special when this flag is set, and be dealt with as being ordinary binary characters. This will ease working with data with embedded newlines. When C is used with C, C auto-chomp's every line. Imagine a file format like M^^Hans^Janssen^Klas 2\n2A^Ja^11-06-2007#\r\n where, the line ending is a very specific "#\r\n", and the sep_char is a ^ (caret). None of the fields is quoted, but embedded binary data is likely to be present. With the specific line ending, that shouldn't be too hard to detect. By default, Text::CSV' parse function however is instructed to only know about "\n" and "\r" to be legal line endings, and so has to deal with the embedded newline as a real end-of-line, so it can scan the next line if binary is true, and the newline is inside a quoted field. With this attribute however, we can tell parse () to parse the line as if \n is just nothing more than a binary character. For parse () this means that the parser has no idea about line ending anymore, and getline () chomps line endings on reading. =item auto_diag Set to true will cause C to be automatically be called in void context upon errors. If set to a value greater than 1, it will die on errors instead of warn. To check future plans and a difference in XS version, please see to L. =back To sum it up, $csv = Text::CSV_PP->new (); is equivalent to $csv = Text::CSV_PP->new ({ quote_char => '"', escape_char => '"', sep_char => ',', eol => $\, always_quote => 0, quote_space => 1, quote_null => 1, binary => 0, keep_meta_info => 0, allow_loose_quotes => 0, allow_loose_escapes => 0, allow_whitespace => 0, blank_is_undef => 0, empty_is_undef => 0, verbatim => 0, auto_diag => 0, }); For all of the above mentioned flags, there is an accessor method available where you can inquire for the current value, or change the value my $quote = $csv->quote_char; $csv->binary (1); It is unwise to change these settings halfway through writing CSV data to a stream. If however, you want to create a new stream using the available CSV object, there is no harm in changing them. If the C constructor call fails, it returns C, and makes the fail reason available through the C method. $csv = Text::CSV->new ({ ecs_char => 1 }) or die "" . Text::CSV->error_diag (); C will return a string like "INI - Unknown attribute 'ecs_char'" =head2 print $status = $csv->print ($io, $colref); Similar to C, but more efficient. It expects an array ref as input (not an array!) and the resulting string is not really created (XS version), but immediately written to the I<$io> object, typically an IO handle or any other object that offers a I method. Note, this implies that the following is wrong in perl 5.005_xx and older: open FILE, ">", "whatever"; $status = $csv->print (\*FILE, $colref); as in perl 5.005 and older, the glob C<\*FILE> is not an object, thus it doesn't have a print method. The solution is to use an IO::File object or to hide the glob behind an IO::Wrap object. See L and L for details. For performance reasons the print method doesn't create a result string. (If its backend is PP version, result strings are created internally.) In particular the I<$csv-Estring ()>, I<$csv-Estatus ()>, I<$csv->fields ()> and I<$csv-Eerror_input ()> methods are meaningless after executing this method. =head2 combine $status = $csv->combine (@columns); This object function constructs a CSV string from the arguments, returning success or failure. Failure can result from lack of arguments or an argument containing an invalid character. Upon success, C can be called to retrieve the resultant CSV string. Upon failure, the value returned by C is undefined and C can be called to retrieve an invalid argument. =head2 string $line = $csv->string (); This object function returns the input to C or the resultant CSV string of C, whichever was called more recently. =head2 getline $colref = $csv->getline ($io); This is the counterpart to print, like parse is the counterpart to combine: It reads a row from the IO object $io using $io->getline () and parses this row into an array ref. This array ref is returned by the function or undef for failure. When fields are bound with C, the return value is a reference to an empty list. The I<$csv-Estring ()>, I<$csv-Efields ()> and I<$csv-Estatus ()> methods are meaningless, again. =head2 getline_all $arrayref = $csv->getline_all ($io); $arrayref = $csv->getline_all ($io, $offset); $arrayref = $csv->getline_all ($io, $offset, $length); This will return a reference to a list of C results. In this call, C is disabled. If C<$offset> is negative, as with C, only the last C records of C<$io> are taken into consideration. Given a CSV file with 10 lines: lines call ----- --------------------------------------------------------- 0..9 $csv->getline_all ($io) # all 0..9 $csv->getline_all ($io, 0) # all 8..9 $csv->getline_all ($io, 8) # start at 8 - $csv->getline_all ($io, 0, 0) # start at 0 first 0 rows 0..4 $csv->getline_all ($io, 0, 5) # start at 0 first 5 rows 4..5 $csv->getline_all ($io, 4, 2) # start at 4 first 2 rows 8..9 $csv->getline_all ($io, -2) # last 2 rows 6..7 $csv->getline_all ($io, -4, 2) # first 2 of last 4 rows =head2 parse $status = $csv->parse ($line); This object function decomposes a CSV string into fields, returning success or failure. Failure can result from a lack of argument or the given CSV string is improperly formatted. Upon success, C can be called to retrieve the decomposed fields . Upon failure, the value returned by C is undefined and C can be called to retrieve the invalid argument. You may use the I method for setting column types. See the description below. =head2 getline_hr The C and C methods work together to allow you to have rows returned as hashrefs. You must call C first to declare your column names. $csv->column_names (qw( code name price description )); $hr = $csv->getline_hr ($io); print "Price for $hr->{name} is $hr->{price} EUR\n"; C will croak if called before C. =head2 getline_hr_all $arrayref = $csv->getline_hr_all ($io); This will return a reference to a list of C results. In this call, C is disabled. =head2 column_names Set the keys that will be used in the C calls. If no keys (column names) are passed, it'll return the current setting. C accepts a list of scalars (the column names) or a single array_ref, so you can pass C $csv->column_names ($csv->getline ($io)); C does B checking on duplicates at all, which might lead to unwanted results. Undefined entries will be replaced with the string C<"\cAUNDEF\cA">, so $csv->column_names (undef, "", "name", "name"); $hr = $csv->getline_hr ($io); Will set C<$hr->{"\cAUNDEF\cA"}> to the 1st field, C<$hr->{""}> to the 2nd field, and C<$hr->{name}> to the 4th field, discarding the 3rd field. C croaks on invalid arguments. =head2 print_hr $csv->print_hr ($io, $ref); Provides an easy way to print a C<$ref> as fetched with L provided the column names are set with L. It is just a wrapper method with basic parameter checks over $csv->print ($io, [ map { $ref->{$_} } $csv->column_names ]); =head2 bind_columns Takes a list of references to scalars to store the fields fetched C in. When you don't pass enough references to store the fetched fields in, C will fail. If you pass more than there are fields to return, the remaining references are left untouched. $csv->bind_columns (\$code, \$name, \$price, \$description); while ($csv->getline ($io)) { print "The price of a $name is \x{20ac} $price\n"; } =head2 eof $eof = $csv->eof (); If C or C was used with an IO stream, this method will return true (1) if the last call hit end of file, otherwise it will return false (''). This is useful to see the difference between a failure and end of file. =head2 types $csv->types (\@tref); This method is used to force that columns are of a given type. For example, if you have an integer column, two double columns and a string column, then you might do a $csv->types ([Text::CSV_PP::IV (), Text::CSV_PP::NV (), Text::CSV_PP::NV (), Text::CSV_PP::PV ()]); Column types are used only for decoding columns, in other words by the I and I methods. You can unset column types by doing a $csv->types (undef); or fetch the current type settings with $types = $csv->types (); =over 4 =item IV Set field type to integer. =item NV Set field type to numeric/float. =item PV Set field type to string. =back =head2 fields @columns = $csv->fields (); This object function returns the input to C or the resultant decomposed fields of C successful , whichever was called more recently. Note that the return value is undefined after using C, which does not fill the data structures returned by C. =head2 meta_info @flags = $csv->meta_info (); This object function returns the flags of the input to C or the flags of the resultant decomposed fields of C, whichever was called more recently. For each field, a meta_info field will hold flags that tell something about the field returned by the C method or passed to the C method. The flags are bitwise-or'd like: =over 4 =item 0x0001 The field was quoted. =item 0x0002 The field was binary. =back See the C methods below. =head2 is_quoted my $quoted = $csv->is_quoted ($column_idx); Where C<$column_idx> is the (zero-based) index of the column in the last result of C. This returns a true value if the data in the indicated column was enclosed in C quotes. This might be important for data where C<,20070108,> is to be treated as a numeric value, and where C<,"20070108",> is explicitly marked as character string data. =head2 is_binary my $binary = $csv->is_binary ($column_idx); Where C<$column_idx> is the (zero-based) index of the column in the last result of C. This returns a true value if the data in the indicated column contained any byte in the range [\x00-\x08,\x10-\x1F,\x7F-\xFF] =head2 status $status = $csv->status (); This object function returns success (or failure) of C or C, whichever was called more recently. =head2 error_input $bad_argument = $csv->error_input (); This object function returns the erroneous argument (if it exists) of C or C, whichever was called more recently. =head2 error_diag Text::CSV_PP->error_diag (); $csv->error_diag (); $error_code = 0 + $csv->error_diag (); $error_str = "" . $csv->error_diag (); ($cde, $str, $pos) = $csv->error_diag (); If (and only if) an error occurred, this function returns the diagnostics of that error. If called in void context, it will print the internal error code and the associated error message to STDERR. If called in list context, it will return the error code and the error message in that order. If the last error was from parsing, the third value returned is the best guess at the location within the line that was being parsed. It's value is 1-based. Note: C<$pos> does not show the error point in many cases. It is for conscience's sake. If called in scalar context, it will return the diagnostics in a single scalar, a-la $!. It will contain the error code in numeric context, and the diagnostics message in string context. To achieve this behavior with CSV_PP, the returned diagnostics is blessed object. =head2 SetDiag $csv->SetDiag (0); Use to reset the diagnostics if you are dealing with errors. =head1 DIAGNOSTICS If an error occurred, $csv->error_diag () can be used to get more information on the cause of the failure. Note that for speed reasons, the internal value is never cleared on success, so using the value returned by error_diag () in normal cases - when no error occurred - may cause unexpected results. Note: CSV_PP's diagnostics is different from CSV_XS's: Text::CSV_XS parses csv strings by dividing one character while Text::CSV_PP by using the regular expressions. That difference makes the different cause of the failure. Currently these errors are available: =over 2 =item 1001 "sep_char is equal to quote_char or escape_char" The separation character cannot be equal to either the quotation character or the escape character, as that will invalidate all parsing rules. =item 1002 "INI - allow_whitespace with escape_char or quote_char SP or TAB" Using C when either C or C is equal to SPACE or TAB is too ambiguous to allow. =item 1003 "INI - \r or \n in main attr not allowed" Using default C characters in either C, C, or C is not allowed. =item 2010 "ECR - QUO char inside quotes followed by CR not part of EOL" =item 2011 "ECR - Characters after end of quoted field" =item 2021 "EIQ - NL char inside quotes, binary off" =item 2022 "EIQ - CR char inside quotes, binary off" =item 2025 "EIQ - Loose unescaped escape" =item 2026 "EIQ - Binary character inside quoted field, binary off" =item 2027 "EIQ - Quoted field not terminated" =item 2030 "EIF - NL char inside unquoted verbatim, binary off" =item 2031 "EIF - CR char is first char of field, not part of EOL", =item 2032 "EIF - CR char inside unquoted, not part of EOL", =item 2034 "EIF - Loose unescaped quote", =item 2037 "EIF - Binary character in unquoted field, binary off", =item 2110 "ECB - Binary character in Combine, binary off" =item 2200 "EIO - print to IO failed. See errno" =item 4002 "EIQ - Unescaped ESC in quoted field" =item 4003 "EIF - ESC CR" =item 4004 "EUF - " =item 3001 "EHR - Unsupported syntax for column_names ()" =item 3002 "EHR - getline_hr () called before column_names ()" =item 3003 "EHR - bind_columns () and column_names () fields count mismatch" =item 3004 "EHR - bind_columns () only accepts refs to scalars" =item 3006 "EHR - bind_columns () did not pass enough refs for parsed fields" =item 3007 "EHR - bind_columns needs refs to writable scalars" =item 3008 "EHR - unexpected error in bound fields" =back =head1 AUTHOR Makamaka Hannyaharamitu, Emakamaka[at]cpan.orgE Text::CSV_XS was written by Ejoe[at]ispsoft.deE and maintained by Eh.m.brand[at]xs4all.nlE. Text::CSV was written by Ealan[at]mfgrtl.comE. =head1 COPYRIGHT AND LICENSE Copyright 2005-2015 by Makamaka Hannyaharamitu, Emakamaka[at]cpan.orgE This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO L, L I got many regexp bases from L =cut Text-CSV-1.33/Makefile.PL0000644000175000017500000000410112475365663013474 0ustar reonreonuse 5.00503; use ExtUtils::MakeMaker; use lib qw( ./lib ); eval q| require Text::CSV |; # B module can't install? I'm not careful for such a problem. # Leave us alone today? if ($@) { print "We try to look up lib/Text::CSV.pm, but in vain. B module can't install?\n"; print "Set the environmental variable 'PERL_DL_NONLAZY' with 0.\n"; print "And see to ExtUtils::MM_Unix.\n"; print "perl says : $@"; print "We do not make Makefile by requiring Perl version 7.0.\n"; require 7.0000; } my $version = Text::CSV->VERSION; my $req_xs_ver = Text::CSV->require_xs_version; my $has_xs = 0; my $message; eval q| require Text::CSV_XS |; $has_xs = 1 unless ($@); if ($has_xs) { my $xs_version = Text::CSV_XS->VERSION; if ($xs_version >= $req_xs_ver) { $message = "You have Text::CSV_XS (v.$xs_version), so Text::CSV can work very fast!!"; } else { $message = "Your Text::CSV_XS version is $xs_version. If you install v.$req_xs_ver,\n" . "Text::CSV will work faster."; } } else { $message = "If you install Text::CSV_XS v.$req_xs_ver, it makes Text::CSV faster."; } print < 'Text::CSV', 'VERSION_FROM' => 'lib/Text/CSV.pm', # finds $VERSION 'PREREQ_PM' => { "IO::Handle" => 0, "Test::More" => 0, "Test::Harness" => 0, }, ($] >= 5.005 ? ## Add these new keywords supported since 5.005 (ABSTRACT_FROM => 'lib/Text/CSV.pm', # retrieve abstract from module AUTHOR => 'Makamaka Hannyaharamitu, Emakamaka[at]cpan.orgE') : ()), ( $ExtUtils::MakeMaker::VERSION >= 6.3002 ? ('LICENSE' => 'perl', ) : () ), ( $ExtUtils::MakeMaker::VERSION >= 6.46 ? ( 'META_MERGE' => { resources => { repository => 'http://github.com/makamaka/Text-CSV', }, } ) : () ), ); Text-CSV-1.33/MANIFEST0000644000175000017500000000275112475371020012644 0ustar reonreonChanges files/macosx.csv A CSV files exported on MacOSX files/utf8.csv A UTF-8 encode test file lib/Text/CSV.pm lib/Text/CSV_PP.pm Makefile.PL MANIFEST README t/00_pod.t Check if pod is valid t/10_base.t Base tests (combine and parse only) t/12_acc.t Accessor methods t/15_flags.t Basic flag tests t/20_file.t IO tests (print and getline) global handles t/21_lexicalio.t IO tests (print and getline) lexical handles t/22_scalario.t IO tests (print and getline) scalar io (perlio) t/30_types.t Tests for the "types" attribute. t/40_misc.t Binary mode tests t/41_null.t NULL test in combination with line endings t/45_eol.t Embedded EOL t/46_eol_si.t Embedded EOL using scalar io (perlio) t/50_utf8.t Unicode stress tests t/51_utf8.t Unicode IO encoding tests t/55_combi.t Different CSV character combinations t/60_samples.t Miscellaneous problems from the modules history. t/65_allow.t Allow bad formats t/70_rt.t Tests based on RT reports (for Text::CSV_XS) t/71_pp.t Tests for bug report fixes or patches (for Text::CSV_PP) t/75_hashref.t getline_hr related tests t/76_magic.t array_ref from magig (useless for Text::CSV_PP) t/77_getall.t gat all rows at once t/80_diag.t Error diagnostics t/81_subclass.t Subclassed t/fields_containing_0.t Tests t/rt99774.t t/util.pl Extra test utilities META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) Text-CSV-1.33/README0000644000175000017500000000176212475365663012414 0ustar reonreonText::CSV version 1.33 ======================== comma-separated values manipulator INSTALLATION To install this module type the following: perl Makefile.PL make make test make install DEPENDENCIES Test::More DESCRIPTION Text::CSV provides facilities for the composition and decomposition of comma-separated values. An instance of the Text::CSV class can combine fields into a CSV string and parse a CSV string into fields. The module accepts either strings or files as input and can utilize any user-specified characters as delimiters, separators, and escapes so it is perhaps better called ASV (anything separated values) rather than just CSV. COPYRIGHT AND LICENSE Copyright (C) 1997 Alan Citterman. All rights reserved. Copyright (C) 2007-2015 Makamaka Hannyaharamitu. All rights reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Text-CSV-1.33/t/0000755000175000017500000000000012475371020011751 5ustar reonreonText-CSV-1.33/t/55_combi.t0000644000175000017500000000445012475365663013563 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; use Test::More tests => 9074; BEGIN { $ENV{PERL_TEXT_CSV} = 0; require_ok "Text::CSV"; plan skip_all => "Cannot load Text::CSV" if $@; require "t/util.pl"; } my $csv = Text::CSV->new ({ binary => 1 }); my @attrib = qw( quote_char escape_char sep_char ); my @special = ('"', "'", ",", ";", "\t", "\\", "~"); # Add undef, once we can return undef my @input = ( "", 1, "1", 1.4, "1.4", " - 1,4", "1+2=3", "' ain't it great '", '"foo"! said the `bär', q{the ~ in "0 \0 this l'ne is \r ; or "'"} ); my $ninput = scalar @input; my $string = join "=", "", @input, ""; my %fail; ok (1, "-- qc ec sc ac"); sub combi { my %attr = @_; my $combi = join " ", "--", map { sprintf "%6s", _readable $attr{$_} } @attrib, "always_quote"; ok (1, $combi); foreach my $attr (sort keys %attr) { $csv->$attr ($attr{$attr}); is ($csv->$attr (), $attr{$attr}, "check $attr"); } my $ret = $csv->combine (@input); if ($attr{sep_char} eq $attr{quote_char} || $attr{sep_char} eq $attr{escape_char}) { is ($ret, undef, "Illegal combo for combine"); ok (!$csv->parse ("foo"), "illegal combo for parse"); return; } ok ($ret, "combine"); ok (my $str = $csv->string, "string"); SKIP: { ok (my $ok = $csv->parse ($str), "parse"); unless ($ok) { $fail{parse}{$combi} = $csv->error_input; skip "parse () failed", 3; } ok (my @ret = $csv->fields, "fields"); unless (@ret) { $fail{fields}{$combi} = $csv->error_input; skip "fields () failed", 2; } is (scalar @ret, $ninput, "$ninput fields"); unless (scalar @ret == $ninput) { $fail{'$#fields'}{$combi} = $str; skip "# fields failed", 1; } my $ret = join "=", "", @ret, ""; is ($ret, $string, "content"); } } # combi foreach my $aq (0, 1) { foreach my $qc (@special) { foreach my $ec (@special, "+") { foreach my $sc (@special, "\0") { combi ( quote_char => $qc, escape_char => $ec, sep_char => $sc, always_quote => $aq, ); } } } } foreach my $fail (sort keys %fail) { print STDERR "Failed combi for $fail ():\n", "-- qc ec sc ac\n"; foreach my $combi (sort keys %{$fail{$fail}}) { printf STDERR "%-20s - %s\n", map { _readable $_ } $combi, $fail{$fail}{$combi}; } } 1; Text-CSV-1.33/t/81_subclass.t0000644000175000017500000000115112475365663014303 0ustar reonreon#!/usr/bin/perl package Text::CSV::Subclass; BEGIN { $ENV{PERL_TEXT_CSV} = 0; } BEGIN { require Text::CSV; } # needed for perl5.005 use strict; $^W = 1; use base "Text::CSV"; use Test::More tests => 5; ok (1, "Subclassed"); my $csvs = Text::CSV::Subclass->new (); is ("" . $csvs->error_diag (), "", "Last failure for new () - OK"); my $sc_csv; eval { $sc_csv = Text::CSV::Subclass->new ({ ecs_char => ":" }); }; is ($sc_csv, undef, "Unsupported option"); is ($@, "", "error"); is ("" . Text::CSV::Subclass->error_diag (), "INI - Unknown attribute 'ecs_char'", "Last failure for new () - FAIL"); 1; Text-CSV-1.33/t/71_pp.t0000644000175000017500000002060512475365663013107 0ustar reonreon#!/usr/bin/perl # tests for bug report fixes or patches. use strict; $^W = 1; use Test::More tests => 103; BEGIN { $ENV{PERL_TEXT_CSV} = $ARGV[0] || 0; } BEGIN { require_ok "Text::CSV"; plan skip_all => "Cannot load Text::CSV" if $@; } #warn Text::CSV->backend; my $csv = Text::CSV->new( { sep_char => "\t", blank_is_undef => 1, allow_whitespace => 1 } ); ok $csv->parse(qq|John\t\t"my notes"|); is_deeply ([ $csv->fields ], [ "John", undef, "my notes" ], "Tab with allow_white_space"); # 2009-04-23 rt#45215 my $str = "this,is,some,csv,data\n"; $csv = Text::CSV->new; $csv->parse($str); is( $csv->string, $str ); #=pod # 2009-05-16 # getline() handles having escaped null my $opts = { 'escape_char' => '"', 'quote_char' => '"', 'binary' => 1, 'sep_char' => ',' }; my $eol = "\r\n"; my $blob = ( join "", map { chr $_ } 0 .. 255 ) x 1; #my $blob = ( join "", map { chr $_ } 0 .. 2 ) x 1; $csv = Text::CSV->new( $opts ); open( FH, '>__test.csv' ) or die $!; binmode FH; # writting ok( $csv->print( *FH, [ $blob ] ) ); close( FH ); # reading open( FH, "__test.csv" ) or die $!; binmode FH; $opts->{eol} = $eol; $csv = Text::CSV->new( $opts ); ok( my $colref = $csv->getline( *FH ) ); is( $colref->[0], $blob, "blob" ); close( FH ); #exit; unlink( '__test.csv' ); #=cut # 2009-07-30 # getline() handles a 0 staring multiline # writting open( FH, '>__test.csv' ) or die $!; binmode FH; ok( $csv->print( *FH, [ "00" ] ) ); ok( $csv->print( *FH, [ "\00" ] ) ); ok( $csv->print( *FH, [ "0\0" ] ) ); ok( $csv->print( *FH, [ "\0\0" ] ) ); ok( $csv->print( *FH, [ "0\n0" ] ) ); ok( $csv->print( *FH, [ "\0\n0" ] ) ); ok( $csv->print( *FH, [ "0\n\0" ] ) ); ok( $csv->print( *FH, [ "\0\n\0" ] ) ); ok( $csv->print( *FH, [ "\"0\n0" ] ) ); ok( $csv->print( *FH, [ "\"\0\n0" ] ) ); ok( $csv->print( *FH, [ "\"0\n\0" ] ) ); ok( $csv->print( *FH, [ "\"\0\n\0" ] ) ); ok( $csv->print( *FH, [ "\"0\n\"0" ] ) ); ok( $csv->print( *FH, [ "\"\0\n\"0" ] ) ); ok( $csv->print( *FH, [ "\"0\n\"\0" ] ) ); ok( $csv->print( *FH, [ "\"\0\n\"\0" ] ) ); ok( $csv->print( *FH, [ "0\n0", "0\n0" ] ) ); ok( $csv->print( *FH, [ "\0\n0", "\0\n0" ] ) ); ok( $csv->print( *FH, [ "0\n\0", "0\n\0" ] ) ); ok( $csv->print( *FH, [ "\0\n\0", "\0\n\0" ] ) ); $csv->always_quote(1); ok( $csv->print( *FH, [ "", undef, "0\n", "", "\0\n0" ] ) ); close( FH ); # reading open( FH, "__test.csv" ) or die $!; binmode FH; is( $csv->getline( *FH )->[0], "00", '*00' ); # Test::More warns 00 is( $csv->getline( *FH )->[0], "\00", '\00' ); is( $csv->getline( *FH )->[0], "0\0", '0\0' ); is( $csv->getline( *FH )->[0], "\0\0", '\0\0' ); is( $csv->getline( *FH )->[0], "0\n0", '*0\n0' ); # Test::More warns 00 is( $csv->getline( *FH )->[0], "\0\n0", '\0\n0' ); is( $csv->getline( *FH )->[0], "0\n\0", '0\n\0' ); is( $csv->getline( *FH )->[0], "\0\n\0", '\0\n\0' ); is( $csv->getline( *FH )->[0], "\"0\n0", '\"0\n0' ); is( $csv->getline( *FH )->[0], "\"\0\n0", '\"\0\n0' ); is( $csv->getline( *FH )->[0], "\"0\n\0", '\"0\n\0' ); is( $csv->getline( *FH )->[0], "\"\0\n\0", '\"\0\n\0' ); is( $csv->getline( *FH )->[0], "\"0\n\"0", '\"0\n\"0' ); is( $csv->getline( *FH )->[0], "\"\0\n\"0", '\"\0\n\"0' ); is( $csv->getline( *FH )->[0], "\"0\n\"\0", '\"0\n\"\0' ); is( $csv->getline( *FH )->[0], "\"\0\n\"\0", '\"\0\n\"\0' ); is( $csv->getline( *FH )->[1], "0\n0", '*0\n0' ); # Test::More warns 00 is( $csv->getline( *FH )->[1], "\0\n0", '\0\n0' ); is( $csv->getline( *FH )->[1], "0\n\0", '0\n\0' ); is( $csv->getline( *FH )->[1], "\0\n\0", '\0\n\0' ); $csv->blank_is_undef(1); my $col = $csv->getline( *FH ); is( $col->[0], "", '' ); is( $col->[1], undef, '' ); is( $col->[2], "0\n", '' ); is( $col->[3], "", '' ); is( $col->[4], "\0\n0", '' ); close( FH ); unlink( '__test.csv' ); # 2010-06-18 reported by https://rt.cpan.org/Public/Bug/Display.html?id=58356 $csv = Text::CSV->new ({ binary => 1, quote_space => 0 }); my @list = ( "a a", "b,b", "c ,c", ); ok( $csv->combine( @list ) ); is( $csv->string, q{a a,"b,b","c ,c"} ); # 2010-06-22 reported { $csv = Text::CSV->new ({ binary => 1, sep_char => ';', always_quote => 1 }); open( FH, '>__test.csv' ) or die $!; binmode FH; ok( $csv->print( *FH, [ 0, qq{t"t"\n} ] ) ); close( FH ); open( FH, "__test.csv" ) or die $!; binmode FH; my $col = $csv->getline( *FH ); is( $col->[0], "0" ); is( $col->[1], qq{t"t"\n} ); close( FH ); unlink( '__test.csv' ); } # 2010-10-13 reported by hiratara { $csv = Text::CSV->new ({ binary => 1, eol => $/, always_quote => 1 }); open( FH, '>__test.csv' ) or die $!; binmode FH; ok( $csv->print( *FH, [qw/A 01/] ) ); ok( $csv->print( *FH, [qw/B 02/] ) ); close( FH ); open( FH, "__test.csv" ) or die $!; binmode FH; my $col = $csv->getline( *FH ); is( $col->[0], 'A' ); is( $col->[1], '01' ); $col = $csv->getline( *FH ); is( $col->[0], 'B' ); is( $col->[1], '02' ); close( FH ); unlink( '__test.csv' ); } # 2010-10-13 reported(2) by hiratara { $csv = Text::CSV->new ({ binary => 1, eol => $/ }); open( FH, '>__test.csv' ) or die $!; binmode FH; ok( $csv->print( *FH, [qw/1 0"/] ) ); ok( $csv->print( *FH, [qw/2 0"/] ) ); close( FH ); open( FH, "__test.csv" ) or die $!; binmode FH; my $col = $csv->getline( *FH ); is( $col->[0], '1' ); is( $col->[1], '0"' ); $col = $csv->getline( *FH ); is( $col->[0], '2' ); is( $col->[1], '0"' ); close( FH ); unlink( '__test.csv' ); } { # previous three test merged $csv = Text::CSV->new ({ binary => 1, eol => $/ }); open( FH, '>__test.csv' ) or die $!; binmode FH; ok( $csv->print( *FH, [ 0, qq{t"t"\n} ] ) ); ok( $csv->print( *FH, [qw/A 01/] ) ); ok( $csv->print( *FH, [qw/1 0"/] ) ); ok( $csv->print( *FH, [undef,undef] ) ); ok( $csv->print( *FH, [qw/1 0"/] ) ); ok( $csv->print( *FH, [qw/A 01/] ) ); close( FH ); open( FH, "__test.csv" ) or die $!; binmode FH; my $col = $csv->getline( *FH ); is( $col->[0], "0" ); is( $col->[1], qq{t"t"\n} ); $col = $csv->getline( *FH ); is( $col->[0], 'A' ); is( $col->[1], '01' ); $col = $csv->getline( *FH ); is( $col->[0], '1' ); is( $col->[1], '0"' ); $col = $csv->getline( *FH ); is( $col->[0], '' ); is( $col->[1], '' ); $col = $csv->getline( *FH ); is( $col->[0], '1' ); is( $col->[1], '0"' ); $col = $csv->getline( *FH ); is( $col->[0], 'A' ); is( $col->[1], '01' ); close( FH ); unlink( '__test.csv' ); } { # https://rt.cpan.org/Ticket/Display.html?id=83705 my $csv = Text::CSV->new( { binary => 1, allow_loose_escapes => 1, allow_loose_quotes => 1, sep_char => q{;}, escape_char => q{"}, quote_char => q{"} } ); $csv->parse(q{"6RE";"EINKAUF";"5";"";"2,5" HD"}); is_deeply([$csv->fields], ["6RE","EINKAUF","5","",'2,5" HD']); my $csv_dump = q{"6RE";"EINKAUF";"5";"";"2,5" HD" "LIDL";"-2"}; open( FH, '>__test.csv' ) or die $!; print FH $csv_dump; close FH; open FH, '<__test.csv'; is_deeply( $csv->getline(*FH), ["6RE","EINKAUF","5","",'2,5" HD'] ); is_deeply( $csv->getline(*FH), ['LIDL','-2'] ); close FH; unlink( '__test.csv' ); } { # imported from t/70_rt.t my $csv = Text::CSV->new ({ escape_char => "\\", auto_diag => 1 }); ok( $csv->parse(q{1,"\,",3}) ); is_deeply ([ $csv->fields ], [ 1, ",", 3 ], "escaped sep in quoted field"); ok( $csv->parse(q{1,"2\,4",3}) ); is_deeply ([ $csv->fields ], [ 1, "2,4", 3 ], "escaped sep in quoted field"); $csv->allow_unquoted_escape(1); ok( $csv->parse(q{1,\,,3}) ); is_deeply ([ $csv->fields ], [ 1, ",", 3 ], "escaped sep in quoted field"); ok( $csv->parse(q{1,2\,4,3}) ); is_deeply ([ $csv->fields ], [ 1, "2,4", 3 ], "escaped sep in quoted field"); } { # https://github.com/makamaka/Text-CSV/pull/3 { package FakeFileHandleForEOF; sub new { return bless { line => "foo,bar,baz\n" }, shift } sub getline { my $self = shift; return delete $self->{line}; } sub eof { my $self = shift; return not exists $self->{line}; } } my $csv = Text::CSV->new({binary => 1}); my $fh = FakeFileHandleForEOF->new; ok(!$fh->eof); eval { is_deeply( $csv->getline($fh), [qw[ foo bar baz ]]) }; is($@, '', "no exception thrown"); ok($fh->eof); } Text-CSV-1.33/t/15_flags.t0000644000175000017500000002417012475365663013563 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; # use warnings core since 5.6 use Test::More tests => 197; BEGIN { $ENV{PERL_TEXT_CSV} = 0; use_ok "Text::CSV"; plan skip_all => "Cannot load Text::CSV" if $@; } sub crnlsp { my $csv = shift; ok (!$csv->parse (), "Missing arguments"); ok ( $csv->parse ("\n"), "NL"); if ($csv->eol eq "\r") { ok ( $csv->parse ("\r"), "CR"); ok ( $csv->parse ("\r\r"), "CR CR"); ok ( $csv->parse ("\r "), "CR + Space"); ok ( $csv->parse (" \r"), "Space + CR"); } else { ok (!$csv->parse ("\r"), "CR"); ok (!$csv->parse ("\r\r"), "CR CR"); if ($csv->binary) { ok ( $csv->parse ("\r "), "CR + Space"); ok ( $csv->parse (" \r"), "Space + CR"); } else { ok (!$csv->parse ("\r "), "CR + Space"); ok (!$csv->parse (" \r"), "Space + CR"); } } ok ( $csv->parse ("\r\n"), "CR NL"); ok ( $csv->parse ("\n "), "NL + Space"); ok ( $csv->parse ("\r\n "), "CR NL + Space"); if ($csv->binary) { ok ( $csv->parse (qq{"\n"}), "Quoted NL"); ok ( $csv->parse (qq{"\r"}), "Quoted CR"); ok ( $csv->parse (qq{"\r\n"}), "Quoted CR NL"); ok ( $csv->parse (qq{"\n "}), "Quoted NL + Space"); ok ( $csv->parse (qq{"\r "}), "Quoted CR + Space"); ok ( $csv->parse (qq{"\r\n "}), "Quoted CR NL + Space"); } else { ok (!$csv->parse (qq{"\n"}), "Quoted NL"); ok (!$csv->parse (qq{"\r"}), "Quoted CR"); ok (!$csv->parse (qq{"\r\n"}), "Quoted CR NL"); ok (!$csv->parse (qq{"\n "}), "Quoted NL + Space"); ok (!$csv->parse (qq{"\r "}), "Quoted CR + Space"); ok (!$csv->parse (qq{"\r\n "}), "Quoted CR NL + Space"); } ok (!$csv->parse (qq{"\r\r\n"\r}), "Quoted CR CR NL >CR"); ok (!$csv->parse (qq{"\r\r\n"\r\r}), "Quoted CR CR NL >CR CR"); ok (!$csv->parse (qq{"\r\r\n"\r\r\n}), "Quoted CR CR NL >CR CR NL"); ok (!$csv->parse (qq{"\r\r\n"\t \r}), "Quoted CR CR NL >TAB Space CR"); ok (!$csv->parse (qq{"\r\r\n"\t \r\r}), "Quoted CR CR NL >TAB Space CR CR"); ok (!$csv->parse (qq{"\r\r\n"\t \r\r\n}), "Quoted CR CR NL >TAB Space CR CR NL"); } # crnlsp { my $csv = Text::CSV->new (); my $c99 = chr (0x99); # A random binary character is ($csv->meta_info, undef, "meta_info () before parse ()"); ok (1, "parse () tests - No meta_info"); crnlsp ($csv); ok (!$csv->parse ('"abc'), "Missing closing \""); ok (!$csv->parse ('ab"c'), "\" outside of \"'s"); ok (!$csv->parse ('"ab"c"'), "Bad character sequence"); ok (!$csv->parse ("ab${c99}c"), "Binary character"); ok (!$csv->parse (qq{"ab${c99}c"}), "Binary character in quotes"); ok (!$csv->parse (qq("abc\nc")), "Bad character (NL)"); ok (!$csv->status (), "Wrong status ()"); ok ( $csv->parse ('","'), "comma - parse ()"); is ( scalar $csv->fields (), 1, "comma - fields () - count"); is ( scalar $csv->meta_info (), 0, "comma - meta_info () - count"); is (($csv->fields ())[0], ",", "comma - fields () - content"); is (($csv->meta_info ())[0], undef, "comma - meta_info () - content"); ok ( $csv->parse (qq("","I said,\t""Hi!""","")), "Hi! - parse ()"); is ( scalar $csv->fields (), 3, "Hi! - fields () - count"); is ( scalar $csv->meta_info (), 0, "Hi! - meta_info () - count"); } { my $csv = Text::CSV->new ({ keep_meta_info => 1 }); ok (1, "parse () tests - With flags"); is ( $csv->meta_info, undef, "meta_info before parse"); ok (!$csv->parse (), "Missing arguments"); is ( $csv->meta_info, undef, "meta_info after failing parse"); crnlsp ($csv); ok (!$csv->parse ('"abc'), "Missing closing \""); ok (!$csv->parse ('ab"c'), "\" outside of \"'s"); ok (!$csv->parse ('"ab"c"'), "Bad character sequence"); ok (!$csv->parse (qq("abc\nc")), "Bad character (NL)"); ok (!$csv->status (), "Wrong status ()"); ok ( $csv->parse ('","'), "comma - parse ()"); is ( scalar $csv->fields (), 1, "comma - fields () - count"); is ( scalar $csv->meta_info (), 1, "comma - meta_info () - count"); is (($csv->fields ())[0], ",", "comma - fields () - content"); is (($csv->meta_info ())[0], 1, "comma - meta_info () - content"); ok ( $csv->parse (qq("","I said,\t""Hi!""",)), "Hi! - parse ()"); is ( scalar $csv->fields (), 3, "Hi! - fields () - count"); is ( scalar $csv->meta_info (), 3, "Hi! - meta_info () - count"); is (($csv->fields ())[0], "", "Hi! - fields () - field 1"); is (($csv->meta_info ())[0], 1, "Hi! - meta_info () - field 1"); is (($csv->fields ())[1], qq(I said,\t"Hi!"), "Hi! - fields () - field 2"); is (($csv->meta_info ())[1], 1, "Hi! - meta_info () - field 2"); is (($csv->fields ())[2], "", "Hi! - fields () - field 3"); is (($csv->meta_info ())[2], 0, "Hi! - meta_info () - field 3"); } { my $csv = Text::CSV->new ({ keep_meta_info => 1, eol => "\r" }); ok (1, "parse () tests - With flags"); is ( $csv->meta_info, undef, "meta_info before parse"); ok (!$csv->parse (), "Missing arguments"); is ( $csv->meta_info, undef, "meta_info after failing parse"); crnlsp ($csv); ok (!$csv->parse ('"abc'), "Missing closing \""); ok (!$csv->parse ('ab"c'), "\" outside of \"'s"); ok (!$csv->parse ('"ab"c"'), "Bad character sequence"); ok (!$csv->parse (qq("abc\nc")), "Bad character (NL)"); ok (!$csv->status (), "Wrong status ()"); ok ( $csv->parse ('","'), "comma - parse ()"); is ( scalar $csv->fields (), 1, "comma - fields () - count"); is ( scalar $csv->meta_info (), 1, "comma - meta_info () - count"); is (($csv->fields ())[0], ",", "comma - fields () - content"); is (($csv->meta_info ())[0], 1, "comma - meta_info () - content"); ok ( $csv->parse (qq("","I said,\t""Hi!""",)), "Hi! - parse ()"); is ( scalar $csv->fields (), 3, "Hi! - fields () - count"); is ( scalar $csv->meta_info (), 3, "Hi! - meta_info () - count"); is (($csv->fields ())[0], "", "Hi! - fields () - field 1"); is (($csv->meta_info ())[0], 1, "Hi! - meta_info () - field 1"); is (($csv->fields ())[1], qq(I said,\t"Hi!"), "Hi! - fields () - field 2"); is (($csv->meta_info ())[1], 1, "Hi! - meta_info () - field 2"); is (($csv->fields ())[2], "", "Hi! - fields () - field 3"); is (($csv->meta_info ())[2], 0, "Hi! - meta_info () - field 3"); } { my $csv = Text::CSV->new ({ keep_meta_info => 1, binary => 1 }); is ($csv->is_quoted (0), undef, "is_quoted () before parse"); is ($csv->is_binary (0), undef, "is_binary () before parse"); my $bintxt = chr ($] < 5.006 ? 0xbf : 0x20ac); ok ( $csv->parse (qq{,"1","a\rb",0,"a\nb",1,\x8e,"a\r\n","$bintxt","",}), "parse () - mixed quoted/binary"); is (scalar $csv->fields, 11, "fields () - count"); my @fflg; ok (@fflg = $csv->meta_info, "meta_info ()"); is (scalar @fflg, 11, "meta_info () - count"); is_deeply ([ @fflg ], [ 0, 1, 3, 0, 3, 0, 2, 3, 3, 1, 0 ], "meta_info ()"); is ($csv->is_quoted (0), 0, "fflag 0 - not quoted"); is ($csv->is_binary (0), 0, "fflag 0 - not binary"); is ($csv->is_quoted (2), 1, "fflag 2 - quoted"); is ($csv->is_binary (2), 1, "fflag 2 - binary"); is ($csv->is_quoted (6), 0, "fflag 5 - not quoted"); is ($csv->is_binary (6), 1, "fflag 5 - binary"); is ($csv->is_quoted (-1), undef, "fflag -1 - undefined"); is ($csv->is_binary (-8), undef, "fflag -8 - undefined"); is ($csv->is_quoted (21), undef, "fflag 21 - undefined"); is ($csv->is_binary (98), undef, "fflag 98 - undefined"); } { my $csv = Text::CSV->new ({ escape_char => "+" }); ok ( $csv->parse ("+"), "ESC"); ok (!$csv->parse ("++"), "ESC ESC"); ok ( $csv->parse ("+ "), "ESC Space"); ok ( $csv->parse ("+0"), "ESC NUL"); ok ( $csv->parse ("+\n"), "ESC NL"); ok (!$csv->parse ("+\r"), "ESC CR"); ok ( $csv->parse ("+\r\n"), "ESC CR NL"); ok (!$csv->parse (qq{"+"}), "Quo ESC"); ok (!$csv->parse (qq{""+}), "Quo ESC >"); ok ( $csv->parse (qq{"++"}), "Quo ESC ESC"); ok (!$csv->parse (qq{"+ "}), "Quo ESC Space"); ok ( $csv->parse (qq{"+0"}), "Quo ESC NUL"); ok (!$csv->parse (qq{"+\n"}), "Quo ESC NL"); ok (!$csv->parse (qq{"+\r"}), "Quo ESC CR"); ok (!$csv->parse (qq{"+\r\n"}), "Quo ESC CR NL"); } { my $csv = Text::CSV->new ({ escape_char => "+", binary => 1 }); ok ( $csv->parse ("+"), "ESC"); ok (!$csv->parse ("++"), "ESC ESC"); ok ( $csv->parse ("+ "), "ESC Space"); ok ( $csv->parse ("+0"), "ESC NUL"); ok ( $csv->parse ("+\n"), "ESC NL"); ok (!$csv->parse ("+\r"), "ESC CR"); ok ( $csv->parse ("+\r\n"), "ESC CR NL"); ok (!$csv->parse (qq{"+"}), "Quo ESC"); ok ( $csv->parse (qq{"++"}), "Quo ESC ESC"); ok (!$csv->parse (qq{"+ "}), "Quo ESC Space"); ok ( $csv->parse (qq{"+0"}), "Quo ESC NUL"); ok (!$csv->parse (qq{"+\n"}), "Quo ESC NL"); ok (!$csv->parse (qq{"+\r"}), "Quo ESC CR"); ok (!$csv->parse (qq{"+\r\n"}), "Quo ESC CR NL"); } ok (1, "Testing always_quote"); { my $csv = Text::CSV->new ({ always_quote => 0 }); ok ($csv->combine (1..3), "Combine"); is ($csv->string, q{1,2,3}, "String"); is ($csv->always_quote, 0, "Attr 0"); ok ($csv->always_quote (1), "Attr 1"); ok ($csv->combine (1..3), "Combine"); is ($csv->string, q{"1","2","3"}, "String"); is ($csv->always_quote, 1, "Attr 1"); is ($csv->always_quote (0), 0, "Attr 0"); ok ($csv->combine (1..3), "Combine"); is ($csv->string, q{1,2,3}, "String"); is ($csv->always_quote, 0, "Attr 0"); } ok (1, "Testing quote_space"); { my $csv = Text::CSV->new ({ quote_space => 1 }); ok ($csv->combine (1, " ", 3), "Combine"); is ($csv->string, q{1," ",3}, "String"); is ($csv->quote_space, 1, "Attr 1"); is ($csv->quote_space (0), 0, "Attr 0"); ok ($csv->combine (1, " ", 3), "Combine"); is ($csv->string, q{1, ,3}, "String"); is ($csv->quote_space, 0, "Attr 0"); is ($csv->quote_space (1), 1, "Attr 1"); ok ($csv->combine (1, " ", 3), "Combine"); is ($csv->string, q{1," ",3}, "String"); is ($csv->quote_space, 1, "Attr 1"); } Text-CSV-1.33/t/45_eol.t0000644000175000017500000001661012475365663013251 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; use Test::More tests => 1065; BEGIN { $ENV{PERL_TEXT_CSV} = 0; require_ok "Text::CSV"; plan skip_all => "Cannot load Text::CSV" if $@; require "t/util.pl"; } $| = 1; # Embedded newline tests my $def_rs = $/; foreach my $rs ("\n", "\r\n", "\r") { for $\ (undef, $rs) { my $csv = Text::CSV->new ({ binary => 1 }); $csv->eol ($/ = $rs) unless defined $\; foreach my $pass (0, 1) { if ($pass == 0) { open FH, ">_eol.csv"; } else { open FH, "<_eol.csv"; } foreach my $eol ("", "\r", "\n", "\r\n", "\n\r") { my $s_eol = join " - ", map { defined $_ ? $_ : "" } $\, $rs, $eol; $s_eol =~ s/\r/\\r/g; $s_eol =~ s/\n/\\n/g; my @p; my @f = ("", 1, $eol, " $eol", "$eol ", " $eol ", "'$eol'", "\"$eol\"", " \" $eol \"\n ", "EOL"); if ($pass == 0) { ok ($csv->combine (@f), "combine |$s_eol|"); ok (my $str = $csv->string, "string |$s_eol|"); my $state = $csv->parse ($str); ok ($state, "parse |$s_eol|"); if ($state) { ok (@p = $csv->fields, "fields |$s_eol|"); } else{ is ($csv->error_input, $str, "error |$s_eol|"); } print FH $str; } else { ok (my $row = $csv->getline (*FH), "getline |$s_eol|"); is (ref $row, "ARRAY", "row |$s_eol|"); @p = @$row; } local $, = "|"; is_binary ("@p", "@f", "result |$s_eol|"); } close FH; } unlink "_eol.csv"; } } $/ = $def_rs; { my $csv = Text::CSV->new ({ escape_char => undef }); ok ($csv->parse (qq{"x"\r\n}), "Trailing \\r\\n with no escape char"); is ($csv->eol ("\r"), "\r", "eol set to \\r"); ok ($csv->parse (qq{"x"\r}), "Trailing \\r with no escape char"); ok ($csv->allow_whitespace (1), "Allow whitespace"); ok ($csv->parse (qq{"x" \r}), "Trailing \\r with no escape char"); } SKIP: { $] < 5.008 and skip "\$\\ tests don't work in perl 5.6.x and older", 2; { local $\ = "#\r\n"; my $csv = Text::CSV->new (); open FH, ">_eol.csv"; $csv->print (*FH, [ "a", 1 ]); close FH; open FH, "<_eol.csv"; local $/; is (, "a,1#\r\n", "Strange \$\\"); close FH; unlink "_eol.csv"; } { local $\ = "#\r\n"; my $csv = Text::CSV->new ({ eol => $\ }); open FH, ">_eol.csv"; $csv->print (*FH, [ "a", 1 ]); close FH; open FH, "<_eol.csv"; local $/; is (, "a,1#\r\n", "Strange \$\\ + eol"); close FH; unlink "_eol.csv"; } } $/ = $def_rs; ok (1, "Auto-detecting \\r"); { my @row = qw( a b c ); local $" = ","; for (["\n", "\\n"], ["\r\n", "\\r\\n"], ["\r", "\\r"]) { my ($eol, $s_eol) = @$_; open FH, ">_eol.csv"; print FH qq{@row$eol@row$eol@row$eol\x91}; close FH; open FH, "<_eol.csv"; my $c = Text::CSV->new ({ binary => 1, auto_diag => 1 }); is ($c->eol (), "", "default EOL"); is_deeply ($c->getline (*FH), [ @row ], "EOL 1 $s_eol"); is ($c->eol (), $eol eq "\r" ? "\r" : "", "EOL"); is_deeply ($c->getline (*FH), [ @row ], "EOL 2 $s_eol"); is_deeply ($c->getline (*FH), [ @row ], "EOL 3 $s_eol"); close FH; unlink "_eol.csv"; } } ok (1, "Specific \\r test from tfrayner"); { $/ = "\r"; open FH, ">_eol.csv"; print FH qq{a,b,c$/}, qq{"d","e","f"$/}; close FH; open FH, "<_eol.csv"; my $c = Text::CSV->new ({ eol => $/ }); my $row; local $" = " "; ok ($row = $c->getline (*FH), "getline 1"); is (scalar @$row, 3, "# fields"); is ("@$row", "a b c", "fields 1"); ok ($row = $c->getline (*FH), "getline 2"); is (scalar @$row, 3, "# fields"); is ("@$row", "d e f", "fields 2"); close FH; unlink "_eol.csv"; } $/ = $def_rs; ok (1, "EOL undef"); { $/ = "\r"; ok (my $csv = Text::CSV->new ({ eol => undef }), "new csv with eol => undef"); open FH, ">_eol.csv"; ok ($csv->print (*FH, [1, 2, 3]), "print"); ok ($csv->print (*FH, [4, 5, 6]), "print"); close FH; open FH, "<_eol.csv"; ok (my $row = $csv->getline (*FH), "getline 1"); is (scalar @$row, 5, "# fields"); is_deeply ($row, [ 1, 2, 34, 5, 6], "fields 1"); close FH; unlink "_eol.csv"; } $/ = $def_rs; foreach my $eol ("!", "!!", "!\n", "!\n!", "!!!!!!!!", "!!!!!!!!!!", "\n!!!!!\n!!!!!", "!!!!!\n!!!!!\n", "%^+_\n\0!X**", "\r\n", "\r") { (my $s_eol = $eol) =~ s/\n/\\n/g; $s_eol =~ s/\r/\\r/g; $s_eol =~ s/\0/\\0/g; ok (1, "EOL $s_eol"); ok (my $csv = Text::CSV->new ({ eol => $eol }), "new csv with eol => $s_eol"); open FH, ">_eol.csv"; ok ($csv->print (*FH, [1, 2, 3]), "print"); ok ($csv->print (*FH, [4, 5, 6]), "print"); close FH; foreach my $rs (undef, "", "\n", $eol, "!", "!\n", "\n!", "!\n!", "\n!\n") { local $/ = $rs; (my $s_rs = defined $rs ? $rs : "-- undef --") =~ s/\n/\\n/g; ok (1, "with RS $s_rs"); open FH, "<_eol.csv"; ok (my $row = $csv->getline (*FH), "getline 1"); is (scalar @$row, 3, "field count"); is_deeply ($row, [ 1, 2, 3], "fields 1"); ok ( $row = $csv->getline (*FH), "getline 2"); is (scalar @$row, 3, "field count"); is_deeply ($row, [ 4, 5, 6], "fields 2"); close FH; } unlink "_eol.csv"; } $/ = $def_rs; { open FH, "new ({ auto_diag => 1, binary => 1 }), "new csv"); diag (); ok (my $row = $csv->getline (*FH), "getline 1"); is (scalar @$row, 15, "field count"); is ($row->[7], "", "field 8"); ok ( $row = $csv->getline (*FH), "getline 2"); is (scalar @$row, 15, "field count"); is ($row->[6], "Category", "field 7"); ok ( $row = $csv->getline (*FH), "getline 3"); is (scalar @$row, 15, "field count"); is ($row->[5], "Notes", "field 6"); ok ( $row = $csv->getline (*FH), "getline 4"); is (scalar @$row, 15, "field count"); is ($row->[7], "Points", "field 8"); ok ( $row = $csv->getline (*FH), "getline 5"); is (scalar @$row, 15, "field count"); is ($row->[7], 11, "field 8"); ok ( $row = $csv->getline (*FH), "getline 6"); is (scalar @$row, 15, "field count"); is ($row->[8], 34, "field 9"); ok ( $row = $csv->getline (*FH), "getline 7"); is (scalar @$row, 15, "field count"); is ($row->[7], 12, "field 8"); ok ( $row = $csv->getline (*FH), "getline 8"); is (scalar @$row, 15, "field count"); is ($row->[8], 2, "field 9"); ok ( $row = $csv->getline (*FH), "getline 9"); is (scalar @$row, 15, "field count"); is ($row->[3], "devs", "field 4"); ok ( $row = $csv->getline (*FH), "getline 10"); is (scalar @$row, 15, "field count"); is ($row->[3], "", "field 4"); ok ( $row = $csv->getline (*FH), "getline 11"); is (scalar @$row, 15, "field count"); is ($row->[6], "Mean", "field 7"); ok ( $row = $csv->getline (*FH), "getline 12"); is (scalar @$row, 15, "field count"); is ($row->[6], "Median", "field 7"); ok ( $row = $csv->getline (*FH), "getline 13"); is (scalar @$row, 15, "field count"); is ($row->[6], "Mode", "field 7"); ok ( $row = $csv->getline (*FH), "getline 14"); is (scalar @$row, 15, "field count"); is ($row->[6], "Min", "field 7"); ok ( $row = $csv->getline (*FH), "getline 15"); is (scalar @$row, 15, "field count"); is ($row->[6], "Max", "field 7"); ok ( $row = $csv->getline (*FH), "getline 16"); is (scalar @$row, 15, "field count"); is ($row->[0], "", "field 1"); } 1; Text-CSV-1.33/t/60_samples.t0000644000175000017500000000143112475365663014126 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; use Test::More tests => 8; BEGIN { $ENV{PERL_TEXT_CSV} = 0; use_ok "Text::CSV", (); plan skip_all => "Cannot load Text::CSV" if $@; } # Some assorted examples from the modules history # "Pavel Kotala" { my $csv = Text::CSV->new ({ quote_char => '"', escape_char => '\\', sep_char => ';', binary => 1, }); ok ($csv, "new (\", \\\\, ;, 1)"); my @list = ("c:\\winnt", "text"); ok ($csv->combine (@list), "combine ()"); my $line = $csv->string; ok ($line, "string ()"); ok ($csv->parse ($line), "parse ()"); my @olist = $csv->fields; is (scalar @list, scalar @olist, "field count"); is ($list[0], $olist[0], "field 1"); is ($list[1], $olist[1], "field 2"); } Text-CSV-1.33/t/00_pod.t0000644000175000017500000000025212475365663013236 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; use Test::More; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok (); Text-CSV-1.33/t/65_allow.t0000644000175000017500000003452712475365663013621 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; #use Test::More "no_plan"; use Test::More tests => 1115; BEGIN { $ENV{PERL_TEXT_CSV} = 0; use_ok "Text::CSV", (); plan skip_all => "Cannot load Text::CSV" if $@; require "t/util.pl"; } my $csv; #=pod ok (1, "Allow unescaped quotes"); # Allow unescaped quotes inside an unquoted field { my @bad = ( # valid, line [ 1, 1, 0, qq{foo,bar,"baz",quux} ], [ 2, 0, 2034, qq{rj,bs,r"jb"s,rjbs} ], [ 3, 0, 2034, qq{some "spaced" quote data,2,3,4} ], [ 4, 1, 0, qq{and an,entirely,quoted,"field"} ], [ 5, 1, 0, qq{and then,"one with ""quoted"" quotes",okay,?} ], ); for (@bad) { my ($tst, $valid, $err, $bad) = @$_; $csv = Text::CSV->new (); ok ($csv, "$tst - new (alq => 0)"); is ($csv->parse ($bad), $valid, "$tst - parse () fail"); is (0 + $csv->error_diag, $err, "$tst - error $err"); $csv->allow_loose_quotes (1); ok ($csv->parse ($bad), "$tst - parse () pass"); ok (my @f = $csv->fields, "$tst - fields"); } #$csv = Text::CSV->new ({ quote_char => '"', escape_char => "=" }); #ok (!$csv->parse (qq{foo,d'uh"bar}), "should fail"); } ok (1, "Allow loose quotes inside quoted"); # Allow unescaped quotes inside a quoted field { my @bad = ( # valid, line [ 1, 1, 0, qq{foo,bar,"baz",quux} ], [ 2, 0, 2023, qq{rj,bs,"r"jb"s",rjbs} ], [ 3, 0, 2023, qq{"some "spaced" quote data",2,3,4} ], [ 4, 1, 0, qq{and an,entirely,quoted,"field"} ], [ 5, 1, 0, qq{and then,"one with ""quoted"" quotes",okay,?} ], ); for (@bad) { my ($tst, $valid, $err, $bad) = @$_; $csv = Text::CSV->new (); ok ($csv, "$tst - new (alq => 0)"); is ($csv->parse ($bad), $valid, "$tst - parse () fail"); # is (0 + $csv->error_diag, $err, "$tst - error $err"); ok (($csv->error_diag + 0) >= 0, "Error " . ($csv->error_diag + 0) . " but in CSV_XS $err" . (($csv->error_diag + 0) == $err ? " * same in PP and XS *" : "") ); $csv->allow_loose_quotes (1); is ($csv->parse ($bad), $valid, "$tst - parse () fail with lq"); # is (0 + $csv->error_diag, $err, "$tst - error $err"); ok (($csv->error_diag + 0) >= 0, "Error " . ($csv->error_diag + 0) . " but in CSV_XS $err" . (($csv->error_diag + 0) == $err ? " * same in PP and XS *" : "") ); $csv->escape_char (undef); ok ($csv->parse ($bad), "$tst - parse () pass"); ok (my @f = $csv->fields, "$tst - fields"); } } ok (1, "Allow loose escapes"); # Allow escapes to escape characters that should not be escaped { my @bad = ( # valid, line [ 1, 1, 0, qq{1,foo,bar,"baz",quux} ], [ 2, 1, 0, qq{2,escaped,"quote\\"s",in,"here"} ], [ 3, 1, 0, qq{3,escaped,quote\\"s,in,"here"} ], [ 4, 1, 0, qq{4,escap\\'d chars,allowed,in,unquoted,fields} ], [ 5, 0, 2025, qq{5,42,"and it\\'s dog",} ], [ 6, 1, 0, qq{\\,} ], [ 7, 1, 0, qq{\\} ], [ 8, 0, 2035, qq{foo\\} ], ); for (@bad) { my ($tst, $valid, $err, $bad) = @$_; $csv = Text::CSV->new ({ escape_char => "\\" }); ok ($csv, "$tst - new (ale => 0)"); is ($csv->parse ($bad), $valid, "$tst - parse () fail"); # is (0 + $csv->error_diag, $err, "$tst - error $err"); ok (($csv->error_diag + 0) >= 0, "Error " . ($csv->error_diag + 0) . " but in CSV_XS $err" . (($csv->error_diag + 0) == $err ? " * same in PP and XS *" : "") ); $csv->allow_loose_escapes (1); if ($tst >= 8) { # Should always fail ok (!$csv->parse ($bad), "$tst - parse () fail"); # is (0 + $csv->error_diag, $err, "$tst - error $err"); ok (($csv->error_diag + 0) >= 0, "Error " . ($csv->error_diag + 0) . " but in CSV_XS $err" . (($csv->error_diag + 0) == $err ? " * same in PP and XS *" : "") ); } else { ok ($csv->parse ($bad), "$tst - parse () pass"); ok (my @f = $csv->fields, "$tst - fields"); } } } ok (1, "Allow whitespace"); # Allow whitespace to surround sep char { my @bad = ( # valid, line [ 1, 1, 0, qq{1,foo,bar,baz,quux} ], [ 2, 1, 0, qq{1,foo,bar,"baz",quux} ], [ 3, 1, 0, qq{1, foo,bar,"baz",quux} ], [ 4, 1, 0, qq{ 1,foo,bar,"baz",quux} ], [ 5, 0, 2034, qq{1,foo,bar, "baz",quux} ], [ 6, 1, 0, qq{1,foo ,bar,"baz",quux} ], [ 7, 1, 0, qq{1,foo,bar,"baz",quux } ], [ 8, 1, 0, qq{1,foo,bar,"baz","quux"} ], [ 9, 0, 2023, qq{1,foo,bar,"baz" ,quux} ], [ 10, 0, 2023, qq{1,foo,bar,"baz","quux" } ], [ 11, 0, 2034, qq{ 1 , foo , bar , "baz" , quux } ], [ 12, 0, 2034, qq{ 1 , foo , bar , "baz" , quux } ], [ 13, 0, 2034, qq{ 1 , foo , bar , "baz"\t , quux } ], ); foreach my $eol ("", "\n", "\r", "\r\n") { my $s_eol = _readable ($eol); for (@bad) { my ($tst, $ok, $err, $bad) = @$_; $csv = Text::CSV->new ({ eol => $eol, binary => 1 }); ok ($csv, "$s_eol / $tst - new - '$bad')"); is ($csv->parse ($bad), $ok, "$s_eol / $tst - parse () fail"); # is (0 + $csv->error_diag, $err, "$tst - error $err"); ok (($csv->error_diag + 0) >= 0, "Error " . ($csv->error_diag + 0) . " but in CSV_XS $err" . (($csv->error_diag + 0) == $err ? " * same in PP and XS *" : "") ); $csv->allow_whitespace (1); ok ($csv->parse ("$bad$eol"), "$s_eol / $tst - parse () pass"); ok (my @f = $csv->fields, "$s_eol / $tst - fields"); local $" = ","; is ("@f", $bad[0][-1], "$s_eol / $tst - content"); } } } ok (1, "Allow whitespace"); # Allow whitespace to surround sep char { my @bad = ( # test, ok, line [ 1, 1, 0, qq{1,foo,bar,baz,quux} ], [ 2, 1, 0, qq{1,foo,bar,"baz",quux} ], [ 3, 1, 0, qq{1, foo,bar,"baz",quux} ], [ 4, 1, 0, qq{ 1,foo,bar,"baz",quux} ], [ 5, 0, 2034, qq{1,foo,bar, "baz",quux} ], [ 6, 1, 0, qq{1,foo ,bar,"baz",quux} ], [ 7, 1, 0, qq{1,foo,bar,"baz",quux } ], [ 8, 1, 0, qq{1,foo,bar,"baz","quux"} ], [ 9, 0, 2023, qq{1,foo,bar,"baz" ,quux} ], [ 10, 0, 2023, qq{1,foo,bar,"baz","quux" } ], [ 11, 0, 2023, qq{1,foo,bar,"baz","quux" } ], [ 12, 0, 2034, qq{ 1 , foo , bar , "baz" , quux } ], [ 13, 0, 2034, qq{ 1 , foo , bar , "baz" , quux } ], [ 14, 0, 2034, qq{ 1 , foo , bar , "baz"\t , quux } ], ); foreach my $eol ("", "\n", "\r", "\r\n") { my $s_eol = _readable ($eol); for (@bad) { my ($tst, $ok, $err, $bad) = @$_; $csv = Text::CSV->new ({ eol => $eol, binary => 1, }); ok ($csv, "$s_eol / $tst - new - '$bad')"); is ($csv->parse ($bad), $ok, "$s_eol / $tst - parse () fail"); # is (0 + $csv->error_diag, $err, "$tst - error $err"); ok (($csv->error_diag + 0) >= 0, "Error " . ($csv->error_diag + 0) . " but in CSV_XS $err" . (($csv->error_diag + 0) == $err ? " * same in PP and XS *" : "") ); $csv->allow_whitespace (1); ok ($csv->parse ("$bad$eol"), "$s_eol / $tst - parse () pass"); ok (my @f = $csv->fields, "$s_eol / $tst - fields"); local $" = ","; is ("@f", $bad[0][-1], "$s_eol / $tst - content"); } } } ok (1, "blank_is_undef"); foreach my $conf ( [ 0, 0, 0, 1, "", " ", '""', 2, "", "", "" ], [ 0, 0, 1, 1, undef, " ", '""', 2, undef, undef, undef ], [ 0, 1, 0, 1, "", " ", '""', 2, "", "", "" ], [ 0, 1, 1, 1, undef, " ", '""', 2, undef, undef, undef ], [ 1, 0, 0, 1, "", " ", '""', 2, "", "", "" ], [ 1, 0, 1, 1, "", " ", '""', 2, undef, "", undef ], [ 1, 1, 0, 1, "", " ", '""', 2, "", "", "" ], [ 1, 1, 1, 1, "", " ", '""', 2, undef, "", undef ], ) { my ($aq, $aw, $bu, @expect, $str) = @$conf; $csv = Text::CSV->new ({ always_quote => $aq, allow_whitespace => $aw, blank_is_undef => $bu }); ok ($csv, "new ({ aq $aq aw $aw bu $bu })"); ok ($csv->combine (1, "", " ", '""', 2, undef, "", undef), "combine ()"); ok ($str = $csv->string, "string ()"); foreach my $eol ("", "\n", "\r\n") { my $s_eol = _readable ($eol); ok ($csv->parse ($str.$eol), "parse (*$str$s_eol*)"); ok (my @f = $csv->fields, "fields ()"); is_deeply (\@f, \@expect, "result"); } } ok (1, "empty_is_undef"); foreach my $conf ( [ 0, 0, 0, 1, "", " ", '""', 2, "", "", "" ], [ 0, 0, 1, 1, undef, " ", '""', 2, undef, undef, undef ], [ 0, 1, 0, 1, "", " ", '""', 2, "", "", "" ], [ 0, 1, 1, 1, undef, " ", '""', 2, undef, undef, undef ], [ 1, 0, 0, 1, "", " ", '""', 2, "", "", "" ], [ 1, 0, 1, 1, undef, " ", '""', 2, undef, undef, undef ], [ 1, 1, 0, 1, "", " ", '""', 2, "", "", "" ], [ 1, 1, 1, 1, undef, " ", '""', 2, undef, undef, undef ], ) { my ($aq, $aw, $bu, @expect, $str) = @$conf; $csv = Text::CSV->new ({ always_quote => $aq, allow_whitespace => $aw, empty_is_undef => $bu }); ok ($csv, "new ({ aq $aq aw $aw bu $bu })"); ok ($csv->combine (1, "", " ", '""', 2, undef, "", undef), "combine ()"); ok ($str = $csv->string, "string ()"); foreach my $eol ("", "\n", "\r\n") { my $s_eol = _readable ($eol); ok ($csv->parse ($str.$eol), "parse (*$str$s_eol*)"); ok (my @f = $csv->fields, "fields ()"); is_deeply (\@f, \@expect, "result"); } } ok (1, "Trailing junk"); foreach my $bin (0, 1) { foreach my $eol (undef, "\r") { my $s_eol = _readable ($eol); my $csv = Text::CSV->new ({ binary => $bin, eol => $eol }); ok ($csv, "$s_eol - new ()"); my @bad = ( # test, line [ 1, qq{"\r\r\n"\r} ], [ 2, qq{"\r\r\n"\r\r} ], [ 3, qq{"\r\r\n"\r\r\n} ], [ 4, qq{"\r\r\n"\t \r} ], [ 5, qq{"\r\r\n"\t \r\r} ], [ 6, qq{"\r\r\n"\t \r\r\n} ], ); my @pass = ( 0, 0, 0, 1 ); my @fail = ( 2022, 2022, 2023, 0 ); foreach my $arg (@bad) { my ($tst, $bad) = @$arg; my $ok = ($bin << 1) | ($eol ? 1 : 0); is ($csv->parse ($bad), $pass[$ok], "$tst $ok - parse () default"); # is (0 + $csv->error_diag, $fail[$ok], "$tst $ok - error $fail[$ok]"); ok (($csv->error_diag + 0) >= 0, "Error " . ($csv->error_diag + 0) . " but in CSV_XS $fail[$ok]" . (($csv->error_diag + 0) == $fail[$ok] ? " * same in PP and XS *" : "") ); $csv->allow_whitespace (1); is ($csv->parse ($bad), $pass[$ok], "$tst $ok - parse () allow"); # is (0 + $csv->error_diag, $fail[$ok], "$tst $ok - error $fail[$ok]"); ok (($csv->error_diag + 0) >= 0, "Error " . ($csv->error_diag + 0) . " but in CSV_XS $fail[$ok]" . (($csv->error_diag + 0) == $fail[$ok] ? " * same in PP and XS *" : "") ); } } } #=cut { ok (1, "verbatim"); my $csv = Text::CSV->new ({ sep_char => "^", binary => 1, }); my @str = ( qq{M^^Abe^Timmerman#\r\n}, qq{M^^Abe\nTimmerman#\r\n}, ); my $gc; ok (1, "verbatim on parse ()"); foreach $gc (0, 1) { $csv->verbatim ($gc); ok ($csv->parse ($str[0]), "\\n $gc parse"); my @fld = $csv->fields; is (@fld, 4, "\\n $gc fields"); is ($fld[2], "Abe", "\\n $gc fld 2"); if ($gc) { # Note line ending is still there! is ($fld[3], "Timmerman#\r\n", "\\n $gc fld 3"); } else { # Note the stripped \r! is ($fld[3], "Timmerman#", "\\n $gc fld 3"); } ok ($csv->parse ($str[1]), "\\n $gc parse"); @fld = $csv->fields; is (@fld, 3, "\\n $gc fields"); if ($gc) { # All newlines verbatim is ($fld[2], "Abe\nTimmerman#\r\n", "\\n $gc fld 2"); } else { # Note, rest is next line is ($fld[2], "Abe", "\\n $gc fld 2"); } } $csv->eol ($/ = "#\r\n"); foreach $gc (0, 1) { $csv->verbatim ($gc); ok ($csv->parse ($str[0]), "#\\r\\n $gc parse"); my @fld = $csv->fields; is (@fld, 4, "#\\r\\n $gc fields"); is ($fld[2], "Abe", "#\\r\\n $gc fld 2"); is ($fld[3], $gc ? "Timmerman#\r\n" : "Timmerman#", "#\\r\\n $gc fld 3"); ok ($csv->parse ($str[1]), "#\\r\\n $gc parse"); @fld = $csv->fields; is (@fld, 3, "#\\r\\n $gc fields"); is ($fld[2], $gc ? "Abe\nTimmerman#\r\n" : "Abe", "#\\r\\n $gc fld 2"); } ok (1, "verbatim on getline (*FH)"); open FH, ">_65test.csv"; print FH @str, "M^Abe^*\r\n"; close FH; foreach $gc (0, 1) { $csv->verbatim ($gc); open FH, "<_65test.csv"; my $row; ok ($row = $csv->getline (*FH), "#\\r\\n $gc getline"); is (@$row, 4, "#\\r\\n $gc fields"); is ($row->[2], "Abe", "#\\r\\n $gc fld 2"); is ($row->[3], "Timmerman", "#\\r\\n $gc fld 3"); ok ($row = $csv->getline (*FH), "#\\r\\n $gc parse"); is (@$row, 3, "#\\r\\n $gc fields"); is ($row->[2], $gc ? "Abe\nTimmerman" : "Abe", "#\\r\\n $gc fld 2"); } $gc = $csv->verbatim (); ok (my $row = $csv->getline (*FH), "#\\r\\n $gc parse EOF"); is (@$row, 3, "#\\r\\n $gc fields"); is ($row->[2], "*\r\n", "#\\r\\n $gc fld 2"); close FH; $csv = Text::CSV->new ({ binary => 0, verbatim => 1, eol => "#\r\n", }); open FH, ">_65test.csv"; print FH $str[1]; close FH; open FH, "<_65test.csv"; is ($csv->getline (*FH), undef, "#\\r\\n $gc getline 2030"); is (0 + $csv->error_diag, 2030, "Got 2030"); close FH; unlink "_65test.csv"; } { ok (1, "keep_meta_info on getline ()"); my $csv = Text::CSV->new ({ eol => "\n" }); open FH, ">_65test.csv"; print FH qq{1,"",,"Q",2\n}; close FH; is ($csv->keep_meta_info (0), 0, "No meta info"); open FH, "<_65test.csv"; my $row = $csv->getline (*FH); ok ($row, "Get 1st line"); $csv->error_diag (); is ($csv->is_quoted (2), undef, "Is field 2 quoted?"); is ($csv->is_quoted (3), undef, "Is field 3 quoted?"); close FH; open FH, ">_65test.csv"; print FH qq{1,"",,"Q",2\n}; close FH; is ($csv->keep_meta_info (1), 1, "Keep meta info"); open FH, "<_65test.csv"; $row = $csv->getline (*FH); ok ($row, "Get 2nd line"); $csv->error_diag (); is ($csv->is_quoted (2), 0, "Is field 2 quoted?"); is ($csv->is_quoted (3), 1, "Is field 3 quoted?"); close FH; unlink "_65test.csv"; } { my $csv = Text::CSV->new ({}); my $s2023 = qq{2023,",2008-04-05," \tFoo, Bar",\n}; # " # ^ is ( $csv->parse ($s2023), 0, "Parse 2023"); is (($csv->error_diag)[0], 2027, "Fail code 2027, but 2023 in CSV_XS"); is (($csv->error_diag)[2], 5, "Fail position"); is ( $csv->allow_whitespace (1), 1, "Allow whitespace"); is ( $csv->parse ($s2023), 0, "Parse 2023"); is (($csv->error_diag)[0], 2027, "Fail code 2027, but 2023 in CSV_XS"); is (($csv->error_diag)[2], 5, "Space is eaten now"); } Text-CSV-1.33/t/12_acc.t0000755000175000017500000001503312475365663013213 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; # use warnings core since 5.6 use Test::More tests => 135; # XS's t/12_acc.t + 2 (for decode_utf8) BEGIN { $ENV{PERL_TEXT_CSV} = 0; use_ok "Text::CSV"; plan skip_all => "Cannot load Text::CSV" if $@; } my $csv; ok ($csv = Text::CSV->new, "new ()"); is ($csv->quote_char, '"', "quote_char"); is ($csv->escape_char, '"', "escape_char"); is ($csv->sep_char, ',', "sep_char"); is ($csv->eol, '', "eol"); is ($csv->always_quote, 0, "always_quote"); is ($csv->binary, 0, "binary"); is ($csv->keep_meta_info, 0, "keep_meta_info"); is ($csv->allow_loose_quotes, 0, "allow_loose_quotes"); is ($csv->allow_loose_escapes, 0, "allow_loose_escapes"); is ($csv->allow_unquoted_escape, 0, "allow_unquoted_escape"); is ($csv->allow_whitespace, 0, "allow_whitespace"); is ($csv->blank_is_undef, 0, "blank_is_undef"); is ($csv->empty_is_undef, 0, "empty_is_undef"); is ($csv->auto_diag, 0, "auto_diag"); is ($csv->diag_verbose, 0, "diag_verbose"); is ($csv->verbatim, 0, "verbatim"); is ($csv->quote_space, 1, "quote_space"); is ($csv->quote_null, 1, "quote_null"); is ($csv->quote_binary, 1, "quote_binary"); is ($csv->record_number, 0, "record_number"); is ($csv->decode_utf8, 1, "decode_utf8"); is ($csv->binary (1), 1, "binary (1)"); my @fld = ( 'txt =, "Hi!"', "Yes", "", 2, undef, "1.09", "\r", undef ); ok ($csv->combine (@fld), "combine"); is ($csv->string, qq{"txt =, ""Hi!""",Yes,,2,,1.09,"\r",}, "string"); is ($csv->sep_char (";"), ';', "sep_char (;)"); is ($csv->quote_char ("="), '=', "quote_char (=)"); is ($csv->eol (undef), "", "eol (undef)"); is ($csv->eol (""), "", "eol ('')"); is ($csv->eol ("\r"), "\r", "eol (\\r)"); is ($csv->keep_meta_info (1), 1, "keep_meta_info (1)"); is ($csv->always_quote (undef), 0, "always_quote (undef)"); is ($csv->always_quote (1), 1, "always_quote (1)"); is ($csv->allow_loose_quotes (1), 1, "allow_loose_quotes (1)"); is ($csv->allow_loose_escapes (1), 1, "allow_loose_escapes (1)"); is ($csv->allow_unquoted_escape (1), 1, "allow_unquoted_escape (1)"); is ($csv->allow_whitespace (1), 1, "allow_whitespace (1)"); is ($csv->blank_is_undef (1), 1, "blank_is_undef (1)"); is ($csv->empty_is_undef (1), 1, "empty_is_undef (1)"); is ($csv->auto_diag (1), 1, "auto_diag (1)"); is ($csv->auto_diag (2), 2, "auto_diag (2)"); is ($csv->auto_diag (9), 9, "auto_diag (9)"); is ($csv->auto_diag ("true"), 1, "auto_diag (\"true\")"); is ($csv->auto_diag (undef), 0, "auto_diag (undef)"); is ($csv->auto_diag (""), 0, "auto_diag (\"\")"); is ($csv->diag_verbose (1), 1, "diag_verbose (1)"); is ($csv->diag_verbose (2), 2, "diag_verbose (2)"); is ($csv->diag_verbose (9), 9, "diag_verbose (9)"); is ($csv->diag_verbose ("true"), 1, "diag_verbose (\"true\")"); is ($csv->diag_verbose (undef), 0, "diag_verbose (undef)"); is ($csv->diag_verbose (""), 0, "diag_verbose (\"\")"); is ($csv->verbatim (1), 1, "verbatim (1)"); is ($csv->quote_space (1), 1, "quote_space (1)"); is ($csv->quote_null (1), 1, "quote_null (1)"); is ($csv->quote_binary (1), 1, "quote_binary (1)"); is ($csv->escape_char ("\\"), "\\", "escape_char (\\)"); ok ($csv->combine (@fld), "combine"); is ($csv->string, qq{=txt \\=, "Hi!"=;=Yes=;==;=2=;;=1.09=;=\r=;\r}, "string"); is ($csv->quote_space (0), 0, "quote_space (0)"); is ($csv->quote_null (0), 0, "quote_null (0)"); is ($csv->quote_binary (0), 0, "quote_binary (0)"); is ($csv->decode_utf8 (0), 0, "decode_utf8 (0)"); # Funny settings, all three translate to \0 internally ok ($csv = Text::CSV->new ({ sep_char => undef, quote_char => undef, escape_char => undef, }), "new (undef ...)"); is ($csv->sep_char, undef, "sep_char undef"); is ($csv->quote_char, undef, "quote_char undef"); is ($csv->escape_char, undef, "escape_char undef"); ok ($csv->parse ("foo"), "parse (foo)"); $csv->sep_char (","); is ($csv->record_number, 1, "record_number"); ok ($csv->parse ("foo"), "parse (foo)"); is ($csv->record_number, 2, "record_number"); ok (!$csv->parse ("foo,foo\0bar"), "parse (foo)"); $csv->escape_char ("\\"); ok (!$csv->parse ("foo,foo\0bar"), "parse (foo)"); $csv->binary (1); ok ( $csv->parse ("foo,foo\0bar"), "parse (foo)"); # Some forbidden combinations foreach my $ws (" ", "\t") { ok ($csv = Text::CSV->new ({ escape_char => $ws }), "New blank escape"); eval { ok ($csv->allow_whitespace (1), "Allow ws") }; is (($csv->error_diag)[0], 1002, "Wrong combo"); ok ($csv = Text::CSV->new ({ quote_char => $ws }), "New blank quote"); eval { ok ($csv->allow_whitespace (1), "Allow ws") }; is (($csv->error_diag)[0], 1002, "Wrong combo"); ok ($csv = Text::CSV->new ({ allow_whitespace => 1 }), "New ws 1"); eval { ok ($csv->escape_char ($ws), "esc") }; is (($csv->error_diag)[0], 1002, "Wrong combo"); ok ($csv = Text::CSV->new ({ allow_whitespace => 1 }), "New ws 1"); eval { ok ($csv->quote_char ($ws), "esc") }; is (($csv->error_diag)[0], 1002, "Wrong combo"); } eval { $csv = Text::CSV->new ({ escape_char => "\t", quote_char => " ", allow_whitespace => 1, }) }; like ((Text::CSV::error_diag)[1], qr{^INI - allow_whitespace}, "Wrong combo - error message"); is ((Text::CSV::error_diag)[0], 1002, "Wrong combo - numeric error"); # Test 1003 in constructor foreach my $x ("\r", "\n", "\r\n", "x\n", "\rx") { foreach my $attr (qw( sep_char quote_char escape_char )) { eval { $csv = Text::CSV->new ({ $attr => $x }) }; is ((Text::CSV::error_diag)[0], 1003, "eol in $attr"); } } # Test 1003 in methods foreach my $attr (qw( sep_char quote_char escape_char )) { ok ($csv = Text::CSV->new, "New"); eval { ok ($csv->$attr ("\n"), "$attr => \\n") }; is (($csv->error_diag)[0], 1003, "not allowed"); } # And test erroneous calls is (Text::CSV::new (0), undef, "new () as function"); is (Text::CSV::error_diag () . '', "usage: my \$csv = Text::CSV_PP->new ([{ option => value, ... }]);", "Generic usage () message"); is (Text::CSV->new ({ oel => "" }), undef, "typo in attr"); is (Text::CSV::error_diag () . '', "INI - Unknown attribute 'oel'", "Unsupported attr"); is (Text::CSV->new ({ _STATUS => "" }), undef, "private attr"); is (Text::CSV::error_diag () . '', "INI - Unknown attribute '_STATUS'", "Unsupported private attr"); foreach my $arg (undef, 0, "", " ", 1, [], [ 0 ], *STDOUT) { is (Text::CSV->new ($arg), undef, "Illegal type for first arg"); is ((Text::CSV::error_diag)[0], 1000, "Should be a hashref - numeric error"); } 1; Text-CSV-1.33/t/41_null.t0000644000175000017500000000403212475365663013433 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; use Test::More tests => 128; BEGIN { $ENV{PERL_TEXT_CSV} = 0; plan skip_all => "Cannot load Text::CSV" if $@; } use Text::CSV; my @pat = ( "00", "\00", "0\0", "\0\0", "0\n0", "\0\n0", "0\n\0", "\0\n\0", "\"0\n0", "\"\0\n0", "\"0\n\0", "\"\0\n\0", "\"0\n\"0", "\"\0\n\"0", "\"0\n\"\0", "\"\0\n\"\0", "0\n0", "\0\n0", "0\n\0", "\0\n\0", ); my %exp = map { my $x = $_; $x =~ s/\0/\\0/g; $x =~ s/\n/\\n/g; ($_ => $x); } @pat; my $line = ["", undef, "0\n", "", "\0\0\n0"]; my $csv = Text::CSV->new ({ eol => "\n", binary => 1, auto_diag => 1, blank_is_undef => 1, }); ok ($csv->combine (@$line), "combine [ ... ]"); is ($csv->string, qq{,,"0\n",,""0"0\n0"\n}, "string"); open FH, ">__41test.csv" or die $!; binmode FH; for (@pat) { ok ($csv->print (*FH, [ $_ ]), "print $exp{$_}"); } $csv->always_quote (1); ok ($csv->print (*FH, $line), "print [ ... ]"); close FH; open FH, "<__41test.csv" or die $!; binmode FH; foreach my $pat (@pat) { ok (my $row = $csv->getline (*FH), "getline $exp{$pat}"); is ($row->[0], $pat, "data $exp{$pat}"); } is_deeply ($csv->getline (*FH), $line, "read [ ... ]"); close FH; unlink "__41test.csv"; $csv = Text::CSV->new ({ eol => "\n", binary => 1, auto_diag => 1, blank_is_undef => 1, quote_null => 0, }); ok ($csv->combine (@$line), "combine [ ... ]"); is ($csv->string, qq{,,"0\n",,"\0\0\n0"\n}, "string"); open FH, ">__41test.csv" or die $!; binmode FH; for (@pat) { ok ($csv->print (*FH, [ $_ ]), "print $exp{$_}"); } $csv->always_quote (1); ok ($csv->print (*FH, $line), "print [ ... ]"); close FH; open FH, "<__41test.csv" or die $!; binmode FH; foreach my $pat (@pat) { ok (my $row = $csv->getline (*FH), "getline $exp{$pat}"); is ($row->[0], $pat, "data $exp{$pat}"); } is_deeply ($csv->getline (*FH), $line, "read [ ... ]"); close FH; unlink "__41test.csv"; Text-CSV-1.33/t/70_rt.t0000755000175000017500000006346412475365663013131 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; #use Test::More "no_plan"; use Test::More tests => 453; BEGIN { $ENV{PERL_TEXT_CSV} = 0; use_ok "Text::CSV", (); plan skip_all => "Cannot load Text::CSV" if $@; require "t/util.pl"; } my $csv_file = "_70test.csv"; END { unlink $csv_file } my ($rt, %input, %desc); while () { if (s/^«(x?[0-9]+)»\s*-?\s*//) { chomp; $rt = $1; $desc{$rt} = $_; next; } s/\\([0-7]{1,3})/chr oct $1/ge; push @{$input{$rt}}, $_; } # Regression Tests based on RT reports { # http://rt.cpan.org/Ticket/Display.html?id=24386 # #24386: \t doesn't work in _XS, works in _PP $rt = 24386; my @lines = @{$input{$rt}}; ok (my $csv = Text::CSV->new ({ sep_char => "\t" }), "RT-$rt: $desc{$rt}"); is ($csv->sep_char, "\t", "sep_char = TAB"); foreach my $line (0 .. $#lines) { ok ($csv->parse ($lines[$line]), "parse line $line"); ok (my @fld = $csv->fields, "Fields for line $line"); is (scalar @fld, 25, "Line $line has 25 fields"); # print STDERR "# $fld[2] - $fld[3]\t- $fld[4]\n"; } } { # http://rt.cpan.org/Ticket/Display.html?id=21530 # 21530: getline () does not return documented value at end of filehandle # IO::Handle was first released with perl 5.00307 $rt = 21530; open FH, ">$csv_file"; print FH @{$input{$rt}}; close FH; ok (my $csv = Text::CSV->new ({ binary => 1 }), "RT-$rt: $desc{$rt}"); open FH, "<$csv_file"; my $row; foreach my $line (1 .. 5) { ok ($row = $csv->getline (*FH), "getline $line"); is (ref $row, "ARRAY", "is arrayref"); is ($row->[0], $line, "Line $line"); } ok (eof FH, "EOF"); is ($row = $csv->getline (*FH), undef, "getline EOF"); close FH; unlink $csv_file; } { # http://rt.cpan.org/Ticket/Display.html?id=21530 # 18703: Fails to use quote_char of '~' $rt = 18703; my ($csv, @fld); ok ($csv = Text::CSV->new ({ quote_char => "~" }), "RT-$rt: $desc{$rt}"); is ($csv->quote_char, "~", "quote_char is '~'"); ok ($csv->parse ($input{$rt}[0]), "Line 1"); ok (@fld = $csv->fields, "Fields"); is (scalar @fld, 1, "Line 1 has only one field"); is ($fld[0], "Style Name", "Content line 1"); # The line has invalid escape. the escape should only be # used for the special characters ok (!$csv->parse ($input{$rt}[1]), "Line 2"); } { # http://rt.cpan.org/Ticket/Display.html?id=15076 # 15076: escape_char before characters that do not need to be escaped. $rt = 15076; my ($csv, @fld); ok ($csv = Text::CSV->new ({ sep_char => ";", escape_char => "\\", allow_loose_escapes => 1, }), "RT-$rt: $desc{$rt}"); ok ($csv->parse ($input{$rt}[0]), "Line 1"); ok (@fld = $csv->fields, "Fields"); is (scalar @fld, 2, "Line 1 has two fields"); is ($fld[0], "Example", "Content field 1"); is ($fld[1], "It's an apostrophee", "Content field 2"); } { # http://rt.cpan.org/Ticket/Display.html?id=34474 # 34474: wish: integrate row-as-hashref feature from Parse::CSV $rt = 34474; open FH, ">$csv_file"; print FH @{$input{$rt}}; close FH; ok (my $csv = Text::CSV->new (), "RT-$rt: $desc{$rt}"); is ($csv->column_names, undef, "No headers yet"); open FH, "<$csv_file"; my $row; ok ($row = $csv->getline (*FH), "getline headers"); is ($row->[0], "code", "Header line"); $csv->column_names (@$row); is_deeply ([ $csv->column_names ], [ @$row ], "Keys set"); while (my $hr = $csv->getline_hr (*FH)) { ok (exists $hr->{code}, "Line has a code field"); like ($hr->{code}, qr/^[0-9]+$/, "Code is numeric"); ok (exists $hr->{name}, "Line has a name field"); like ($hr->{name}, qr/^[A-Z][a-z]+$/, "Name"); } close FH; unlink $csv_file; } { # http://rt.cpan.org/Ticket/Display.html?id=38960 # 38960: print () on invalid filehandle warns and returns success $rt = 38960; open FH, ">$csv_file"; print FH ""; close FH; my $err = ""; open FH, "<$csv_file"; ok (my $csv = Text::CSV->new (), "RT-$rt: $desc{$rt}"); local $SIG{__WARN__} = sub { $err = "Warning" }; ok (!$csv->print (*FH, [ 1 .. 4 ]), "print ()"); is ($err, "Warning", "IO::Handle triggered a warning"); is (($csv->error_diag)[0], 2200, "error 2200"); close FH; unlink $csv_file; } { # http://rt.cpan.org/Ticket/Display.html?id=40507 # 40507: Parsing fails on escaped null byte $rt = 40507; ok (my $csv = Text::CSV->new ({ binary => 1 }), "RT-$rt: $desc{$rt}"); my $str = $input{$rt}[0]; ok ($csv->parse ($str), "parse () correctly escaped NULL"); is_deeply ([ $csv->fields ], [ qq{Audit active: "TRUE \0}, qq{Desired:}, qq{Audit active: "TRUE \0} ], "fields ()"); $str = $input{$rt}[1]; is ($csv->parse ($str), 0, "parse () badly escaped NULL"); my @diag = $csv->error_diag; SKIP: { skip "incompatible between PP and XS", 2; is ($diag[0], 2023, "Error 2023"); is ($diag[2], 23, "Position 23"); } $csv->allow_loose_escapes (1); ok ($csv->parse ($str), "parse () badly escaped NULL"); } { # http://rt.cpan.org/Ticket/Display.html?id=42642 # 42642: failure on unusual quote/sep values $rt = 42642; SKIP: { $] < 5.008002 and skip "UTF8 unreliable in perl $]", 6; open FH, ">$csv_file"; print FH @{$input{$rt}}; close FH; my ($sep, $quo) = ("\x14", "\xfe"); chop ($_ = "$_\x{20ac}") for $sep, $quo; ok (my $csv = Text::CSV->new ({ binary => 1, sep_char => $sep }), "RT-$rt: $desc{$rt}"); ok ($csv->quote_char ($quo), "Set quote_char"); open FH, "<$csv_file"; ok (my $row = $csv->getline (*FH), "getline () with decode sep/quo"); $csv->error_diag (); close FH; unlink $csv_file; is_deeply ($row, [qw( DOG CAT WOMBAT BANDERSNATCH )], "fields ()"); ok ($csv->parse ($input{$rt}[1]), "parse () with decoded sep/quo"); is_deeply ([ $csv->fields ], [ 0..3 ], "fields ()"); } } { # http://rt.cpan.org/Ticket/Display.html?id=43927 # 43927: Is bind_columns broken or am I using it wrong? $rt = 43927; open FH, ">$csv_file"; print FH @{$input{$rt}}; close FH; my ($c1, $c2); ok (my $csv = Text::CSV->new ({ binary => 1 }), "RT-$rt: $desc{$rt}"); ok ($csv->bind_columns (\$c1, \$c2), "bind columns"); open FH, "<$csv_file"; ok (my $row = $csv->getline (*FH), "getline () with bound columns"); $csv->error_diag (); close FH; unlink $csv_file; is_deeply ($row, [], "should return empty ref"); is_deeply ([ $c1, $c2], [ 1, 2 ], "fields ()"); } { # http://rt.cpan.org/Ticket/Display.html?id=44402 # 44402 - Unexpected results parsing tab-separated spaces $rt = 44402; open FH, ">$csv_file"; my @ws = ("", " ", " "); foreach my $f1 (@ws) { foreach my $f2 (@ws) { foreach my $f3 (@ws) { print FH "$f1\t$f2\t$f3\r\n"; } } } close FH; my $csv; ok ($csv = Text::CSV->new ({ sep_char => "\t", }), "RT-$rt: $desc{$rt}"); my $FH; open $FH, "<$csv_file"; while (my $row = $csv->getline ($FH)) { ok ($row, "getline $."); my @row = @$row; is ($#row, 2, "Got 3 fields"); like ($row[$_], qr{^ *$}, "field $_ with only spaces") for 0..2; } ok ($csv->eof, "read complete file"); close FH; ok ($csv = Text::CSV->new ({ sep_char => "\t", allow_whitespace => 1, }), "RT-$rt: $desc{$rt}"); open $FH, "<$csv_file"; while (my $row = $csv->getline ($FH)) { ok ($row, "getline $."); my @row = @$row; is ($#row, 2, "Got 3 fields"); is ($row[$_], "", "field $_ empty") for 0..2; } ok ($csv->eof, "read complete file"); close FH; unlink $csv_file; ok ($csv->parse (" \t \t "), "parse ()"); is_deeply ([$csv->fields],["","",""],"3 empty fields"); } { # Detlev reported an inconsistent difference between _XS and _PP $rt = "x1000"; open FH, ">$csv_file"; print FH @{$input{$rt}}; close FH; my ($c1, $c2); ok (my $csv = Text::CSV->new ({ binary => 1, eol => "\n", sep_char => "\t", escape_char => undef, quote_char => undef, binary => 1 }), "RT-$rt: $desc{$rt}"); open FH, "<$csv_file"; for (1 .. 4) { ok (my $row = $csv->getline (*FH), "getline ()"); is (scalar @$row, 27, "Line $_: 27 columns"); } for (5 .. 6) { ok (my $row = $csv->getline (*FH), "getline ()"); is (scalar @$row, 1, "Line $_: 1 column"); } $csv->error_diag (); close FH; unlink $csv_file; } { # Ruslan reported a case where only Text::CSV_PP misbehaved (regression test) $rt = "x1001"; open FH, ">$csv_file"; print FH @{$input{$rt}}; close FH; my ($c1, $c2); ok (my $csv = Text::CSV->new (), "RT-$rt: $desc{$rt}"); open FH, "<$csv_file"; for (1 .. 4) { ok (my $row = $csv->getline (*FH), "getline ($_)"); is (scalar @$row, 2, "Line $_: 2 columns"); my @exp = $_ <= 2 ? ("0", "A") : ("A", "0"); is_deeply ($row, \@exp, "@exp"); } close FH; unlink $csv_file; } { # http://rt.cpan.org/Ticket/Display.html?id=58356 # 58356 - Incorrect CSV generated if "quote_space => 0" $rt = "58356"; ok (my $csv = Text::CSV->new ({ binary => 1, quote_space => 0 }), "RT-$rt: $desc{$rt}"); my @list = ("a a", "b,b", "c ,c"); ok ($csv->combine (@list), "combine ()"); is ($csv->string, q{a a,"b,b","c ,c"}, "string ()"); } { # http://rt.cpan.org/Ticket/Display.html?id=61525 $rt = "61525"; # First try with eol in constructor foreach my $eol ("\n", "\r", "!") { $/ = "\n"; my $s_eol = _readable ($eol); ok (my $csv = Text::CSV->new ({ binary => 1, sep_char => ":", quote_char => '"', escape_char => '"', eol => $eol, auto_diag => 1, }), "RT-$rt: $desc{$rt} - eol = $s_eol (1)"); open FH, ">$csv_file"; print FH join $eol => qw( "a":"b" "c":"d" "e":"x!y" "!!":"z" ); close FH; open FH, "<$csv_file"; is_deeply ($csv->getline (*FH), [ "a", "b" ], "Pair 1"); is_deeply ($csv->getline (*FH), [ "c", "d" ], "Pair 2"); is_deeply ($csv->getline (*FH), [ "e", "x!y" ], "Pair 3"); is_deeply ($csv->getline (*FH), [ "!!", "z" ], "Pair 4"); is ($csv->getline (*FH), undef, "no more pairs"); ok ($csv->eof, "EOF"); close FH; unlink $csv_file; } # And secondly with eol as method only if not one of the defaults foreach my $eol ("\n", "\r", "!") { $/ = "\n"; my $s_eol = _readable ($eol); ok (my $csv = Text::CSV->new ({ binary => 1, sep_char => ":", quote_char => '"', escape_char => '"', auto_diag => 1, }), "RT-$rt: $desc{$rt} - eol = $s_eol (2)"); $eol eq "!" and $csv->eol ($eol); open FH, ">$csv_file"; print FH join $eol => qw( "a":"b" "c":"d" "e":"x!y" "!!":"z" ); close FH; open FH, "<$csv_file"; is_deeply ($csv->getline (*FH), [ "a", "b" ], "Pair 1"); is_deeply ($csv->getline (*FH), [ "c", "d" ], "Pair 2"); is_deeply ($csv->getline (*FH), [ "e", "x!y" ], "Pair 3"); is_deeply ($csv->getline (*FH), [ "!!", "z" ], "Pair 4"); is ($csv->getline (*FH), undef, "no more pairs"); ok ($csv->eof, "EOF"); close FH; unlink $csv_file; } } { # http://rt.cpan.org/Ticket/Display.html?id=74216 $rt = "74216"; # setting 'eol' affects global input record separator open FH, ">$csv_file"; print FH @{$input{$rt}}; close FH; my $slurp_check = sub { open FH, "<$csv_file"; is (scalar @{[]}, 4); close FH; }; $slurp_check->(); my $crlf = "\015\012"; open FH, ">_$csv_file"; print FH "a,b,c" . $crlf . "1,2,3" . $crlf; close FH; open FH, "<_$csv_file"; my $csv = Text::CSV->new ({ eol => $crlf }); is_deeply ($csv->getline (*FH), [qw( a b c )]); close FH; unlink "_$csv_file"; $slurp_check->(); { local $/ = "\n"; $slurp_check->(); } } SKIP: { # http://rt.cpan.org/Ticket/Display.html?id=74220 $] < 5.008002 and skip "UTF8 unreliable in perl $]", 7; $rt = "74220"; # Text::CSV can be made to produce bad strings my $csv = Text::CSV->new ({ binary => 1 }); my $ax = chr (0xfa); my $bx = "foo"; # We set the UTF-8 flag on a string with no funny characters utf8::upgrade ($bx); is ($bx, "foo", "no funny characters in the string"); ok (utf8::valid ($ax), "first string correct in Perl"); ok (utf8::valid ($bx), "second string correct in Perl"); ok ($csv->combine ($ax, $bx), "combine ()"); ok (my $foo = $csv->string (), "string ()"); ok (utf8::valid ($foo), "is combined string correct inside Perl?"); is ($foo, qq{\xfa,foo}, "expected result"); } =pod SKIP: { # http://rt.cpan.org/Ticket/Display.html?id=80680 skip "skip tests for XS, this tests too long to PP", 20000; (eval { require Encode; $Encode::VERSION } || "0.00") =~ m{^([0-9.]+)}; $1 < 2.47 and skip "Encode is too old for these tests", 20000; $] < 5.008008 and skip "UTF8+Encode unreliable in perl $]", 20000; $rt = "80680"; # Text::CSV produces garbage on some data my $csv = Text::CSV->new ({ binary => 1 }); my $txt = "\x{415}\x{43a}\x{438}\x{43d}\x{431}\x{443}\x{440}\x{433}\x{2116}"; BIG_LOOP: foreach my $n (1 .. 5000) { foreach my $e (0 .. 3) { my $data = ("a" x $e) . ($txt x $n); my $enc = Encode::encode ("UTF-8", $data); my $exp = qq{1,"$enc"}; my $out = ""; open my $fh, ">:encoding(utf-8)", \$out; $csv->print ($fh, [ 1, $data ]); close $fh; my $l = length ($out); if ($out eq $exp) { ok (1, "Buffer boundary check $n/$e ($l)"); next; } is ($out, $exp, "Data $n/$e ($l)"); last BIG_LOOP; } } } =cut { # http://rt.cpan.org/Ticket/Display.html?id=81295 $rt = 81295; # escaped sep_char discarded when only item in unquoted field my $csv = Text::CSV->new ({ escape_char => "\\", auto_diag => 1 }); ok ($csv->parse ($input{$rt}[0]), "parse without allow_unquoted_escape"); is_deeply ([ $csv->fields ], [ 1, ",", 3 ], "escaped sep in quoted field"); $csv->allow_unquoted_escape (1); ok ($csv->parse ($input{$rt}[1]), "parse with allow_unquoted_escape"); is_deeply ([ $csv->fields ], [ 1, ",", 3 ], "escaped sep in unquoted field"); } __END__ «24386» - \t doesn't work in _XS, works in _PP VIN StockNumber Year Make Model MD Engine EngineSize Transmission DriveTrain Trim BodyStyle CityFuel HWYFuel Mileage Color InteriorColor InternetPrice RetailPrice Notes ShortReview Certified NewUsed Image_URLs Equipment 1HGCM66573A030460 1621HA 2003 HONDA ACCORD EX V-6 ACCORD DOHC 16-Valve VTEC 3.0L 5-Speed Automatic EX V-6 4DR 21 30 70940 Gray Gray 15983 15983 AutoWeek calls the 2003 model the best Accord yet * Fun to hustle down a twisty road according to Road & Track * Sedan perfection according to Car and Driver * Named on the 2003 Car and Driver Ten Best List * Named a Consumer Guide Best Buy for 2003 * 0 0 http://vin.windowstickers.biz/incoming/w_1HGCM66573A030460_1.JPG, http://vin.windowstickers.biz/incoming/w_1HGCM66573A030460_2.JPG, http://vin.windowstickers.biz/incoming/w_1HGCM66573A030460_3.JPG, http://vin.windowstickers.biz/incoming/w_1HGCM66573A030460_4.JPG, http://vin.windowstickers.biz/incoming/w_1HGCM66573A030460_5.JPG, http://vin.windowstickers.biz/incoming/w_1HGCM66573A030460_6.JPG, http://vin.windowstickers.biz/incoming/w_1HGCM66573A030460_7.JPG, http://vin.windowstickers.biz/incoming/w_1HGCM66573A030460_8.JPG 120-Watt AM/FM Stereo System,3-Point Seat Belts,4-Wheel Double Wishbone Suspension,6-Disc In-Dash Compact Disc Changer,6-Speaker Audio System,8-Way Power Adjustable Driver's Seat,Air Conditioning w/Air-Filtration System,Anti-Lock Braking System,Automatic-Up/Down Driver's Window,Center Console Armrest w/Storage,Child Safety Rear Door Locks,Cruise Control,Driver & Front Passenger Dual-Stage Airbags,Electronic Remote Trunk Release,Emergency Trunk Release,Fold-Down Rear Seat Center Armrest,Fold-Down Rear Seatback w/Lock,Front Seat Side-Impact Airbags,Immobilizer Theft Deterrent System,LATCH Lower Anchor & Tethers For Children,Power Driver's Seat Height Adjustment,Power Exterior Mirrors,Power Moonroof w/Tilt Feature,Power Windows & Door Locks,Power-Assisted 4-Wheel Disc Brakes,Rear Window Defroster w/Timer,Remote Keyless Entry System w/Window Control,Security System,Tilt & Telescopic Steering Column,Traction Control System,Variable Intermittent Windshield Wipers,Variable-Assist Power Rack & Pinion Steering 1FTRW12W66KA65476 4110J 2006 FORD F-150 XLT CREW 5.5SB 4X2 F-150 SOHC Triton V8 4.6L 4-Speed Automatic 4X2 XLT CREW 5.5SB 15 19 20334 Black Gray 22923 22923 Named a Consumer Guide 2005 & 2006 Best Buy * Named Best Pickup by Car and Driver * The Detroit Free Press calls F-150 the best pickup truck ever * The Detroit News calls F-150 the best America has to offer * 0 0 http://vin.windowstickers.biz/incoming/w_1FTRW12W66KA65476_1.JPG, http://vin.windowstickers.biz/incoming/w_1FTRW12W66KA65476_2.JPG, http://vin.windowstickers.biz/incoming/w_1FTRW12W66KA65476_3.JPG, http://vin.windowstickers.biz/incoming/w_1FTRW12W66KA65476_4.JPG, http://vin.windowstickers.biz/incoming/w_1FTRW12W66KA65476_5.JPG, http://vin.windowstickers.biz/incoming/w_1FTRW12W66KA65476_6.JPG, http://vin.windowstickers.biz/incoming/w_1FTRW12W66KA65476_7.JPG, http://vin.windowstickers.biz/incoming/w_1FTRW12W66KA65476_8.JPG, http://vin.windowstickers.biz/incoming/w_1FTRW12W66KA65476_9.JPG 4-Pin Trailer Tow Connector,Air Conditioning,AM/FM Stereo w/Single Compact Disc Player,Auxiliary Power Outlets,Cargo Box Light & Tie-Downs,Child Safety Seat Lower Anchors & Tether Anchors,Crash Severity Sensor,Cruise Control,Dual-Stage Driver & Front-Right Passenger Airbags,Electronic Brake Force Distribution,Exterior Temperature & Compass Display,Fail-Safe Engine Cooling System,Front Dome Light w/Integrated Map Lights,Front Power Points,Front Seat Personal Safety System,Front-Passenger Sensing System,Manual Day/Night Interior Rearview Mirror,Oil Pressure & Coolant Temperature Gauges,Power 4-Wheel Disc Anti-Lock Brakes,Power Door Locks,Power Exterior Mirrors,Power Front Windows w/One-Touch Driver Side,Power Rack & Pinion Steering,Remote Keyless Entry System,Removable Tailgate w/Key Lock,Securilock Passive Anti-Theft System,Spare Tire w/Wheel Lock,Speed-Dependent Interval Windshield Wipers,Tailgate Assist System,Tilt Steering Wheel,Visors w/Covered Vanity Mirrors 5GZCZ23D03S826657 2111A 2003 SATURN VUE BASE FWD VUE DOHC 4-cylinder 2.2L 5-Speed Manual FWD BASE 5DR 23 28 74877 Silver Gray 11598 11598 Edmunds 2003 Buyer's Guide calls Vue a well-thought-out and capable mini sport utility vehicle, with large doors for ease of entry and exit, extensive cabin space and excellent crash test scores * 0 0 http://vin.windowstickers.biz/incoming/w_5GZCZ23D03S826657_1.JPG, http://vin.windowstickers.biz/incoming/w_5GZCZ23D03S826657_2.JPG, http://vin.windowstickers.biz/incoming/w_5GZCZ23D03S826657_3.JPG, http://vin.windowstickers.biz/incoming/w_5GZCZ23D03S826657_4.JPG, http://vin.windowstickers.biz/incoming/w_5GZCZ23D03S826657_5.JPG, http://vin.windowstickers.biz/incoming/w_5GZCZ23D03S826657_6.JPG, http://vin.windowstickers.biz/incoming/w_5GZCZ23D03S826657_7.JPG, http://vin.windowstickers.biz/incoming/w_5GZCZ23D03S826657_8.JPG 70/30 Split Folding Rear Seatback,AM/FM Stereo System,Center Console w/Storage,Center High-Mounted Rear Stop Light,CFC-Free Air Conditioning,Cloth Upholstery,Daytime Running Lights,Dent-Resistant Polymer Body Panels,Distributorless Ignition System,Driver & Front Passenger Frontal Airbags,Electric Power Rack-And-Pinion Steering,Fold-Flat Front Passenger Seat,Front & Rear Crumple Zones,Front & Rear Cup Holders,Front Bucket Seats,Front-Wheel Drive,Independent Front & Rear Suspension,Interval Rear Window Wiper/Washer,Interval Windshield Wipers,LATCH Child Safety Seat Anchor System,Platinum-Tipped Spark Plugs,Power Front Disc/Rear Drum Brakes,Rear Privacy Glass,Rear Window Defogger,Remote Rear Liftgate Release,Roof Rack,Sequential Fuel Injection,Side-Impact Door Beams,Tachometer,Theft-Deterrent System,Tilt Adjustable Steering Wheel,Visor Vanity Mirrors 1FMZU67K15UB18754 4067T 2005 FORD EXPLORER SPORT TRAC XLT 4X2 EXPLORER SPORT TRAC Flex Fuel SOHC V6 4.0L 5-Speed Automatic 4X2 XLT 4DR 16 21 12758 Maroon Gray 20995 20995 Consumer Guide 2005 reports Sport Trac offers more passenger space than other crew-cab pick-ups and is a good choice as a multipurpose vehicle * Consumer Guide 2005 credits Sport Trac with good in-cabin storage * 0 0 http://vin.windowstickers.biz/incoming/w_1FMZU67K15UB18754_1.JPG, http://vin.windowstickers.biz/incoming/w_1FMZU67K15UB18754_2.JPG, http://vin.windowstickers.biz/incoming/w_1FMZU67K15UB18754_3.JPG, http://vin.windowstickers.biz/incoming/w_1FMZU67K15UB18754_4.JPG, http://vin.windowstickers.biz/incoming/w_1FMZU67K15UB18754_5.JPG, http://vin.windowstickers.biz/incoming/w_1FMZU67K15UB18754_6.JPG, http://vin.windowstickers.biz/incoming/w_1FMZU67K15UB18754_7.JPG 3-Point Front & Rear Seatbelts,4-Speaker Audio System,Air Conditioning,AM/FM Stereo w/Compact Disc Player,Belt-Minder Safety Belt Reminder System,Child Safety Rear Door Locks,Cloth Upholstery,Cruise Control,Driver & Front Passenger Airbags,Driver Door Keyless Entry Keypad,Headlights-On Alert Chime,Height-Adjustable Front Seatbelts,LATCH Child Seat Lower Anchors & Tether Anchors,Locking Tailgate,Low-Back Front Bucket Seats,Lower Bodyside Moldings,Manual Day/Night Interior Rearview Mirror,Power 4-Wheel Disc Anti-Lock Brakes,Power Door Locks,Power Exterior Mirrors,Power Rack & Pinion Steering,Power Rear Window w/Anti-Pinch,Power Windows w/Driver One-Touch Down,Remote Keyless Entry System,Roof Rails,Securilock Passive Anti-Theft System,Side-Intrusion Door Beams,Sirius Satellite Radio/MP3 Capability,Solar-Tinted Glass Windows,Speed-Sensitive Intermittent Windshield Wipers,Tachometer,Tilt Steering Wheel 1J4GK48K96W108753 4068T 2006 JEEP LIBERTY SPORT 4X2 LIBERTY SOHC 12-valve V6 3.7L 4-Speed Automatic 4X2 SPORT 5DR 17 22 12419 Silver Gray 16999 16999 Named on the Automobile Magazine 50 Great New Cars List * Motor Trend reports Liberty fulfills the original go-anywhere mission of SUVs without fail or compromise * A Consumer Guide 2005 & 2006 Recommended Buy * 0 0 http://vin.windowstickers.biz/incoming/w_1J4GK48K96W108753_1.JPG, http://vin.windowstickers.biz/incoming/w_1J4GK48K96W108753_2.JPG, http://vin.windowstickers.biz/incoming/w_1J4GK48K96W108753_3.JPG, http://vin.windowstickers.biz/incoming/w_1J4GK48K96W108753_4.JPG, http://vin.windowstickers.biz/incoming/w_1J4GK48K96W108753_5.JPG, http://vin.windowstickers.biz/incoming/w_1J4GK48K96W108753_6.JPG, http://vin.windowstickers.biz/incoming/w_1J4GK48K96W108753_7.JPG, http://vin.windowstickers.biz/incoming/w_1J4GK48K96W108753_8.JPG, http://vin.windowstickers.biz/incoming/w_1J4GK48K96W108753_9.JPG 12-Volt Cargo Area Power Outlet,65/35 Split-Folding Rear Bench Seat,6-Speaker Audio System,Advanced Multi-Stage Frontal Airbags,Air Conditioning,All-Wheel Traction Control System,AM/FM Stereo w/Compact Disc Player,Center Console 12-Volt Power Outlet,Child Safety Rear Door Locks,Cloth Sun Visors w/Pull-Out Sunshade,Cloth Upholstery,Coolant Temperature Gauge,Electric Rear Window Defroster,Electronic Stability Program,Enhanced Accident Response System,Halogen Headlights w/Delay-Off Feature,LATCH Child Safety Seat Anchor System,Manual Day/Night Interior Rearview Mirror,Power 4-Wheel Disc Anti-Lock Brake System,Power Door Locks,Power Exterior Mirrors,Power Rack & Pinion Steering,Power Windows w/Front One-Touch Down,Rear Window Wiper/Washer,Remote Keyless Entry System,Roof Side Rails,Sentry Key Engine Immobilizer,Spare Tire Carrier,Tachometer,Tilt Steering Column,Tinted Windshield Glass,Variable Speed Intermittent Windshield Wipers «21530» - getline () does not return documented value at end of filehandle 1,1,2,3,4,5 2,1,2,3,4,5 3,1,2,3,4,5 4,1,2,3,4,5 5,1,2,3,4,5 «18703» - Fails to use quote_char of '~' ~Style Name~ ~5dr Crew Cab 130" WB 2WD LS~ ",~"~,~""~,~"""~,,~~, «15076» - escape_char before characters that do not need to be escaped. "Example";"It\'s an apostrophee" «34474» - wish: integrate row-as-hashref feature from Parse::CSV code,name,price,description 1,Dress,240.00,"Evening gown" 2,Drinks,82.78,"Drinks" 3,Sex,-9999.99,"Priceless" «38960» - print () on invalid filehandle warns and returns success «40507» - Parsing fails on escaped null byte "Audit active: ""TRUE "0","Desired:","Audit active: ""TRUE "0" "Audit active: ""TRUE "\0","Desired:","Audit active: ""TRUE "\0" «42642» - failure on unusual quote/sep values þDOGþþCATþþWOMBATþþBANDERSNATCHþ þ0þþ1þþ2þþ3þ «43927» - Is bind_columns broken or am I using it wrong? 1,2 «44402» - Unexpected results parsing tab-separated spaces «x1000» - Detlev reported inconsisten behavior between XS and PP B:033_02_ -drop, +drop animal legs @p 02-033.bmp @p 02-033.bmp  \x{A} 1 :c/b01:!1 ! 13 !6.!6 :b/b01:0 B:033_02_ R#012a 2 B:034_02c diagonal, trac -bound up @p 02-034c.bmp @p 02-034c.bmp  Found through e_sect2.pdf as U+F824 (,) and U+2E88 (⺈,) but won't display \x{A} 1 :c/b01:!1 ! 11 !10 :b/b01:0 2E88 B:034_02c R#018b 2 B:035_02_ +drop, -drop fission 丷  Aufgrund folgender Fälle definiere ich einen neuen Baustein, der simp. mit "horns&" identisch ist.\x{A}隊队 (jap.: pinnacle, horns&sow)\x{A}曾曾兌兑\x{A}über "golden calf":\x{A}é€é€ 1 :c/b01:!1 ! 11 !10 :b/b01:0 4E37 B:035_02_ 2 B:035_03_ fission, one horns @p 03-035.bmp @p 03-035.bmp  obsolete Heising explanation for form without the horizontal line: Variante von "horns", die erscheint, wenn darunter keine horizontale Linie steht\x{A}\x{A}Found through e_sect2.pdf as U+F7EA (??,) but won't display \x{A} 1 :c/b01:!1 ! 11 !10 :b/b01:0 B:035_03_ 3 --------------090302050909040309030109-- «58356» - Incorrect CSV generated if "quote_space => 0" «61525» - eol not working for values other than "\n"? «74216» - setting 'eol' affects global input record separator 1,2 3,4 5,6 7,8 «74330» - Text::CSV can be made to produce bad strings «80680» - Text::CSV produces garbage on some data «81295» - escaped sep_char discarded when only item in unquoted field 1,"\,",3 1,\,,3 «x1001» - Lines starting with "0" (Ruslan Dautkhanov) "0","A" "0","A" "A","0" "A","0" Text-CSV-1.33/t/76_magic.t0000644000175000017500000000222212475365663013550 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; #use Test::More "no_plan"; use Test::More tests => 7; BEGIN { $ENV{PERL_TEXT_CSV} = 0; use_ok "Text::CSV", (); plan skip_all => "Cannot load Text::CSV" if $@; } my $csv = Text::CSV->new ({ binary => 1, eol => "\n" }); my $foo; my @foo = ("#", 1..3); SKIP: { $] < 5.006 and skip "Need perl 5.6.0 or higher for magic here", 2; tie $foo, "Foo"; ok ($csv->combine (@$foo), "combine () from magic"); untie $foo; is_deeply ([$csv->fields], \@foo, "column_names ()"); } tie $foo, "Foo"; open FH, ">_76test.csv"; ok ($csv->print (*FH, $foo), "print with unused magic scalar"); close FH; untie $foo; open FH, "<_76test.csv"; is_deeply ($csv->getline (*FH), \@foo, "Content read-back"); close FH; tie $foo, "Foo"; ok ($csv->column_names ($foo), "column_names () from magic"); untie $foo; is_deeply ([$csv->column_names], \@foo, "column_names ()"); unlink "_76test.csv"; package Foo; use strict; local $^W = 1; require Tie::Scalar; use vars qw( @ISA ); @ISA = qw(Tie::Scalar); sub FETCH { [ "#", 1 .. 3 ]; } # FETCH sub TIESCALAR { bless [], "Foo"; } # TIESCALAR 1; Text-CSV-1.33/t/77_getall.t0000644000175000017500000000410512475365663013743 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; use Test::More tests => 61; BEGIN { $ENV{PERL_TEXT_CSV} = 0; require_ok "Text::CSV"; plan skip_all => "Cannot load Text::CSV" if $@; require "t/util.pl"; } $| = 1; my @testlist = ( [ 1, "a", "\x01", "A" ], [ 2, "b", "\x02", "B" ], [ 3, "c", "\x03", "C" ], [ 4, "d", "\x04", "D" ], ); my @list; sub do_tests { my $sub = shift; $sub->(\@list); $sub->(\@list, 0); $sub->([@list[2,3]], 2); $sub->([], 0, 0); $sub->(\@list, 0, 10); $sub->([@list[0,1]], 0, 2); $sub->([@list[1,2]], 1, 2); $sub->([@list[1..3]], -3); $sub->([@list[1,2]], -3, 2); $sub->([@list[1..3]], -3, 3); } # do_tests foreach my $eol ("\n", "\r") { @list = @testlist; { ok (my $csv = Text::CSV->new ({ binary => 1, eol => $eol }), "csv out EOL "._readable ($eol)); open my $fh, ">", "_77test.csv" or die "_77test.csv: $!"; ok ($csv->print ($fh, $_), "write $_->[0]") for @list; close $fh; } { ok (my $csv = Text::CSV->new ({ binary => 1 }), "csv in"); do_tests (sub { my ($expect, @args) = @_; open my $fh, "<", "_77test.csv" or die "_77test.csv: $!"; my $s_args = join ", " => @args; is_deeply ($csv->getline_all ($fh, @args), $expect, "getline_all ($s_args)"); close $fh; }); } { ok (my $csv = Text::CSV->new ({ binary => 1 }), "csv in"); ok ($csv->column_names (my @cn = qw( foo bar bin baz )), "Set column names"); @list = map { my %h; @h{@cn} = @$_; \%h } @list; do_tests (sub { my ($expect, @args) = @_; open my $fh, "<", "_77test.csv" or die "_77test.csv: $!"; my $s_args = join ", " => @args; is_deeply ($csv->getline_hr_all ($fh, @args), $expect, "getline_hr_all ($s_args)"); close $fh; }); } { ok (my $csv = Text::CSV->new ({ binary => 1 }), "csv in"); open my $fh, "<", "_77test.csv" or die "_77test.csv: $!"; eval { my $row = $csv->getline_hr_all ($fh); }; is ($csv->error_diag () + 0, 3002, "Use _hr before colnames ()"); } unlink "_77test.csv"; } Text-CSV-1.33/t/46_eol_si.t0000644000175000017500000001230712475365663013744 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; use Config; use Test::More; BEGIN { unless (exists $Config{useperlio} && defined $Config{useperlio} && $] >= 5.008 && # perlio was experimental in 5.6.2, but not reliable $Config{useperlio} eq "define") { plan skip_all => "No reliable perlIO available"; } else { plan tests => 546; } } BEGIN { $ENV{PERL_TEXT_CSV} = 0; require_ok "Text::CSV"; plan skip_all => "Cannot load Text::CSV" if $@; require "t/util.pl"; } $| = 1; # Embedded newline tests my $file = ""; my $def_rs = $/; foreach my $rs ("\n", "\r\n", "\r") { for $\ (undef, $rs) { my $csv = Text::CSV->new ({ binary => 1 }); $csv->eol ($/ = $rs) unless defined $\; foreach my $pass (0, 1) { if ($pass == 0) { $file = ""; open FH, ">", \$file; } else { open FH, "<", \$file; } foreach my $eol ("", "\r", "\n", "\r\n", "\n\r") { my $s_eol = join " - ", map { defined $_ ? $_ : "" } $\, $rs, $eol; $s_eol =~ s/\r/\\r/g; $s_eol =~ s/\n/\\n/g; my @p; my @f = ("", 1, $eol, " $eol", "$eol ", " $eol ", "'$eol'", "\"$eol\"", " \" $eol \"\n ", "EOL"); if ($pass == 0) { ok ($csv->combine (@f), "combine |$s_eol|"); ok (my $str = $csv->string, "string |$s_eol|"); my $state = $csv->parse ($str); ok ($state, "parse |$s_eol|"); if ($state) { ok (@p = $csv->fields, "fields |$s_eol|"); } else{ is ($csv->error_input, $str, "error |$s_eol|"); } print FH $str; } else { ok (my $row = $csv->getline (*FH), "getline |$s_eol|"); is (ref $row, "ARRAY", "row |$s_eol|"); @p = @$row; } local $, = "|"; is_binary ("@p", "@f", "result |$s_eol|"); } close FH; } } } $/ = $def_rs; { my $csv = Text::CSV->new ({ escape_char => undef }); ok ($csv->parse (qq{"x"\r\n}), "Trailing \\r\\n with no escape char"); is ($csv->eol ("\r"), "\r", "eol set to \\r"); ok ($csv->parse (qq{"x"\r}), "Trailing \\r with no escape char"); ok ($csv->allow_whitespace (1), "Allow whitespace"); ok ($csv->parse (qq{"x" \r}), "Trailing \\r with no escape char"); } SKIP: { $] < 5.008 and skip "\$\\ tests don't work in perl 5.6.x and older", 2; { local $\ = "#\r\n"; my $csv = Text::CSV->new (); $file = ""; open FH, ">", \$file; $csv->print (*FH, [ "a", 1 ]); close FH; open FH, "<", \$file; local $/; is (, "a,1#\r\n", "Strange \$\\"); close FH; } { local $\ = "#\r\n"; my $csv = Text::CSV->new ({ eol => $\ }); $file = ""; open FH, ">", \$file; $csv->print (*FH, [ "a", 1 ]); close FH; open FH, "<", \$file; local $/; is (, "a,1#\r\n", "Strange \$\\ + eol"); close FH; } } $/ = $def_rs; ok (1, "Auto-detecting \\r"); { my @row = qw( a b c ); local $" = ","; for (["\n", "\\n"], ["\r\n", "\\r\\n"], ["\r", "\\r"]) { my ($eol, $s_eol) = @$_; $file = ""; open FH, ">", \$file; print FH qq{@row$eol@row$eol@row$eol\x91}; close FH; open FH, "<", \$file; my $c = Text::CSV->new ({ binary => 1, auto_diag => 1 }); is ($c->eol (), "", "default EOL"); is_deeply ($c->getline (*FH), [ @row ], "EOL 1 $s_eol"); is ($c->eol (), $eol eq "\r" ? "\r" : "", "EOL"); is_deeply ($c->getline (*FH), [ @row ], "EOL 2 $s_eol"); is_deeply ($c->getline (*FH), [ @row ], "EOL 3 $s_eol"); close FH; } } ok (1, "Specific \\r test from tfrayner"); { $/ = "\r"; $file = ""; open FH, ">", \$file; print FH qq{a,b,c$/}, qq{"d","e","f"$/}; close FH; open FH, "<", \$file; my $c = Text::CSV->new ({ eol => $/ }); my $row; local $" = " "; ok ($row = $c->getline (*FH), "getline 1"); is (scalar @$row, 3, "# fields"); is ("@$row", "a b c", "fields 1"); ok ($row = $c->getline (*FH), "getline 2"); is (scalar @$row, 3, "# fields"); is ("@$row", "d e f", "fields 2"); close FH; } $/ = $def_rs; ok (1, "EOL undef"); { $/ = "\r"; ok (my $csv = Text::CSV->new ({ eol => undef }), "new csv with eol => undef"); $file = ""; open FH, ">", \$file; ok ($csv->print (*FH, [1, 2, 3]), "print"); ok ($csv->print (*FH, [4, 5, 6]), "print"); close FH; open FH, "<", \$file; ok (my $row = $csv->getline (*FH), "getline 1"); is (scalar @$row, 5, "# fields"); is_deeply ($row, [ 1, 2, 34, 5, 6], "fields 1"); close FH; } $/ = $def_rs; foreach my $eol ("!", "!!", "!\n", "!\n!") { (my $s_eol = $eol) =~ s/\n/\\n/g; ok (1, "EOL $s_eol"); ok (my $csv = Text::CSV->new ({ eol => $eol }), "new csv with eol => $s_eol"); $file = ""; open FH, ">", \$file; ok ($csv->print (*FH, [1, 2, 3]), "print"); ok ($csv->print (*FH, [4, 5, 6]), "print"); close FH; foreach my $rs (undef, "", "\n", $eol, "!", "!\n", "\n!", "!\n!", "\n!\n") { local $/ = $rs; (my $s_rs = defined $rs ? $rs : "-- undef --") =~ s/\n/\\n/g; ok (1, "with RS $s_rs"); open FH, "<", \$file; ok (my $row = $csv->getline (*FH), "getline 1"); is (scalar @$row, 3, "# fields"); is_deeply ($row, [ 1, 2, 3], "fields 1"); ok ( $row = $csv->getline (*FH), "getline 2"); is (scalar @$row, 3, "# fields"); is_deeply ($row, [ 4, 5, 6], "fields 2"); close FH; } } $/ = $def_rs; 1; Text-CSV-1.33/t/80_diag.t0000644000175000017500000001104612475365663013373 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; use Test::More tests => 124; #use Test::More "no_plan"; my %err; BEGIN { $ENV{PERL_TEXT_CSV} = 0; require_ok "Text::CSV"; plan skip_all => "Cannot load Text::CSV" if $@; require "t/util.pl"; open PP, "< lib/Text/CSV_PP.pm" or die "Cannot read error messages from PP\n"; while () { m/^ ([0-9]{4}) => "([^"]+)"/ and $err{$1} = $2; } } $| = 1; my $csv = Text::CSV->new (); is (Text::CSV::error_diag() . '', "", "Last failure for new () - OK"); is_deeply ([ $csv->error_diag ], [ 0, "", 0], "OK in list context"); sub parse_err ($$$) { my ($n_err, $p_err, $str) = @_; my $s_err = $err{$n_err}; my $STR = _readable ($str); is ($csv->parse ($str), 0, "$n_err - Err for parse ('$STR')"); is ($csv->error_diag () + 0, $n_err, "$n_err - Diag in numerical context"); is ($csv->error_diag () . '', $s_err, "$n_err - Diag in string context"); my ($c_diag, $s_diag, $p_diag) = $csv->error_diag (); is ($c_diag, $n_err, "$n_err - Num diag in list context"); is ($s_diag, $s_err, "$n_err - Str diag in list context"); is ($p_diag, $p_err, "$n_err - Pos diag in list context"); } # parse_err # a difference between PP and XS parse_err 2027, 5, qq{2023,",2008-04-05,"Foo, Bar",\n}; # " $csv = Text::CSV->new ({ escape_char => "+", eol => "\n" }); is ( "" . $csv->error_diag (), "", "No errors yet"); # error pos are different from XS parse_err 2010, 3, qq{"x"\r}; parse_err 2011, 3, qq{"x"x}; parse_err 2021, 2, qq{"\n"}; parse_err 2022, 2, qq{"\r"}; parse_err 2025, 3, qq{"+ "}; parse_err 2026, 3, qq{"\0 "}; parse_err 2027, 1, '"'; parse_err 2031, 1, qq{\r }; parse_err 2032, 2, qq{ \r}; parse_err 2034, 2, qq{1, "bar",2}; parse_err 2037, 1, qq{\0 }; { my @warn; local $SIG{__WARN__} = sub { push @warn, @_ }; $csv->error_diag (); ok (@warn == 1, "Got error message"); like ($warn[0], qr{^# CSV_PP ERROR: 2037 - EIF}, "error content"); } is (Text::CSV->new ({ ecs_char => ":" }), undef, "Unsupported option"); { my @warn; local $SIG{__WARN__} = sub { push @warn, @_ }; Text::CSV::error_diag (); ok (@warn == 1, "Error_diag in void context ::"); like ($warn[0], qr{^# CSV_PP ERROR: 1000 - INI}, "error content"); } { my @warn; local $SIG{__WARN__} = sub { push @warn, @_ }; Text::CSV->error_diag (); ok (@warn == 1, "Error_diag in void context ->"); like ($warn[0], qr{^# CSV_PP ERROR: 1000 - INI}, "error content"); } { my @warn; local $SIG{__WARN__} = sub { push @warn, @_ }; is (Text::CSV->new ({ auto_diag => 0, ecs_char => ":" }), undef, "Unsupported option"); ok (@warn == 0, "Error_diag in from new ({ auto_diag => 0})"); } { my @warn; local $SIG{__WARN__} = sub { push @warn, @_ }; is (Text::CSV->new ({ auto_diag => 1, ecs_char => ":" }), undef, "Unsupported option"); ok (@warn == 1, "Error_diag in from new ({ auto_diag => 1})"); like ($warn[0], qr{^# CSV_PP ERROR: 1000 - INI}, "error content"); } is (Text::CSV::error_diag() . '', "INI - Unknown attribute 'ecs_char'", "Last failure for new () - FAIL"); is (Text::CSV->error_diag() . '', "INI - Unknown attribute 'ecs_char'", "Last failure for new () - FAIL"); is (Text::CSV::error_diag (bless {}, "Foo") . '', "INI - Unknown attribute 'ecs_char'", "Last failure for new () - FAIL"); $csv->SetDiag (0); is (0 + $csv->error_diag (), 0, "Reset error NUM"); is (''. $csv->error_diag (), "", "Reset error NUM"); ok (1, "Test auto_diag"); $csv = Text::CSV->new ({ auto_diag => 1 }); { my @warn; local $SIG{__WARN__} = sub { push @warn, @_ }; is ($csv->parse ('"","'), 0, "1 - bad parse"); ok (@warn == 1, "1 - One error"); like ($warn[0], qr '^# CSV_PP ERROR: 2027 -', "1 - error message"); } { ok ($csv->{auto_diag} = 2, "auto_diag = 2 to die"); eval { $csv->parse ('"","') }; like ($@, qr '^# CSV_PP ERROR: 2027 -', "2 - error message"); } SKIP: { skip "incompatible between PP and XS", 25; { my @warn; local $SIG{__WARN__} = sub { push @warn, @_ }; Text::CSV->new ()->_cache_diag (); ok (@warn == 1, "Got warn"); is ($warn[0], "CACHE: invalid\n", "Uninitialized cache"); } my $diag_file = "_$$.out"; open EH, ">&STDERR"; open STDERR, ">$diag_file"; ok ($csv->_cache_diag, "Cache debugging output"); close STDERR; open STDERR, ">&EH"; open EH, "<$diag_file"; is (scalar , "CACHE:\n", "Title"); while () { like ($_, qr{^ \w+\s+[0-9a-f]+:(?:".*"|\s*[0-9]+)$}, "Content"); } close EH; unlink $diag_file; } 1; Text-CSV-1.33/t/10_base.t0000644000175000017500000001004412475365663013367 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; # use warnings core since 5.6 use Test::More tests => 64; BEGIN { $ENV{PERL_TEXT_CSV} = 0; use_ok "Text::CSV"; plan skip_all => "Cannot load Text::CSV" if $@; } # empty subclass test # package Empty_Subclass; @Empty_Subclass::ISA = qw( Text::CSV ); package main; ok (new Text::CSV, "Indirect object notation"); # Important: Do not modify these tests unless you have a good # reason. This file ought to guarantee compatibility to Text::CSV. # my $empty = Empty_Subclass->new (); is (ref $empty, "Empty_Subclass", "Empty Subclass"); is ($empty->version (), Text::CSV->version (), "Version"); ok ($empty->parse (""), "Subclass parse ()"); ok ($empty->combine (""), "Subclass combine ()"); ok ($empty->new, "new () based on object"); my $csv; ok ($csv = Text::CSV->new, "new ()"); is ($csv->fields, undef, "fields () before parse ()"); is ($csv->string, undef, "string () undef before combine"); # Important: Do not modify these tests unless you have a good # reason. This file ought to guarantee compatibility to Text::CSV. # ok (1, "combine () & string () tests"); ok (!$csv->combine (), "Missing arguments"); ok (!$csv->combine ("abc", "def\n", "ghi"), "Bad character"); is ( $csv->error_input, "def\n", "Error_input ()"); ok ( $csv->combine (""), "Empty string - combine ()"); is ( $csv->string, '', "Empty string - string ()"); ok ( $csv->combine ("", " "), "Two fields, one space - combine ()"); is ( $csv->string, '," "', "Two fields, one space - string ()"); ok ( $csv->combine ("", 'I said, "Hi!"', ""), "Hi! - combine ()"); is ( $csv->string, ',"I said, ""Hi!""",', "Hi! - string ()"); ok ( $csv->combine ('"', "abc"), "abc - combine ()"); is ( $csv->string, '"""",abc', "abc - string ()"); ok ( $csv->combine (","), "comma - combine ()"); is ( $csv->string, '","', "comma - string ()"); ok ( $csv->combine ("abc", '"'), "abc + \" - combine ()"); is ( $csv->string, 'abc,""""', "abc + \" - string ()"); ok ( $csv->combine ("abc", "def", "ghi", "j,k"), "abc .. j,k - combine ()"); is ( $csv->string, 'abc,def,ghi,"j,k"', "abc .. j,k - string ()"); ok ( $csv->combine ("abc\tdef", "ghi"), "abc + TAB - combine ()"); is ( $csv->string, qq("abc\tdef",ghi), "abc + TAB - string ()"); ok (1, "parse () tests"); ok (!$csv->parse (), "Missing arguments"); ok ( $csv->parse ("\n"), "Single newline"); ok (!$csv->parse ('"abc'), "Missing closing \""); ok (!$csv->parse ('ab"c'), "\" outside of \"'s"); ok (!$csv->parse ('"ab"c"'), "Bad character sequence"); ok (!$csv->parse (qq("abc\nc")), "Bad character (NL)"); ok (!$csv->status (), "Wrong status ()"); ok ( $csv->parse ('","'), "comma - parse ()"); is ( scalar $csv->fields (), 1, "comma - fields () - count"); is (($csv->fields ())[0], ",", "comma - fields () - content"); ok ( $csv->parse (qq("","I said,\t""Hi!""","")), "Hi! - parse ()"); is ( scalar $csv->fields (), 3, "Hi! - fields () - count"); is (($csv->fields ())[0], "", "Hi! - fields () - field 1"); is (($csv->fields ())[1], qq(I said,\t"Hi!"), "Hi! - fields () - field 2"); is (($csv->fields ())[2], "", "Hi! - fields () - field 3"); ok ( $csv->status (), "status ()"); ok ( $csv->parse (""), "Empty line"); is ( scalar $csv->fields (), 1, "Empty - count"); is (($csv->fields ())[0], "", "One empty field"); # Are Integers and Reals quoted? # # Important: Do not modify these tests unless you have a good # reason. This file ought to guarantee compatibility to Text::CSV. # ok (1, "Integers and Reals"); ok ( $csv->combine ("", 2, 3.4, "a", "a b"), "Mixed - combine ()"); is ( $csv->string, ',2,3.4,a,"a b"', "Mixed - string ()"); # New from object ok ($csv->new (), "\$csv->new ()"); my $state; for ( [ 0, 0 ], [ 0, "foo" ], [ 0, {} ], [ 0, \0 ], [ 0, *STDOUT ], ) { eval { $state = $csv->print (@$_) }; ok (!$state, "print needs (IO, ARRAY_REF)"); ok ($@ =~ m/^Expected fields to be an array ref/, "Error msg"); } 1; Text-CSV-1.33/t/20_file.t0000644000175000017500000001042612475365663013401 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; # use warnings; use Test::More tests => 105; BEGIN { $ENV{PERL_TEXT_CSV} = 0; use_ok "Text::CSV"; plan skip_all => "Cannot load Text::CSV" if $@; require "t/util.pl"; } $| = 1; $/ = "\n"; $\ = undef; my $csv = Text::CSV->new (); my $UTF8 = ($ENV{LANG} || "C").($ENV{LC_ALL} || "C") =~ m/utf-?8/i ? 1 : 0; ok (!$csv->print (*FH, ["abc", "def\007", "ghi"]), "print bad character"); for ( [ 1, 1, 1, '""' ], [ 2, 1, 1, '', '' ], [ 3, 1, 0, '', 'I said, "Hi!"', '' ], [ 4, 1, 0, '"', 'abc' ], [ 5, 1, 0, 'abc', '"' ], [ 6, 1, 1, 'abc', 'def', 'ghi' ], [ 7, 1, 1, "abc\tdef", 'ghi' ], [ 8, 1, 0, '"abc' ], [ 9, 1, 0, 'ab"c' ], [ 10, 1, 0, '"ab"c"' ], [ 11, 0, 0, qq("abc\nc") ], [ 12, 1, 1, q(","), ',' ], [ 13, 1, 0, qq("","I said,\t""Hi!""",""), '', qq(I said,\t"Hi!"), '' ], ) { my ($tst, $validp, $validg, @arg, $row) = @$_; open FH, ">_20test.csv" or die "_20test.csv: $!"; is ($csv->print (*FH, \@arg), $validp||"", "$tst - print ()"); close FH; open FH, ">_20test.csv" or die "_20test.csv: $!"; print FH join ",", @arg; close FH; open FH, "<_20test.csv" or die "_20test.csv: $!"; $row = $csv->getline (*FH); unless ($validg) { is ($row, undef, "$tst - false getline ()"); next; } ok ($row, "$tst - good getline ()"); $tst == 12 and @arg = (",", "", ""); foreach my $a (0 .. $#arg) { (my $exp = $arg[$a]) =~ s/^"(.*)"$/$1/; is ($row->[$a], $exp, "$tst - field $a"); } } unlink "_20test.csv"; # This test because of a problem with DBD::CSV ok (1, "Tests for DBD::CSV"); open FH, ">_20test.csv" or die "_20test.csv: $!"; $csv->binary (1); $csv->eol ("\r\n"); ok ($csv->print (*FH, [ "id", "name" ]), "Bad character"); ok ($csv->print (*FH, [ 1, "Alligator Descartes" ]), "Name 1"); ok ($csv->print (*FH, [ "3", "Jochen Wiedmann" ]), "Name 2"); ok ($csv->print (*FH, [ 2, "Tim Bunce" ]), "Name 3"); ok ($csv->print (*FH, [ " 4", "Andreas König" ]), "Name 4"); ok ($csv->print (*FH, [ 5 ]), "Name 5"); close FH; my $expected = <<"CONTENTS"; id,name\015 1,"Alligator Descartes"\015 3,"Jochen Wiedmann"\015 2,"Tim Bunce"\015 " 4","Andreas König"\015 5\015 CONTENTS open FH, "<_20test.csv" or die "_20test.csv: $!"; my $content = do { local $/; }; close FH; is ($content, $expected, "Content"); open FH, ">_20test.csv" or die "_20test.csv: $!"; print FH $content; close FH; open FH, "<_20test.csv" or die "_20test.csv: $!"; my $fields; print "# Retrieving data\n"; for (0 .. 5) { ok ($fields = $csv->getline (*FH), "Fetch field $_"); is ($csv->eof, "", "EOF"); print "# Row $_: $fields (@$fields)\n"; } is ($csv->getline (*FH), undef, "Fetch field 6"); is ($csv->eof, 1, "EOF"); # Edge cases $csv = Text::CSV->new ({ escape_char => "+" }); for ([ 1, 1, 0, "\n" ], [ 2, 1, 0, "+\n" ], [ 3, 1, 0, "+" ], [ 4, 0, 2021, qq{"+"\n} ], [ 5, 0, 2025, qq{"+\n} ], [ 6, 0, 2011, qq{""+\n} ], [ 7, 0, 2027, qq{"+"} ], [ 8, 0, 2024, qq{"+} ], [ 9, 0, 2011, qq{""+} ], [ 10, 0, 2037, "\r" ], [ 11, 0, 2031, "\r\r" ], [ 12, 0, 2032, "+\r\r" ], [ 13, 0, 2032, "+\r\r+" ], [ 14, 0, 2022, qq{"\r"} ], [ 15, 0, 2022, qq{"\r\r" } ], [ 16, 0, 2022, qq{"\r\r"\t} ], [ 17, 0, 2025, qq{"+\r\r"} ], [ 18, 0, 2025, qq{"+\r\r+"} ], [ 19, 0, 2022, qq{"\r"\r} ], [ 20, 0, 2022, qq{"\r\r"\r} ], [ 21, 0, 2025, qq{"+\r\r"\r} ], [ 22, 0, 2025, qq{"+\r\r+"\r} ], ) { my ($tst, $valid, $err, $str) = @$_; open FH, ">_20test.csv" or die "_20test.csv: $!"; print FH $str; close FH; open FH, "<_20test.csv" or die "_20test.csv: $!"; my $row = $csv->getline (*FH); close FH; my @err = $csv->error_diag; my $sstr = _readable ($str); SKIP: { $tst == 10 && $] >= 5.008 && $] < 5.008003 && $UTF8 and skip "Be reasonable, this perl version does not do Unicode reliable", 2; ok ($valid ? $row : !$row, "$tst - getline ESC +, '$sstr'"); # is ($err[0], $err, "Error expected $err"); ok ($err[0] >= 0, "Error $err[0] but in CSV_XS $err" . ($err[0] == $err ? " * same in PP and XS *" : "") ); } } unlink "_20test.csv"; Text-CSV-1.33/t/util.pl0000644000175000017500000000075112475365663013306 0ustar reonreonuse strict; my %special = ( 9 => "\\t", 10 => "\\n", 13 => "\\r" ); sub _readable { defined $_[0] or return "--undef--"; join "", map { my $cp = ord $_; $cp >= 0x20 && $cp <= 0x7e ? $_ : $special{$cp} || sprintf "\\x{%02x}", $cp } split m//, $_[0]; } # _readable sub is_binary { my ($str, $exp, $tst) = @_; if ($str eq $exp) { ok (1, $tst); } else { my ($hs, $he) = map { _readable $_ } $str, $exp; is ($hs, $he, $tst); } } # is_binary 1; Text-CSV-1.33/t/22_scalario.t0000644000175000017500000001050312475365663014255 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; # use warnings; $| = 1; use Config; use Test::More; BEGIN { unless (exists $Config{useperlio} && defined $Config{useperlio} && $] >= 5.008 && # perlio was experimental in 5.6.2, but not reliable $Config{useperlio} eq "define") { plan skip_all => "No reliable perlIO available"; } else { plan tests => 105; } } BEGIN { $ENV{PERL_TEXT_CSV} = 0; use_ok "Text::CSV"; plan skip_all => "Cannot load Text::CSV" if $@; require "t/util.pl"; } $/ = "\n"; $\ = undef; my $io; my $io_str; my $csv = Text::CSV->new (); ok (!$csv->print ($io, ["abc", "def\007", "ghi"]), "print bad character"); for ( [ 1, 1, 1, '""' ], [ 2, 1, 1, '', '' ], [ 3, 1, 0, '', 'I said, "Hi!"', '' ], [ 4, 1, 0, '"', 'abc' ], [ 5, 1, 0, 'abc', '"' ], [ 6, 1, 1, 'abc', 'def', 'ghi' ], [ 7, 1, 1, "abc\tdef", 'ghi' ], [ 8, 1, 0, '"abc' ], [ 9, 1, 0, 'ab"c' ], [ 10, 1, 0, '"ab"c"' ], [ 11, 0, 0, qq("abc\nc") ], [ 12, 1, 1, q(","), ',' ], [ 13, 1, 0, qq("","I said,\t""Hi!""",""), '', qq(I said,\t"Hi!"), '' ], ) { my ($tst, $validp, $validg, @arg, $row) = @$_; open $io, ">", \$io_str or die "_22test.csv: $!"; is ($csv->print ($io, \@arg), $validp||"", "$tst - print ()"); close $io; open $io, ">", \$io_str or die "_22test.csv: $!"; print $io join ",", @arg; close $io; open $io, "<", \$io_str or die "_22test.csv: $!"; $row = $csv->getline ($io); unless ($validg) { is ($row, undef, "$tst - false getline ()"); next; } ok ($row, "$tst - good getline ()"); $tst == 12 and @arg = (",", "", ""); foreach my $a (0 .. $#arg) { (my $exp = $arg[$a]) =~ s/^"(.*)"$/$1/; is ($row->[$a], $exp, "$tst - field $a"); } } unlink "_22test.csv"; # This test because of a problem with DBD::CSV ok (1, "Tests for DBD::CSV"); open $io, ">", \$io_str or die "_22test.csv: $!"; $csv->binary (1); $csv->eol ("\r\n"); ok ($csv->print ($io, [ "id", "name" ]), "Bad character"); ok ($csv->print ($io, [ 1, "Alligator Descartes" ]), "Name 1"); ok ($csv->print ($io, [ "3", "Jochen Wiedmann" ]), "Name 2"); ok ($csv->print ($io, [ 2, "Tim Bunce" ]), "Name 3"); ok ($csv->print ($io, [ " 4", "Andreas König" ]), "Name 4"); ok ($csv->print ($io, [ 5 ]), "Name 5"); close $io; my $expected = <<"CONTENTS"; id,name\015 1,"Alligator Descartes"\015 3,"Jochen Wiedmann"\015 2,"Tim Bunce"\015 " 4","Andreas König"\015 5\015 CONTENTS open $io, "<", \$io_str or die "_22test.csv: $!"; my $content = do { local $/; <$io> }; close $io; is ($content, $expected, "Content"); open $io, ">", \$io_str or die "_22test.csv: $!"; print $io $content; close $io; open $io, "<", \$io_str or die "_22test.csv: $!"; my $fields; print "# Retrieving data\n"; for (0 .. 5) { ok ($fields = $csv->getline ($io), "Fetch field $_"); is ($csv->eof, "", "EOF"); print "# Row $_: $fields (@$fields)\n"; } is ($csv->getline ($io), undef, "Fetch field 6"); is ($csv->eof, 1, "EOF"); # Edge cases $csv = Text::CSV->new ({ escape_char => "+" }); for ([ 1, 1, 0, "\n" ], [ 2, 1, 0, "+\n" ], [ 3, 1, 0, "+" ], [ 4, 0, 2021, qq{"+"\n} ], [ 5, 0, 2025, qq{"+\n} ], [ 6, 0, 2011, qq{""+\n} ], [ 7, 0, 2027, qq{"+"} ], [ 8, 0, 2024, qq{"+} ], [ 9, 0, 2011, qq{""+} ], [ 10, 0, 2037, "\r" ], [ 11, 0, 2031, "\r\r" ], [ 12, 0, 2032, "+\r\r" ], [ 13, 0, 2032, "+\r\r+" ], [ 14, 0, 2022, qq{"\r"} ], [ 15, 0, 2022, qq{"\r\r" } ], [ 16, 0, 2022, qq{"\r\r"\t} ], [ 17, 0, 2025, qq{"+\r\r"} ], [ 18, 0, 2025, qq{"+\r\r+"} ], [ 19, 0, 2022, qq{"\r"\r} ], [ 20, 0, 2022, qq{"\r\r"\r} ], [ 21, 0, 2025, qq{"+\r\r"\r} ], [ 22, 0, 2025, qq{"+\r\r+"\r} ], ) { my ($tst, $valid, $err, $str) = @$_; $io_str = $str; open $io, "<", \$io_str or die "_22test.csv: $!"; my $row = $csv->getline ($io); close $io; my @err = $csv->error_diag; my $sstr = _readable ($str); ok ($valid ? $row : !$row, "$tst - getline ESC +, '$sstr'"); # is ($err[0], $err, "Error expected $err"); ok ($err[0] >= 0, "Error $err[0] but in CSV_XS $err" . ($err[0] == $err ? " * same in PP and XS *" : "") ); } Text-CSV-1.33/t/fields_containing_0.t0000644000175000017500000000443712475365663016064 0ustar reonreon#!/usr/bin/perl =head1 DESCRIPTION This is a test program that succeeds with Text::CSV_PP and fails with Text::CSV_XS. The Text::CSV_XS behaviour is the correct one. =head1 CREDITS AND LICENSE The sample data (now anonymised) and a test program were contributed by blue_sky on Freenode’s #perl channel as part of a problem report with Text::CSV_PP based on the Text::CSV documentation. License is open source and compatible with the license of Text::CSV. Converted into a test program by Shlomi Fish ( L ) , while disclaiming all explicit or implicit copyright ownership on the modifications. ==head1 MODIFICATION modified by makamaka for old perl. =cut #use warnings; $^W = 1; use strict; use Test::More tests => 4; my $FALSE = 0; # my $USE_XS = $ENV{'USE_TEXT_CSV_XS'}; my $USE_XS = $FALSE; use Text::CSV_PP; use Data::Dumper qw(Dumper); END { unlink '_fc0_test.csv'; } if ($USE_XS) { require Text::CSV_XS; } { my $csv_text = <<'EOF'; "DIVISION CODE", "DIVISION DESCRIPTION", "CUSTOMER CODE", "CUSTOMER NAME", "SHORT NAME", "ADDRESS LINE 1", "ADDRESS LINE 2", "ADDRESS LINE 3", "TOWN", "COUNTY", "POST CODE", "COUNTRY", "GRID REF", "TELEPHONE", "AGENT CODE", "YEAR TO DATE SALES" "1", "UK", "Lambda", "Gambda Noo", "Foo", "Quad", "Rectum", "", "Eingoon", "Land", "Simplex", "", "", "099 999", "", 0.00 EOF # open my $IF, "<", \$csv_text; my $IF; open $IF, ">_fc0_test.csv" or die "_fc0_test.csv: $!"; print $IF $csv_text; close $IF; open $IF, "<_fc0_test.csv" or die "_fc0_test.csv: $!"; my $csv = ($USE_XS ? "Text::CSV_XS" : "Text::CSV_PP")->new({ allow_whitespace => 1, allow_loose_escapes => 1, }) or die "Cannot use CSV: ".Text::CSV->error_diag(); $csv->column_names( $csv->getline($IF) ); { my $first_line = $csv->getline_hr($IF); # TEST is ($first_line->{'POST CODE'}, 'Simplex', "First line POST CODE" ); # TEST is ($first_line->{'COUNTRY'}, '', "First line COUNTRY", ); # TEST is ($first_line->{'GRID REF'}, '', "First line GRID REF", ); # TEST is ($first_line->{'TELEPHONE'}, '099 999', "First line TELEPHONE", ); } close($IF); } Text-CSV-1.33/t/40_misc.t0000644000175000017500000000326112475365663013416 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; use Test::More tests => 24; BEGIN { $ENV{PERL_TEXT_CSV} = 0; require_ok "Text::CSV"; plan skip_all => "Cannot load Text::CSV" if $@; require "t/util.pl"; } $| = 1; my @binField = ("abc\0def\n\rghi", "ab\"ce,\032\"'", "\377"); my $csv = Text::CSV->new ({ binary => 1 }); ok ($csv->combine (@binField), "combine ()"); my $string; is_binary ($string = $csv->string, qq("abc"0def\n\rghi","ab""ce,\032""'",\377), "string ()"); ok ($csv->parse ($string), "parse ()"); is ($csv->fields, scalar @binField, "field count"); my @field = $csv->fields (); for (0 .. $#binField) { is ($field[$_], $binField[$_], "Field $_"); } ok (1, "eol \\r\\n"); $csv->eol ("\r\n"); ok ($csv->combine (@binField), "combine ()"); is_binary ($csv->string, qq("abc"0def\n\rghi","ab""ce,\032""'",\377\r\n), "string ()"); ok (1, "eol \\n"); $csv->eol ("\n"); ok ($csv->combine (@binField), "combine ()"); is_binary ($csv->string, qq("abc"0def\n\rghi","ab""ce,\032""'",\377\n), "string ()"); ok (1, "eol ,xxxxxxx\\n"); $csv->eol (",xxxxxxx\n"); ok ($csv->combine (@binField), "combine ()"); is_binary ($csv->string, qq("abc"0def\n\rghi","ab""ce,\032""'",\377,xxxxxxx\n), "string ()"); $csv->eol ("\n"); ok (1, "quote_char undef"); $csv->quote_char (undef); ok ($csv->combine ("abc","def","ghi"), "combine"); is ($csv->string, "abc,def,ghi\n", "string ()"); # Ken's test ok (1, "always_quote"); my $csv2 = Text::CSV->new ({ always_quote => 1 }); ok ($csv2, "new ()"); ok ($csv2->combine ("abc","def","ghi"), "combine ()"); is ($csv2->string, '"abc","def","ghi"', "string ()"); Text-CSV-1.33/t/21_lexicalio.t0000644000175000017500000001070012475365663014427 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; # use warnings; use Test::More; BEGIN { if ($] < 5.006) { plan skip_all => "No lexical file handles in in this ancient perl version"; } else { plan tests => 105; } } BEGIN { $ENV{PERL_TEXT_CSV} = 0; use_ok "Text::CSV"; plan skip_all => "Cannot load Text::CSV" if $@; require "t/util.pl"; } $| = 1; $/ = "\n"; $\ = undef; my $io; my $csv = Text::CSV->new (); my $UTF8 = ($ENV{LANG} || "C").($ENV{LC_ALL} || "C") =~ m/utf-?8/i ? 1 : 0; ok (!$csv->print ($io, ["abc", "def\007", "ghi"]), "print bad character"); for ( [ 1, 1, 1, '""' ], [ 2, 1, 1, '', '' ], [ 3, 1, 0, '', 'I said, "Hi!"', '' ], [ 4, 1, 0, '"', 'abc' ], [ 5, 1, 0, 'abc', '"' ], [ 6, 1, 1, 'abc', 'def', 'ghi' ], [ 7, 1, 1, "abc\tdef", 'ghi' ], [ 8, 1, 0, '"abc' ], [ 9, 1, 0, 'ab"c' ], [ 10, 1, 0, '"ab"c"' ], [ 11, 0, 0, qq("abc\nc") ], [ 12, 1, 1, q(","), ',' ], [ 13, 1, 0, qq("","I said,\t""Hi!""",""), '', qq(I said,\t"Hi!"), '' ], ) { my ($tst, $validp, $validg, @arg, $row) = @$_; open $io, ">_21test.csv" or die "_21test.csv: $!"; is ($csv->print ($io, \@arg), $validp||"", "$tst - print ()"); close $io; open $io, ">_21test.csv" or die "_21test.csv: $!"; print $io join ",", @arg; close $io; open $io, "<_21test.csv" or die "_21test.csv: $!"; $row = $csv->getline ($io); unless ($validg) { is ($row, undef, "$tst - false getline ()"); next; } ok ($row, "$tst - good getline ()"); $tst == 12 and @arg = (",", "", ""); foreach my $a (0 .. $#arg) { (my $exp = $arg[$a]) =~ s/^"(.*)"$/$1/; is ($row->[$a], $exp, "$tst - field $a"); } } unlink "_21test.csv"; # This test because of a problem with DBD::CSV ok (1, "Tests for DBD::CSV"); open $io, ">_21test.csv" or die "_21test.csv: $!"; $csv->binary (1); $csv->eol ("\r\n"); ok ($csv->print ($io, [ "id", "name" ]), "Bad character"); ok ($csv->print ($io, [ 1, "Alligator Descartes" ]), "Name 1"); ok ($csv->print ($io, [ "3", "Jochen Wiedmann" ]), "Name 2"); ok ($csv->print ($io, [ 2, "Tim Bunce" ]), "Name 3"); ok ($csv->print ($io, [ " 4", "Andreas König" ]), "Name 4"); ok ($csv->print ($io, [ 5 ]), "Name 5"); close $io; my $expected = <<"CONTENTS"; id,name\015 1,"Alligator Descartes"\015 3,"Jochen Wiedmann"\015 2,"Tim Bunce"\015 " 4","Andreas König"\015 5\015 CONTENTS open $io, "<_21test.csv" or die "_21test.csv: $!"; my $content = do { local $/; <$io> }; close $io; is ($content, $expected, "Content"); open $io, ">_21test.csv" or die "_21test.csv: $!"; print $io $content; close $io; open $io, "<_21test.csv" or die "_21test.csv: $!"; my $fields; print "# Retrieving data\n"; for (0 .. 5) { ok ($fields = $csv->getline ($io), "Fetch field $_"); is ($csv->eof, "", "EOF"); print "# Row $_: $fields (@$fields)\n"; } is ($csv->getline ($io), undef, "Fetch field 6"); is ($csv->eof, 1, "EOF"); # Edge cases $csv = Text::CSV->new ({ escape_char => "+" }); for ([ 1, 1, 0, "\n" ], [ 2, 1, 0, "+\n" ], [ 3, 1, 0, "+" ], [ 4, 0, 2021, qq{"+"\n} ], [ 5, 0, 2025, qq{"+\n} ], [ 6, 0, 2011, qq{""+\n} ], [ 7, 0, 2027, qq{"+"} ], [ 8, 0, 2024, qq{"+} ], [ 9, 0, 2011, qq{""+} ], [ 10, 0, 2037, "\r" ], [ 11, 0, 2031, "\r\r" ], [ 12, 0, 2032, "+\r\r" ], [ 13, 0, 2032, "+\r\r+" ], [ 14, 0, 2022, qq{"\r"} ], [ 15, 0, 2022, qq{"\r\r" } ], [ 16, 0, 2022, qq{"\r\r"\t} ], [ 17, 0, 2025, qq{"+\r\r"} ], [ 18, 0, 2025, qq{"+\r\r+"} ], [ 19, 0, 2022, qq{"\r"\r} ], [ 20, 0, 2022, qq{"\r\r"\r} ], [ 21, 0, 2025, qq{"+\r\r"\r} ], [ 22, 0, 2025, qq{"+\r\r+"\r} ], ) { my ($tst, $valid, $err, $str) = @$_; open my $io, ">_21test.csv" or die "_21test.csv: $!"; print $io $str; close $io; open $io, "<_21test.csv" or die "_21test.csv: $!"; my $row = $csv->getline ($io); my @err = $csv->error_diag; my $sstr = _readable ($str); SKIP: { $tst == 10 && $] >= 5.008 && $] < 5.008003 && $UTF8 and skip "Be reasonable, this perl version does not do Unicode reliable", 2; ok ($valid ? $row : !$row, "$tst - getline ESC +, '$sstr'"); # is ($err[0], $err, "Error expected $err"); ok ($err[0] >= 0, "Error $err[0] but in CSV_XS $err" . ($err[0] == $err ? " * same in PP and XS *" : "") ); } } unlink "_21test.csv"; Text-CSV-1.33/t/75_hashref.t0000644000175000017500000001155412475365663014117 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; #use Test::More "no_plan"; use Test::More tests => 75; BEGIN { $ENV{PERL_TEXT_CSV} = 0; use_ok "Text::CSV", (); plan skip_all => "Cannot load Text::CSV" if $@; } open FH, ">_75test.csv"; print FH <new (), "new"); is ($csv->column_names, undef, "No headers yet"); foreach my $args ([\1], ["foo", \1], [{ 1 => 2 }]) { eval { $csv->column_names (@$args) }; like ($@, qr/^EHR/, "croak"); is ($csv->error_diag () + 0, 3001, "Bad args to column_names"); } ok ($csv->column_names ("name"), "One single name"); is ($csv->column_names (undef), undef, "reset column_names"); eval { $csv->column_names (\undef) }; is ($csv->error_diag () + 0, 3001, "No hash please"); eval { $csv->column_names ({ 1 => 2 }) }; is ($csv->error_diag () + 0, 3001, "No hash please"); my $hr; eval { $hr = $csv->getline_hr (*FH) }; is ($hr, undef, "getline_hr before column_names"); like ($@, qr/^EHR/, "croak"); is ($csv->error_diag () + 0, 3002, "error code"); ok ($csv->column_names ("name", "code"), "column_names (list)"); is_deeply ([ $csv->column_names ], [ "name", "code" ], "well set"); open FH, "<_75test.csv"; my $row; ok ($row = $csv->getline (*FH), "getline headers"); is ($row->[0], "code", "Header line"); ok ($csv->column_names ($row), "column_names from array_ref"); is_deeply ([ $csv->column_names ], [ @$row ], "Keys set"); while (my $hr = $csv->getline_hr (*FH)) { ok (exists $hr->{code}, "Line has a code field"); like ($hr->{code}, qr/^[0-9]+$/, "Code is numeric"); ok (exists $hr->{name}, "Line has a name field"); like ($hr->{name}, qr/^[A-Z][a-z]+$/, "Name"); } close FH; my ($code, $name, $price, $desc) = (1..4); is ($csv->bind_columns (), undef, "No bound columns yet"); eval { $csv->bind_columns (\$code) }; is ($csv->error_diag () + 0, 3003, "Arg cound mismatch"); eval { $csv->bind_columns ({}, {}, {}, {}) }; is ($csv->error_diag () + 0, 3004, "bad arg types"); is ($csv->column_names (undef), undef, "reset column_names"); ok ($csv->bind_columns (\($code, $name, $price)), "Bind columns"); eval { $csv->column_names ("foo") }; is ($csv->error_diag () + 0, 3003, "Arg cound mismatch"); $csv->bind_columns (undef); eval { $csv->bind_columns ([undef]) }; is ($csv->error_diag () + 0, 3004, "legal header defenition"); my @bcr = \($code, $name, $price, $desc); open FH, "<_75test.csv"; ok ($row = $csv->getline (*FH), "getline headers"); ok ($csv->bind_columns (@bcr), "Bind columns"); ok ($csv->column_names ($row), "column_names from array_ref"); is_deeply ([ $csv->column_names ], [ @$row ], "Keys set"); $row = $csv->getline (*FH); is_deeply ([ $csv->bind_columns ], [ @bcr ], "check refs"); is_deeply ($row, [], "return from getline with bind_columns"); is ($csv->column_names (undef), undef, "reset column headers"); is ($csv->bind_columns (undef), undef, "reset bound columns"); my $foo; ok ($csv->bind_columns (@bcr, \$foo), "bind too many columns"); ($code, $name, $price, $desc, $foo) = (101 .. 105); ok ($csv->getline (*FH), "fetch less than expected"); is_deeply ([ $code, $name, $price, $desc, $foo ], [ 2, "Drinks", "82.78", "Drinks", 105 ], "unfetched not reset"); my @foo = (0) x 0x012345; ok ($csv->bind_columns (\(@foo)), "bind a lot of columns"); ok ($csv->bind_columns (\1, \2, \3, \""), "bind too constant columns"); is ($csv->getline (*FH), undef, "fetch to read-only ref"); is ($csv->error_diag () + 0, 3008, "Read-only"); ok ($csv->bind_columns (\$code), "bind not enough columns"); eval { $row = $csv->getline (*FH) }; is ($csv->error_diag () + 0, 3006, "cannot read all fields"); close FH; open FH, "<_75test.csv"; is ($csv->column_names (undef), undef, "reset column headers"); is ($csv->bind_columns (undef), undef, "reset bound columns"); is_deeply ([ $csv->column_names (undef, "", "name", "name") ], [ "\cAUNDEF\cA", "", "name", "name" ], "undefined column header"); ok ($hr = $csv->getline_hr (*FH), "getline_hr ()"); is (ref $hr, "HASH", "returned a hashref"); is_deeply ($hr, { "\cAUNDEF\cA" => "code", "" => "name", "name" => "description" }, "Discarded 3rd field"); close FH; open FH, ">_75test.csv"; $hr = { c_foo => 1, foo => "poison", zebra => "Of course" }; is ($csv->column_names (undef), undef, "reset column headers"); ok ($csv->column_names (sort keys %$hr), "set column names"); ok ($csv->eol ("\n"), "set eol for output"); ok ($csv->print (*FH, [ $csv->column_names ]), "print header"); ok ($csv->print_hr (*FH, $hr), "print_hr"); close FH; open FH, "<_75test.csv"; ok ($csv->column_names ($csv->getline (*FH)), "get column names"); is_deeply ($csv->getline_hr (*FH), $hr, "compare to written hr"); close FH; unlink "_75test.csv"; Text-CSV-1.33/t/50_utf8.t0000644000175000017500000001024612475365663013353 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; use Test::More; BEGIN { if ($] < 5.008001) { plan skip_all => "UTF8 tests useless in this ancient perl version"; } else { plan tests => 91; } } BEGIN { $ENV{PERL_TEXT_CSV} = 0; require_ok "Text::CSV"; plan skip_all => "Cannot load Text::CSV" if $@; require "t/util.pl"; } # No binary => 1, as UTF8 is supposed to be allowed without it my $csv = Text::CSV->new ({ always_quote => 1, keep_meta_info => 1, }); # Special characters to check: # 0A = \n 2C = , 20 = 22 = " # 0D = \r 3B = ; foreach my $test ( # Space-like characters [ "\x{0000A0}", "U+0000A0 NO-BREAK SPACE" ], [ "\x{00200B}", "U+00200B ZERO WIDTH SPACE" ], # Some characters with possible problems in the code point [ "\x{000122}", "U+000122 LATIN CAPITAL LETTER G WITH CEDILLA" ], [ "\x{002C22}", "U+002C22 GLAGOLITIC CAPITAL LETTER SPIDERY HA" ], [ "\x{000A2C}", "U+000A2C GURMUKHI LETTER BA" ], [ "\x{000E2C}", "U+000E2C THAI CHARACTER LO CHULA" ], [ "\x{010A2C}", "U+010A2C KHAROSHTHI LETTER VA" ], # Characters with possible problems in the encoded representation # Should not be possible. ASCII is coded in 000..127, all other # characters in 128..255 ) { my ($u, $msg) = @$test; ($u = "$u\x{0123}") =~ s/.$//; # Make sure it's marked UTF8 my @in = ("", " ", $u, ""); my $exp = join ",", map { qq{"$_"} } @in; ok ($csv->combine (@in), "combine $msg"); my $str = $csv->string; is_binary ($str, $exp, "string $msg"); ok ($csv->parse ($str), "parse $msg"); my @out = $csv->fields; # Cannot use is_deeply (), because of the binary content is (scalar @in, scalar @out, "fields $msg"); is_binary ($in[$_], $out[$_], "field $_ $msg") for 0 .. $#in; } # Test if the UTF8 part is accepted, but the \n is not is ($csv->parse (qq{"\x{0123}\n\x{20ac}"}), 0, "\\n still needs binary"); is ($csv->binary, 0, "bin flag still unset"); is ($csv->error_diag + 0, 2021, "Error 2021"); my $file = "files/utf8.csv"; SKIP: { open my $fh, "<:encoding(utf8)", $file or skip "Cannot open UTF-8 test file", 6; my $row; ok ($row = $csv->getline ($fh), "read/parse"); is ($csv->is_quoted (0), 1, "First field is quoted"); is ($csv->is_quoted (1), 0, "Second field is not quoted"); is ($csv->is_binary (0), 1, "First field is binary"); is ($csv->is_binary (1), 0, "Second field is not binary"); ok (utf8::valid ($row->[0]), "First field is valid utf8"); $csv->combine (@$row); ok (utf8::valid ($csv->string), "Combined string is valid utf8"); } # Test quote_binary $csv->always_quote (0); $csv->quote_space (0); $csv->quote_binary (0); ok ($csv->combine (" ", 1, "\x{20ac} "), "Combine"); is ($csv->string, qq{ ,1,\x{20ac} }, "String 0-0"); $csv->quote_binary (1); ok ($csv->combine (" ", 1, "\x{20ac} "), "Combine"); is ($csv->string, qq{ ,1,"\x{20ac} "}, "String 0-1"); $csv->quote_space (1); $csv->quote_binary (0); ok ($csv->combine (" ", 1, "\x{20ac} "), "Combine"); is ($csv->string, qq{" ",1,"\x{20ac} "}, "String 1-0"); ok ($csv->quote_binary (1), "quote binary on"); ok ($csv->combine (" ", 1, "\x{20ac} "), "Combine"); is ($csv->string, qq{" ",1,"\x{20ac} "}, "String 1-1"); open my $fh, ">:encoding(utf-8)", "_50test.csv"; print $fh "euro\n\x{20ac}\neuro\n"; close $fh; open $fh, "<:encoding(utf-8)", "_50test.csv"; SKIP: { my $out = ""; my $isutf8 = $] < 5.008001 ? sub { !$_[0]; } : # utf8::is_utf8 () not available in 5.8.0 sub { utf8::is_utf8 ($out); }; ok ($csv->auto_diag (1), "auto diag"); ok ($csv->binary (1), "set binary"); ok ($csv->bind_columns (\$out), "bind"); ok ($csv->getline ($fh), "parse"); is ($csv->is_binary (0), 0, "not binary"); is ($out, "euro", "euro"); ok (!$isutf8->(1), "not utf8"); ok ($csv->getline ($fh), "parse"); is ($csv->is_binary (0), 1, "is binary"); is ($out, "\x{20ac}", "euro"); ok ($isutf8->(0), "is utf8"); ok ($csv->getline ($fh), "parse"); is ($csv->is_binary (0), 0, "not binary"); is ($out, "euro", "euro"); ok (!$isutf8->(1), "not utf8"); close $fh; unlink "_50test.csv"; } Text-CSV-1.33/t/30_types.t0000644000175000017500000000321512475365663013625 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; use Test::More tests => 25; BEGIN { $ENV{PERL_TEXT_CSV} = 0; use_ok "Text::CSV", (); plan skip_all => "Cannot load Text::CSV" if $@; } $| = 1; my $csv = Text::CSV->new ({ types => [ Text::CSV::IV (), Text::CSV::PV (), Text::CSV::NV (), ], }); ok ($csv, "CSV_XS->new ()"); is (@{$csv->{types}}, 3, "->{types} as hash"); is ($csv->{types}[0], Text::CSV::IV (), "type IV"); is ($csv->{types}[1], Text::CSV::PV (), "type PV"); is ($csv->{types}[2], Text::CSV::NV (), "type NV"); is (ref ($csv->types), "ARRAY", "->types () as method"); is ($csv->types ()->[0], Text::CSV::IV (), "type IV"); is ($csv->types ()->[1], Text::CSV::PV (), "type PV"); is ($csv->types ()->[2], Text::CSV::NV (), "type NV"); is (length $csv->{_types}, 3, "->{_types}"); my $inp = join "", map { chr $_ } Text::CSV::IV (), Text::CSV::PV (), Text::CSV::NV (); # should be "\001\000\002" is ($csv->{_types}, $inp, "IV PV NV"); ok ($csv->parse ("2.55,CSFDATVM01,3.77"), "parse ()"); my @fields = $csv->fields (); is ($fields[0], "2", "Field 1"); is ($fields[1], "CSFDATVM01", "Field 2"); is ($fields[2], "3.77", "Field 3"); ok ($csv->combine ("", "", "1.00"), "combine ()"); is ($csv->string, ',,1.00', "string"); my $warning; $SIG{__WARN__} = sub { $warning = shift }; ok ($csv->parse ($csv->string ()), "parse (combine ())"); like ($warning, qr/numeric/, "numeric warning"); @fields = $csv->fields (); is ($fields[0], "0", "Field 1"); is ($fields[1], "", "Field 2"); is ($fields[2], "1", "Field 3"); is ($csv->types (0), undef, "delete types"); is ($csv->types, undef, "types gone"); Text-CSV-1.33/t/rt99774.t0000644000175000017500000000174212475365663013233 0ustar reonreon $^W = 1; use strict; use Test::More tests => 8; BEGIN { $ENV{PERL_TEXT_CSV} = 0; require_ok "Text::CSV"; plan skip_all => "Cannot load Text::CSV" if $@; require "t/util.pl"; } my $csv = Text::CSV->new ( { binary => 1, sep_char => ';', allow_whitespace => 1, quote_char => '"' } ); # https://rt.cpan.org/Public/Bug/Display.html?id=99774 while ( my $line = ) { my $text = $line; chomp($text); $text =~ s/"//g; my $expect = [ split/;/, $text ]; $csv->parse($line); is_deeply( [$csv->fields], $expect, $line ); } # https://rt.cpan.org/Public/Bug/Display.html?id=92509 for my $allow_whitespace ( 0, 1 ) { $csv = Text::CSV_PP->new ( { allow_whitespace => $allow_whitespace } ); $csv->parse(q{"value1","0","value3"}); is_deeply( [$csv->fields], ["value1","0","value3"], 'allow_whitespace:' . $allow_whitespace ); } __DATA__ "data_quality_id";"language_version_id";"name" "0";"2";"0%" "10";"2";"33%" "20";"2";"66%" "30";"2";"100%" Text-CSV-1.33/t/51_utf8.t0000644000175000017500000001077212475365663013360 0ustar reonreon#!/usr/bin/perl use strict; $^W = 1; use Test::More; BEGIN { $] < 5.008001 and plan skip_all => "UTF8 tests useless in this ancient perl version"; } my @tests; BEGIN { delete $ENV{PERLIO}; my $euro_ch = "\x{20ac}"; utf8::encode (my $bytes = $euro_ch); utf8::downgrade (my $bytes_dn = $bytes); utf8::upgrade (my $bytes_up = $bytes); @tests = ( # $test $perlio $data, $encoding $expect_w # ---------------------------- ------------------- ----------- --------- ---------- [ "Unicode default", "", $euro_ch, "utf8", "warn", ], [ "Unicode binmode", "[binmode]", $euro_ch, "utf8", "warn", ], [ "Unicode :utf8", ":utf8", $euro_ch, "utf8", "no warn", ], [ "Unicode :encoding(utf8)", ":encoding(utf8)", $euro_ch, "utf8", "no warn", ], [ "Unicode :encoding(UTF-8)", ":encoding(UTF-8)", $euro_ch, "utf8", "no warn", ], [ "bytes dn default", "", $bytes_dn, "[none]", "no warn", ], [ "bytes dn binmode", "[binmode]", $bytes_dn, "[none]", "no warn", ], [ "bytes dn :utf8", ":utf8", $bytes_dn, "utf8", "no warn", ], [ "bytes dn :encoding(utf8)", ":encoding(utf8)", $bytes_dn, "utf8", "no warn", ], [ "bytes dn :encoding(UTF-8)", ":encoding(UTF-8)", $bytes_dn, "utf8", "no warn", ], [ "bytes up default", "", $bytes_up, "[none]", "no warn", ], [ "bytes up binmode", "[binmode]", $bytes_up, "[none]", "no warn", ], [ "bytes up :utf8", ":utf8", $bytes_up, "utf8", "no warn", ], [ "bytes up :encoding(utf8)", ":encoding(utf8)", $bytes_up, "utf8", "no warn", ], [ "bytes up :encoding(UTF-8)", ":encoding(UTF-8)", $bytes_up, "utf8", "no warn", ], ); plan tests => 11 + 6 * @tests; } BEGIN { require_ok "Text::CSV_PP"; plan skip_all => "Cannot load Text::CSV_PP" if $@; require "t/util.pl"; } sub hexify { join " ", map { sprintf "%02x", $_ } unpack "C*", @_ } sub warned { length ($_[0]) ? "warn" : "no warn" } my $csv = Text::CSV_PP->new ({ auto_diag => 1, binary => 1 }); for (@tests) { my ($test, $perlio, $data, $enc, $expect_w) = @$_; my $expect = qq{"$data"}; $enc eq "utf8" and utf8::encode ($expect); my ($p_out, $p_fh) = (""); my ($c_out, $c_fh) = (""); if ($perlio eq "[binmode]") { open $p_fh, ">", \$p_out; binmode $p_fh; open $c_fh, ">", \$c_out; binmode $c_fh; } else { open $p_fh, ">$perlio", \$p_out; open $c_fh, ">$perlio", \$c_out; } my $p_warn = ""; { local $SIG{__WARN__} = sub { $p_warn .= join "", @_ }; ok ((print $p_fh qq{"$data"}), "$test perl print"); close $p_fh; } my $c_warn = ""; { local $SIG{__WARN__} = sub { $c_warn .= join "", @_ }; ok ($csv->print ($c_fh, [ $data ]), "$test csv print"); close $c_fh; } is (hexify ($c_out), hexify ($p_out), "$test against Perl"); is (hexify ($c_out), hexify ($expect), "$test against expected"); is (warned ($c_warn), warned ($p_warn), "$test against Perl warning"); is (warned ($c_warn), $expect_w, "$test against expected warning"); } # Test automatic upgrades for valid UTF-8 { my $blob = pack "C*", 0..255; $blob =~ tr/",//d; # perl-5.10.x has buggy SvCUR () on blob $] >= 5.010000 && $] <= 5.012001 and $blob =~ tr/\0//d; my @data = ( qq[1,aap,3], # No diac qq[1,a\x{e1}p,3], # a_ACUTE in ISO-8859-1 qq[1,a\x{c4}\x{83}p,3], # a_BREVE in UTF-8 qq[1,"$blob",3], # Binary shit ) x 2; my $data = join "\n" => @data; my @expect = ("aap", "a\341p", "a\x{0103}p", $blob) x 2; my $csv = Text::CSV_PP->new ({ binary => 1, auto_diag => 1 }); foreach my $bc (undef, 3) { my @read; # Using getline () open my $fh, "<", \$data; $bc and $csv->bind_columns (\my ($f1, $f2, $f3)); is (scalar $csv->bind_columns, $bc, "Columns_bound?"); while (my $row = $csv->getline ($fh)) { push @read, $bc ? $f2 : $row->[1]; } close $fh; is_deeply (\@read, \@expect, "Set and reset UTF-8 ".($bc?"no bind":"bind_columns")); is_deeply ([ map { utf8::is_utf8 ($_) } @read ], [ "", "", 1, "", "", "", 1, "" ], "UTF8 flags"); # Using parse () @read = map { $csv->parse ($_); $bc ? $f2 : ($csv->fields)[1]; } @data; is_deeply (\@read, \@expect, "Set and reset UTF-8 ".($bc?"no bind":"bind_columns")); is_deeply ([ map { utf8::is_utf8 ($_) } @read ], [ "", "", 1, "", "", "", 1, "" ], "UTF8 flags"); } } Text-CSV-1.33/META.json0000664000175000017500000000211612475371020013131 0ustar reonreon{ "abstract" : "comma-separated values manipulator (using XS or PurePerl)", "author" : [ "Makamaka Hannyaharamitu, Emakamaka[at]cpan.orgE" ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 6.98, CPAN::Meta::Converter version 2.140640", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Text-CSV", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "IO::Handle" : "0", "Test::Harness" : "0", "Test::More" : "0" } } }, "release_status" : "stable", "resources" : { "repository" : { "url" : "http://github.com/makamaka/Text-CSV" } }, "version" : "1.33" }