.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.14 .\" .\" Standard preamble: .\" ======================================================================== .de Sh \" Subsection heading .br .if t .Sp .ne 5 .PP \fB\\$1\fR .PP .. .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. | will give a .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' .\" expand to `' in nroff, nothing in troff, for use with C<>. .tr \(*W-|\(bv\*(Tr .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .\" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .hy 0 .if n .na .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "CSV_XS 3" .TH CSV_XS 3 "2010-02-15" "perl v5.8.4" "User Contributed Perl Documentation" .SH "NAME" Text::CSV_XS \- comma\-separated values manipulation routines .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Text::CSV_XS; .Ve .PP .Vb 10 \& my @rows; \& my $csv = Text::CSV_XS->new ({ binary => 1 }) 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; .Ve .PP .Vb 4 \& $csv->eol ("\er\en"); \& open $fh, ">:encoding(utf8)", "new.csv" or die "new.csv: $!"; \& $csv->print ($fh, $_) for @rows; \& close $fh or die "new.csv: $!"; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Text::CSV_XS provides facilities for the composition and decomposition of comma-separated values. An instance of the Text::CSV_XS class can combine fields into a \s-1CSV\s0 string and parse a \s-1CSV\s0 string into fields. .PP 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 \s-1ASV\s0 (anything separated values) rather than just \s-1CSV\s0. .Sh "Embedded newlines" .IX Subsection "Embedded newlines" \&\fBImportant Note\fR: 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 \f(CW\*(C`binary => 1\*(C'\fR in the call to \f(CW\*(C`new ()\*(C'\fR. To cover the widest range of parsing options, you will always want to set binary. .PP But you still have the problem that you have to pass a correct line to the \&\f(CW\*(C`parse ()\*(C'\fR method, which is more complicated from the usual point of usage: .PP .Vb 4 \& my $csv = Text::CSV_XS->new ({ binary => 1, eol => $/ }); \& while (<>) { # WRONG! \& $csv->parse ($_); \& my @fields = $csv->fields (); .Ve .PP will break, as the while might read broken lines, as that doesn't care about the quoting. If you need to support embedded newlines, the way to go is either .PP .Vb 3 \& my $csv = Text::CSV_XS->new ({ binary => 1, eol => $/ }); \& while (my $row = $csv->getline (*ARGV)) { \& my @fields = @$row; .Ve .PP or, more safely in perl 5.6 and up .PP .Vb 4 \& my $csv = Text::CSV_XS->new ({ binary => 1, eol => $/ }); \& open my $io, "<", $file or die "$file: $!"; \& while (my $row = $csv->getline ($io)) { \& my @fields = @$row; .Ve .Sh "Unicode (\s-1UTF8\s0)" .IX Subsection "Unicode (UTF8)" On parsing (both for \f(CW\*(C`getline ()\*(C'\fR and \f(CW\*(C`parse ()\*(C'\fR), if the source is marked being \s-1UTF8\s0, then all fields that are marked binary will also be be marked \s-1UTF8\s0. .PP On combining (\f(CW\*(C`print ()\*(C'\fR and \f(CW\*(C`combine ()\*(C'\fR), if any of the combining fields was marked \s-1UTF8\s0, the resulting string will be marked \s-1UTF8\s0. .PP For complete control over encoding, please use Text::CSV::Encoded: .PP .Vb 5 \& 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 \& }); .Ve .PP .Vb 3 \& $csv = Text::CSV::Encoded->new ({ encoding => "utf8" }); \& # combine () and print () accept *literally* utf8 encoded data \& # parse () and getline () return *literally* utf8 encoded data .Ve .PP .Vb 3 \& $csv = Text::CSV::Encoded->new ({ encoding => undef }); # default \& # combine () and print () accept UTF8 marked data \& # parse () and getline () return UTF8 marked data .Ve .SH "SPECIFICATION" .IX Header "SPECIFICATION" While no formal specification for \s-1CSV\s0 exists, \s-1RFC\s0 4180 1) describes a common format and establishes \*(L"text/csv\*(R" as the \s-1MIME\s0 type registered with the \s-1IANA\s0. .PP Many informal documents exist that describe the \s-1CSV\s0 format. How To: The Comma Separated Value (\s-1CSV\s0) File Format 2) provides an overview of the \s-1CSV\s0 format in the most widely used applications and explains how it can best be used and supported. .PP .Vb 2 \& 1) http://tools.ietf.org/html/rfc4180 \& 2) http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm .Ve .PP The basic rules are as follows: .PP \&\fB\s-1CSV\s0\fR is a delimited data format that has fields/columns separated by the comma character and records/rows separated by newlines. Fields that contain a special character (comma, newline, or double quote), must be enclosed in double quotes. However, if a line contains a single entry which is the empty string, it may be enclosed in double quotes. If a field's value contains a double quote character it is escaped by placing another double quote character next to it. The \s-1CSV\s0 file format does not require a specific character encoding, byte order, or line terminator format. .IP "\(bu" 2 Each record is one line terminated by a line feed (ASCII/LF=0x0A) or a carriage return and line feed pair (ASCII/CRLF=0x0D 0x0A), however, line-breaks can be embedded. .IP "\(bu" 2 Fields are separated by commas. .IP "\(bu" 2 Allowable characters within a \s-1CSV\s0 field include 0x09 (tab) and the inclusive range of 0x20 (space) through 0x7E (tilde). In binary mode all characters are accepted, at least in quoted fields. .IP "\(bu" 2 A field within \s-1CSV\s0 must be surrounded by double-quotes to contain a the separator character (comma). .PP Though this is the most clear and restrictive definition, Text::CSV_XS is way more liberal than this, and allows extension: .IP "\(bu" 2 Line termination by a single carriage return is accepted by default .IP "\(bu" 2 The separation\-, escape\-, and escape\- characters can be any \s-1ASCII\s0 character in the range from 0x20 (space) to 0x7E (tilde). Characters outside this range may or may not work as expected. Multibyte characters, like U+060c (\s-1ARABIC\s0 \&\s-1COMMA\s0), U+FF0C (\s-1FULLWIDTH\s0 \s-1COMMA\s0), U+241B (\s-1SYMBOL\s0 \s-1FOR\s0 \s-1ESCAPE\s0), U+2424 (\s-1SYMBOL\s0 \&\s-1FOR\s0 \s-1NEWLINE\s0), U+FF02 (\s-1FULLWIDTH\s0 \s-1QUOTATION\s0 \s-1MARK\s0), and U+201C (\s-1LEFT\s0 \s-1DOUBLE\s0 \&\s-1QUOTATION\s0 \s-1MARK\s0) (to give some examples of what might look promising) are therefor not allowed. .Sp If you use perl\-5.8.2 or higher, these three attributes are utf8\-decoded, to increase the likelihood of success. This way U+00FE will be allowed as a quote character. .IP "\(bu" 2 A field within \s-1CSV\s0 must be surrounded by double-quotes to contain an embedded double\-quote, represented by a pair of consecutive double\-quotes. In binary mode you may additionally use the sequence \f(CW\*(C`"0\*(C'\fR for representation of a \&\s-1NULL\s0 byte. .IP "\(bu" 2 Several violations of the above specification may be allowed by passing options to the object creator. .SH "FUNCTIONS" .IX Header "FUNCTIONS" .Sh "version ()" .IX Subsection "version ()" (Class method) Returns the current module version. .Sh "new (\e%attr)" .IX Subsection "new (%attr)" (Class method) Returns a new instance of Text::CSV_XS. The objects attributes are described by the (optional) hash ref \f(CW\*(C`\e%attr\*(C'\fR. Currently the following attributes are available: .IP "eol" 4 .IX Item "eol" An end-of-line string to add to rows. \f(CW\*(C`undef\*(C'\fR is replaced with an empty string. The default is \f(CW\*(C`$\e\*(C'\fR. Common values for \f(CW\*(C`eol\*(C'\fR are \&\f(CW"\e012"\fR (Line Feed) or \f(CW"\e015\e012"\fR (Carriage Return, Line Feed). Cannot be longer than 7 (\s-1ASCII\s0) characters. .Sp If both \f(CW$/\fR and \f(CW\*(C`eol\*(C'\fR equal \f(CW"\e015"\fR, parsing lines that end on only a Carriage Return without Line Feed, will be \f(CW\*(C`parse\*(C'\fRd correct. Line endings, whether in \f(CW$/\fR or \f(CW\*(C`eol\*(C'\fR, other than \f(CW\*(C`undef\*(C'\fR, \&\f(CW"\en"\fR, \f(CW"\er\en"\fR, or \f(CW"\er"\fR are not (yet) supported for parsing. .IP "sep_char" 4 .IX Item "sep_char" The char used for separating fields, by default a comma. (\f(CW\*(C`,\*(C'\fR). Limited to a single-byte character, usually in the range from 0x20 (space) to 0x7e (tilde). .Sp The separation character can not be equal to the quote character. The separation character can not be equal to the escape character. .Sp See also \s-1CAVEATS\s0 .IP "allow_whitespace" 4 .IX Item "allow_whitespace" When this option is set to true, whitespace (\s-1TAB\s0's and \s-1SPACE\s0's) surrounding the separation character is removed when parsing. If either \s-1TAB\s0 or \s-1SPACE\s0 is one of the three major characters \f(CW\*(C`sep_char\*(C'\fR, \&\f(CW\*(C`quote_char\*(C'\fR, or \f(CW\*(C`escape_char\*(C'\fR it will not be considered whitespace. .Sp So lines like: .Sp .Vb 1 \& 1 , "foo" , bar , 3 , zapp .Ve .Sp are now correctly parsed, even though it violates the \s-1CSV\s0 specs. .Sp Note that \fBall\fR whitespace is stripped from start and end of each field. That would make it more a \fIfeature\fR than a way to be able to parse bad \s-1CSV\s0 lines, as .Sp .Vb 1 \& 1, 2.0, 3, ape , monkey .Ve .Sp will now be parsed as .Sp .Vb 1 \& ("1", "2.0", "3", "ape", "monkey") .Ve .Sp even if the original line was perfectly sane \s-1CSV\s0. .IP "blank_is_undef" 4 .IX Item "blank_is_undef" Under normal circumstances, \s-1CSV\s0 data makes no distinction between quoted\- and unquoted empty fields. They both end up in an empty string field once read, so .Sp .Vb 1 \& 1,"",," ",2 .Ve .Sp is read as .Sp .Vb 1 \& ("1", "", "", " ", "2") .Ve .Sp When \fIwriting\fR \s-1CSV\s0 files with \f(CW\*(C`always_quote\*(C'\fR set, the unquoted empty field is the result of an undefined value. To make it possible to also make this distinction when reading \s-1CSV\s0 data, the \f(CW\*(C`blank_is_undef\*(C'\fR option will cause unquoted empty fields to be set to undef, causing the above to be parsed as .Sp .Vb 1 \& ("1", "", undef, " ", "2") .Ve .IP "empty_is_undef" 4 .IX Item "empty_is_undef" Going one step further than \f(CW\*(C`blank_is_undef\*(C'\fR, this attribute converts all empty fields to undef, so .Sp .Vb 1 \& 1,"",," ",2 .Ve .Sp is read as .Sp .Vb 1 \& (1, undef, undef, " ", 2) .Ve .Sp Note that this only effects fields that are \fIrealy\fR empty, not fields that are empty after stripping allowed whitespace. \s-1YMMV\s0. .IP "quote_char" 4 .IX Item "quote_char" The char used for quoting fields containing blanks, by default the double quote character (\f(CW\*(C`"\*(C'\fR). 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). .Sp The quote character can not be equal to the separation character. .IP "allow_loose_quotes" 4 .IX Item "allow_loose_quotes" By default, parsing fields that have \f(CW\*(C`quote_char\*(C'\fR characters inside an unquoted field, like .Sp .Vb 1 \& 1,foo "bar" baz,42 .Ve .Sp 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. .Sp In case there is \fBreally\fR bad \s-1CSV\s0 data, like .Sp .Vb 1 \& 1,"foo "bar" baz",42 .Ve .Sp or .Sp .Vb 1 \& 1,""foo bar baz"",42 .Ve .Sp there is a way to get that parsed, and leave the quotes inside the quoted field as\-is. This can be achieved by setting \f(CW\*(C`allow_loose_quotes\*(C'\fR \fB\s-1AND\s0\fR making sure that the \f(CW\*(C`escape_char\*(C'\fR is \fInot\fR equal to \f(CW\*(C`quote_char\*(C'\fR. .IP "escape_char" 4 .IX 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). .Sp The \f(CW\*(C`escape_char\*(C'\fR defaults to being the literal double-quote mark (\f(CW\*(C`"\*(C'\fR) in other words, the same as the default \f(CW\*(C`quote_char\*(C'\fR. This means that doubling the quote mark in a field escapes it: .Sp .Vb 1 \& "foo","bar","Escape ""quote mark"" with two ""quote marks""","baz" .Ve .Sp 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. .Sp The escape character can not be equal to the separation character. .IP "allow_loose_escapes" 4 .IX Item "allow_loose_escapes" By default, parsing fields that have \f(CW\*(C`escape_char\*(C'\fR characters that escape characters that do not need to be escaped, like: .Sp .Vb 2 \& my $csv = Text::CSV_XS->new ({ escape_char => "\e\e" }); \& $csv->parse (qq{1,"my bar\e's",baz,42}); .Ve .Sp 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. .IP "binary" 4 .IX Item "binary" If this attribute is \s-1TRUE\s0, you may use binary characters in quoted fields, including line feeds, carriage returns and \s-1NULL\s0 bytes. (The latter must be escaped as \f(CW\*(C`"0\*(C'\fR.) By default this feature is off. .Sp If a string is marked \s-1UTF8\s0, binary will be turned on automatically when binary characters other than \s-1CR\s0 or \s-1NL\s0 are encountered. Note that a simple string like \f(CW"\ex{00a0}"\fR might still be binary, but not marked \s-1UTF8\s0, so setting \f(CW\*(C`{ binary =\*(C'\fR 1 }> is still a wise option. .IP "types" 4 .IX Item "types" A set of column types; this attribute is immediately passed to the \&\fItypes\fR method below. You must not set this attribute otherwise, except for using the \fItypes\fR method. For details see the description of the \fItypes\fR method below. .IP "always_quote" 4 .IX 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 \s-1TRUE\s0 value, then all fields will be quoted. This is typically easier to handle in external applications. (Poor creatures who aren't using Text::CSV_XS. :\-) .IP "quote_space" 4 .IX Item "quote_space" By default, a space in a field would trigger quotation. As no rule exists this to be forced in \s-1CSV\s0, 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. .IP "keep_meta_info" 4 .IX 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 \f(CW\*(C`meta_info ()\*(C'\fR, \f(CW\*(C`is_quoted ()\*(C'\fR, and \f(CW\*(C`is_binary ()\*(C'\fR described below. Default is false. .IP "verbatim" 4 .IX Item "verbatim" This is a quite controversial attribute to set, but it makes hard things possible. .Sp The basic thought behind this is to tell the parser that the normally special characters newline (\s-1NL\s0) and Carriage Return (\s-1CR\s0) 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. .Sp When \f(CW\*(C`verbatim\*(C'\fR is used with \f(CW\*(C`getline ()\*(C'\fR, \f(CW\*(C`getline ()\*(C'\fR auto\-chomp's every line. .Sp Imagine a file format like .Sp .Vb 1 \& M^^Hans^Janssen^Klas 2\en2A^Ja^11-06-2007#\er\en .Ve .Sp where, the line ending is a very specific \*(L"#\er\en\*(R", 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. .Sp By default, Text::CSV_XS' parse function however is instructed to only know about \*(L"\en\*(R" and \*(L"\er\*(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 \en is just nothing more than a binary character. .Sp For parse () this means that the parser has no idea about line ending anymore, and getline () chomps line endings on reading. .IP "auto_diag" 4 .IX Item "auto_diag" Set to true will cause \f(CW\*(C`error_diag ()\*(C'\fR to be automatically be called in void context upon errors. .Sp In case of error \f(CW\*(C`2012 \- EOF\*(C'\fR), this call will be void. .Sp If set to a value greater than 1, it will die on errors instead of warn. .Sp Future extensions to this feature will include more reliable auto-detection of the \f(CW\*(C`autodie\*(C'\fR module being enabled, which will raise the value of \&\f(CW\*(C`auto_diag\*(C'\fR with \f(CW1\fR on the moment the error is detected. .PP To sum it up, .PP .Vb 1 \& $csv = Text::CSV_XS->new (); .Ve .PP is equivalent to .PP .Vb 17 \& $csv = Text::CSV_XS->new ({ \& quote_char => '"', \& escape_char => '"', \& sep_char => ',', \& eol => $\e, \& always_quote => 0, \& quote_space => 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, \& }); .Ve .PP 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 .PP .Vb 2 \& my $quote = $csv->quote_char; \& $csv->binary (1); .Ve .PP It is unwise to change these settings halfway through writing \s-1CSV\s0 data to a stream. If however, you want to create a new stream using the available \s-1CSV\s0 object, there is no harm in changing them. .PP If the \f(CW\*(C`new ()\*(C'\fR constructor call fails, it returns \f(CW\*(C`undef\*(C'\fR, and makes the fail reason available through the \f(CW\*(C`error_diag ()\*(C'\fR method. .PP .Vb 2 \& $csv = Text::CSV_XS->new ({ ecs_char => 1 }) or \& die "".Text::CSV_XS->error_diag (); .Ve .PP \&\f(CW\*(C`error_diag ()\*(C'\fR will return a string like .PP .Vb 1 \& "INI - Unknown attribute 'ecs_char'" .Ve .Sh "print" .IX Subsection "print" .Vb 1 \& $status = $csv->print ($io, $colref); .Ve .PP Similar to \f(CW\*(C`combine () + string () + print\*(C'\fR, but more efficient. It expects an array ref as input (not an array!) and the resulting string is not really created, but immediately written to the \fI$io\fR object, typically an \s-1IO\s0 handle or any other object that offers a \fIprint\fR method. Note, this implies that the following is wrong in perl 5.005_xx and older: .PP .Vb 2 \& open FILE, ">", "whatever"; \& $status = $csv->print (\e*FILE, $colref); .Ve .PP as in perl 5.005 and older, the glob \f(CW\*(C`\e*FILE\*(C'\fR 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 IO::File and IO::Wrap for details. .PP For performance reasons the print method doesn't create a result string. In particular the \fI$csv\->string ()\fR, \fI$csv\->status ()\fR, \&\fI$csv\-\fRfields ()> and \fI$csv\->error_input ()\fR methods are meaningless after executing this method. .Sh "combine" .IX Subsection "combine" .Vb 1 \& $status = $csv->combine (@columns); .Ve .PP This object function constructs a \s-1CSV\s0 string from the arguments, returning success or failure. Failure can result from lack of arguments or an argument containing an invalid character. Upon success, \f(CW\*(C`string ()\*(C'\fR can be called to retrieve the resultant \s-1CSV\s0 string. Upon failure, the value returned by \&\f(CW\*(C`string ()\*(C'\fR is undefined and \f(CW\*(C`error_input ()\*(C'\fR can be called to retrieve an invalid argument. .Sh "string" .IX Subsection "string" .Vb 1 \& $line = $csv->string (); .Ve .PP This object function returns the input to \f(CW\*(C`parse ()\*(C'\fR or the resultant \s-1CSV\s0 string of \f(CW\*(C`combine ()\*(C'\fR, whichever was called more recently. .Sh "getline" .IX Subsection "getline" .Vb 1 \& $colref = $csv->getline ($io); .Ve .PP This is the counterpart to print, like parse is the counterpart to combine: It reads a row from the \s-1IO\s0 object \f(CW$io\fR using \f(CW$io\fR\->getline () and parses this row into an array ref. This array ref is returned by the function or undef for failure. .PP When fields are bound with \f(CW\*(C`bind_columns ()\*(C'\fR, the return value is a reference to an empty list. .PP The \fI$csv\->string ()\fR, \fI$csv\->fields ()\fR and \fI$csv\->status ()\fR methods are meaningless, again. .Sh "parse" .IX Subsection "parse" .Vb 1 \& $status = $csv->parse ($line); .Ve .PP This object function decomposes a \s-1CSV\s0 string into fields, returning success or failure. Failure can result from a lack of argument or the given \s-1CSV\s0 string is improperly formatted. Upon success, \f(CW\*(C`fields ()\*(C'\fR can be called to retrieve the decomposed fields . Upon failure, the value returned by \f(CW\*(C`fields ()\*(C'\fR is undefined and \f(CW\*(C`error_input ()\*(C'\fR can be called to retrieve the invalid argument. .PP You may use the \fItypes ()\fR method for setting column types. See the description below. .Sh "getline_hr" .IX Subsection "getline_hr" The \f(CW\*(C`getline_hr ()\*(C'\fR and \f(CW\*(C`column_names ()\*(C'\fR methods work together to allow you to have rows returned as hashrefs. You must call \f(CW\*(C`column_names ()\*(C'\fR first to declare your column names. .PP .Vb 3 \& $csv->column_names (qw( code name price description )); \& $hr = $csv->getline_hr ($io); \& print "Price for $hr->{name} is $hr->{price} EUR\en"; .Ve .PP \&\f(CW\*(C`getline_hr ()\*(C'\fR will croak if called before \f(CW\*(C`column_names ()\*(C'\fR. .Sh "column_names" .IX Subsection "column_names" Set the keys that will be used in the \f(CW\*(C`getline_hr ()\*(C'\fR calls. If no keys (column names) are passed, it'll return the current setting. .PP \&\f(CW\*(C`column_names ()\*(C'\fR accepts a list of scalars (the column names) or a single array_ref, so you can pass \f(CW\*(C`getline ()\*(C'\fR .PP .Vb 1 \& $csv->column_names ($csv->getline ($io)); .Ve .PP \&\f(CW\*(C`column_names ()\*(C'\fR does \fBno\fR checking on duplicates at all, which might lead to unwanted results. Undefined entries will be replaced with the string \f(CW"\ecAUNDEF\ecA"\fR, so .PP .Vb 2 \& $csv->column_names (undef, "", "name", "name"); \& $hr = $csv->getline_hr ($io); .Ve .PP Will set \f(CW\*(C`$hr\-\*(C'\fR{\*(L"\ecAUNDEF\ecA\*(R"}> to the 1st field, \f(CW\*(C`$hr\-\*(C'\fR{""}> to the 2nd field, and \f(CW\*(C`$hr\-\*(C'\fR{name}> to the 4th field, discarding the 3rd field. .PP \&\f(CW\*(C`column_names ()\*(C'\fR croaks on invalid arguments. .Sh "bind_columns" .IX Subsection "bind_columns" Takes a list of references to scalars to store the fields fetched \&\f(CW\*(C`getline ()\*(C'\fR in. When you don't pass enough references to store the fetched fields in, \f(CW\*(C`getline ()\*(C'\fR will fail. If you pass more than there are fields to return, the remaining references are left untouched. .PP .Vb 4 \& $csv->bind_columns (\e$code, \e$name, \e$price, \e$description); \& while ($csv->getline ($io)) { \& print "The price of a $name is \ex{20ac} $price\en"; \& } .Ve .Sh "eof" .IX Subsection "eof" .Vb 1 \& $eof = $csv->eof (); .Ve .PP If \f(CW\*(C`parse ()\*(C'\fR or \f(CW\*(C`getline ()\*(C'\fR was used with an \s-1IO\s0 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. .Sh "types" .IX Subsection "types" .Vb 1 \& $csv->types (\e@tref); .Ve .PP 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 .PP .Vb 4 \& $csv->types ([Text::CSV_XS::IV (), \& Text::CSV_XS::NV (), \& Text::CSV_XS::NV (), \& Text::CSV_XS::PV ()]); .Ve .PP Column types are used only for decoding columns, in other words by the \fIparse ()\fR and \fIgetline ()\fR methods. .PP You can unset column types by doing a .PP .Vb 1 \& $csv->types (undef); .Ve .PP or fetch the current type settings with .PP .Vb 1 \& $types = $csv->types (); .Ve .IP "\s-1IV\s0" 4 .IX Item "IV" Set field type to integer. .IP "\s-1NV\s0" 4 .IX Item "NV" Set field type to numeric/float. .IP "\s-1PV\s0" 4 .IX Item "PV" Set field type to string. .Sh "fields" .IX Subsection "fields" .Vb 1 \& @columns = $csv->fields (); .Ve .PP This object function returns the input to \f(CW\*(C`combine ()\*(C'\fR or the resultant decomposed fields of C successful , whichever was called more recently. .PP Note that the return value is undefined after using \f(CW\*(C`getline ()\*(C'\fR, which does not fill the data structures returned by \f(CW\*(C`parse ()\*(C'\fR. .Sh "meta_info" .IX Subsection "meta_info" .Vb 1 \& @flags = $csv->meta_info (); .Ve .PP This object function returns the flags of the input to \f(CW\*(C`combine ()\*(C'\fR or the flags of the resultant decomposed fields of \f(CW\*(C`parse ()\*(C'\fR, whichever was called more recently. .PP For each field, a meta_info field will hold flags that tell something about the field returned by the \f(CW\*(C`fields ()\*(C'\fR method or passed to the \f(CW\*(C`combine ()\*(C'\fR method. The flags are bitwise\-or'd like: .IP "0x0001" 4 .IX Item "0x0001" The field was quoted. .IP "0x0002" 4 .IX Item "0x0002" The field was binary. .PP See the \f(CW\*(C`is_*** ()\*(C'\fR methods below. .Sh "is_quoted" .IX Subsection "is_quoted" .Vb 1 \& my $quoted = $csv->is_quoted ($column_idx); .Ve .PP Where \f(CW$column_idx\fR is the (zero\-based) index of the column in the last result of \f(CW\*(C`parse ()\*(C'\fR. .PP This returns a true value if the data in the indicated column was enclosed in \f(CW\*(C`quote_char\*(C'\fR quotes. This might be important for data where \f(CW\*(C`,20070108,\*(C'\fR is to be treated as a numeric value, and where \&\f(CW\*(C`,"20070108",\*(C'\fR is explicitly marked as character string data. .Sh "is_binary" .IX Subsection "is_binary" .Vb 1 \& my $binary = $csv->is_binary ($column_idx); .Ve .PP Where \f(CW$column_idx\fR is the (zero\-based) index of the column in the last result of \f(CW\*(C`parse ()\*(C'\fR. .PP This returns a true value if the data in the indicated column contained any byte in the range [\ex00\-\ex08,\ex10\-\ex1F,\ex7F\-\exFF] .Sh "status" .IX Subsection "status" .Vb 1 \& $status = $csv->status (); .Ve .PP This object function returns success (or failure) of \f(CW\*(C`combine ()\*(C'\fR or \&\f(CW\*(C`parse ()\*(C'\fR, whichever was called more recently. .Sh "error_input" .IX Subsection "error_input" .Vb 1 \& $bad_argument = $csv->error_input (); .Ve .PP This object function returns the erroneous argument (if it exists) of \&\f(CW\*(C`combine ()\*(C'\fR or \f(CW\*(C`parse ()\*(C'\fR, whichever was called more recently. .Sh "error_diag" .IX Subsection "error_diag" .Vb 5 \& Text::CSV_XS->error_diag (); \& $csv->error_diag (); \& $error_code = 0 + $csv->error_diag (); \& $error_str = "" . $csv->error_diag (); \& ($cde, $str, $pos) = $csv->error_diag (); .Ve .PP If (and only if) an error occured, this function returns the diagnostics of that error. .PP If called in void context, it will print the internal error code and the associated error message to \s-1STDERR\s0. .PP 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 a best guess at the location within the line that was being parsed. It's value is 1\-based. See \f(CW\*(C`examples/csv\-check\*(C'\fR for how this can be used. .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. .PP When called as a class method or a direct function call, the error diag is that of the last \f(CW\*(C`new ()\*(C'\fR call. .Sh "SetDiag" .IX Subsection "SetDiag" .Vb 1 \& $csv->SetDiag (0); .Ve .PP Use to reset the diagnostics if you are dealing with errors. .SH "INTERNALS" .IX Header "INTERNALS" .IP "Combine (...)" 4 .IX Item "Combine (...)" .PD 0 .IP "Parse (...)" 4 .IX Item "Parse (...)" .PD .PP The arguments to these two internal functions are deliberately not described or documented to enable the module author(s) to change it when they feel the need for it and using them is highly discouraged as the \s-1API\s0 may change in future releases. .SH "EXAMPLES" .IX Header "EXAMPLES" Reading a \s-1CSV\s0 file line by line: .PP .Vb 7 \& my $csv = Text::CSV_XS->new ({ binary => 1 }); \& open my $fh, "<", "file.csv" or die "file.csv: $!"; \& while (my $row = $csv->getline ($fh)) { \& # do something with @$row \& } \& $csv->eof or $csv->error_diag; \& close $fh or die "file.csv: $!"; .Ve .PP Parsing \s-1CSV\s0 strings: .PP .Vb 1 \& my $csv = Text::CSV_XS->new ({ keep_meta_info => 1, binary => 1 }); .Ve .PP .Vb 14 \& my $sample_input_string = \& qq{"I said, ""Hi!""",Yes,"",2.34,,"1.09","\ex{20ac}",}; \& if ($csv->parse ($sample_input_string)) { \& my @field = $csv->fields; \& foreach my $col (0 .. $#field) { \& my $quo = $csv->is_quoted ($col) ? $csv->{quote_char} : ""; \& printf "%2d: %s%s%s\en", $col, $quo, $field[$col], $quo; \& } \& } \& else { \& print STDERR "parse () failed on argument: ", \& $csv->error_input, "\en"; \& $csv->error_diag (); \& } .Ve .PP An example for creating \s-1CSV\s0 files using the \f(CW\*(C`print ()\*(C'\fR method, like in dumping the content of a database ($dbh) table ($tbl) to \s-1CSV:\s0 .PP .Vb 9 \& my $csv = Text::CSV_XS->new ({ binary => 1, eol => $/ }); \& open my $fh, ">", "$tbl.csv" or die "$tbl.csv: $!"; \& my $sth = $dbh->prepare ("select * from $tbl"); \& $sth->execute; \& $csv->print ($fh, $sth->{NAME_lc}); \& while (my $row = $sth->fetch) { \& $csv->print ($fh, $row) or $csv->error_diag; \& } \& close $fh or die "$tbl.csv: $!"; .Ve .PP or using the slower \f(CW\*(C`combine ()\*(C'\fR and \f(CW\*(C`string ()\*(C'\fR methods: .PP .Vb 1 \& my $csv = Text::CSV_XS->new; .Ve .PP .Vb 1 \& open my $csv_fh, ">", "hello.csv" or die "hello.csv: $!"; .Ve .PP .Vb 11 \& my @sample_input_fields = ( \& 'You said, "Hello!"', 5.67, \& '"Surely"', '', '3.14159'); \& if ($csv->combine (@sample_input_fields)) { \& print $csv_fh $csv->string, "\en"; \& } \& else { \& print "combine () failed on argument: ", \& $csv->error_input, "\en"; \& } \& close $csv_fh or die "hello.csv: $!"; .Ve .PP For more extended examples, see the \f(CW\*(C`examples/\*(C'\fR subdirectory in the original distribution or the git repository at http://repo.or.cz/w/Text\-CSV_XS.git?a=tree;f=examples. The following files can be found there: .IP "parser\-xs.pl" 2 .IX Item "parser-xs.pl" This can be used as a boilerplate to `fix' bad \s-1CSV\s0 and parse beyond errors. .Sp .Vb 1 \& $ perl examples/parser-xs.pl bad.csv >good.csv .Ve .IP "csv-check" 2 .IX Item "csv-check" This is a command-line tool that uses parser\-xs.pl techniques to check the \&\s-1CSV\s0 file and report on its content. .Sp .Vb 4 \& $ csv-check files/utf8.csv \& Checked with examples/csv-check 1.2 using Text::CSV_XS 0.61 \& OK: rows: 1, columns: 2 \& sep = <,>, quo = <">, bin = <1> .Ve .IP "csv2xls" 2 .IX Item "csv2xls" A script to convert \s-1CSV\s0 to Microsoft Excel. This requires Date::Calc and Spreadsheet::WriteExcel. The converter accepts various options and can produce \s-1UTF\-8\s0 Excel files. .IP "csvdiff" 2 .IX Item "csvdiff" A script that provides colorized diff on sorted \s-1CSV\s0 files, assuming first line is header and first field is the key. Output options include colorized \&\s-1ANSI\s0 escape codes or \s-1HTML\s0. .Sp .Vb 1 \& $ csvdiff --html --output=diff.html file1.csv file2.csv .Ve .SH "CAVEATS" .IX Header "CAVEATS" \&\f(CW\*(C`Text::CSV_XS\*(C'\fR is not designed to detect the characters used for field separation and quoting. The parsing is done using predefined settings. In the examples subdirectory, you can find scripts that demonstrate how you can try to detect these characters yourself. .Sh "Microsoft Excel" .IX Subsection "Microsoft Excel" The import/export from Microsoft Excel is a \fIrisky task\fR, according to the documentation in \f(CW\*(C`Text::CSV::Separator\*(C'\fR. Microsoft uses the system's default list separator defined in the regional settings, which happens to be a semicolon for Dutch, German and Spanish (and probably some others as well). For the English locale, the default is a comma. In Windows however, the user is free to choose a predefined locale, and then change every individual setting in it, so checking the locale is no solution. .SH "TODO" .IX Header "TODO" .IP "More Errors & Warnings" 2 .IX Item "More Errors & Warnings" New extensions ought to be clear and concise in reporting what error occurred where and why, and possibly also tell a remedy to the problem. error_diag is a (very) good start, but there is more work to be done here. .Sp Basic calls should croak or warn on illegal parameters. Errors should be documented. .IP "setting meta info" 2 .IX Item "setting meta info" Future extensions might include extending the \f(CW\*(C`meta_info ()\*(C'\fR, \&\f(CW\*(C`is_quoted ()\*(C'\fR, and \f(CW\*(C`is_binary ()\*(C'\fR to accept setting these flags for fields, so you can specify which fields are quoted in the combine ()/string () combination. .Sp .Vb 2 \& $csv->meta_info (0, 1, 1, 3, 0, 0); \& $csv->is_quoted (3, 1); .Ve .IP "combined methods" 2 .IX Item "combined methods" Requests for adding means (methods) that combine \f(CW\*(C`combine ()\*(C'\fR and \&\f(CW\*(C`string ()\*(C'\fR in a single call will \fBnot\fR be honored. Likewise for \&\f(CW\*(C`parse ()\*(C'\fR and \f(CW\*(C`fields ()\*(C'\fR. Given the trouble with embedded newlines, Using \f(CW\*(C`getline ()\*(C'\fR and \f(CW\*(C`print ()\*(C'\fR instead is the prefered way to go. .IP "Parse the whole file at once" 2 .IX Item "Parse the whole file at once" Implement a new methods that enables the parsing of a complete file at once, returning a lis of hashes. Possible extension to this could be to enable a column selection on the call: .Sp .Vb 1 \& my @AoH = $csv->parse_file ($filename, { cols => [ 1, 4..8, 12 ]}); .Ve .Sp Returning something like .Sp .Vb 9 \& [ { fields => [ 1, 2, "foo", 4.5, undef, "", 8 ], \& flags => [ ... ], \& errors => [ ... ], \& }, \& { fields => [ ... ], \& . \& . \& }, \& ] .Ve .IP "\s-1EBCDIC\s0" 2 .IX Item "EBCDIC" The hard-coding of characters and character ranges makes this module unusable on \s-1EBCDIC\s0 system. Using some #ifdef structure could enable these again without loosing speed. Testing would be the hard part. .SH "Release plan" .IX Header "Release plan" No guarantees, but this is what I have in mind right now: .IP "next" 2 .IX Item "next" .Vb 3 \& - This might very well be 1.00 \& - DIAGNOSTICS setction in pod to *describe* the errors (see below) \& - croak / carp .Ve .IP "next + 1" 2 .IX Item "next + 1" .Vb 2 \& - csv2csv - a script to regenerate a CSV file to follow standards \& - EBCDIC support .Ve .SH "DIAGNOSTICS" .IX Header "DIAGNOSTICS" Still under construction ... .PP If an error occured, \f(CW\*(C`$csv\-\*(C'\fRerror_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 \f(CW\*(C`error_diag ()\*(C'\fR in normal cases \- when no error occured \- may cause unexpected results. .PP If the constructor failed, the cause can be found using \f(CW\*(C`error_diag ()\*(C'\fR as a class method, like \f(CW\*(C`Text::CSV_XS\-\*(C'\fRerror_diag ()>. .PP \&\f(CW\*(C`$csv\-\*(C'\fRerror_diag ()> is automatically called upon error when the contractor was called with \f(CW\*(C`auto_diag\*(C'\fR set to 1 or 2, or when \f(CW\*(C`autodie\*(C'\fR is in effect. When set to 1, this will cause a \f(CW\*(C`warn ()\*(C'\fR with the error message, when set to 2, it will \f(CW\*(C`die ()\*(C'\fR. \f(CW\*(C`2012 \- EOF\*(C'\fR is excluded from \f(CW\*(C`auto_diag\*(C'\fR reports. .PP Currently errors as described below are available. I've tried to make the error itself explanatory enough, but more descriptions will be added. For most of these errors, the first three capitals describe the error category: .IP "\s-1INI\s0" 2 .IX Item "INI" Initialization error or option conflict. .IP "\s-1ECR\s0" 2 .IX Item "ECR" Carriage-Return related parse error. .IP "\s-1EOF\s0" 2 .IX Item "EOF" End-Of-File related parse error. .IP "\s-1EIQ\s0" 2 .IX Item "EIQ" Parse error inside quotation. .IP "\s-1EIF\s0" 2 .IX Item "EIF" Parse error inside field. .IP "\s-1ECB\s0" 2 .IX Item "ECB" Combine error. .IP "\s-1EHR\s0" 2 .IX Item "EHR" HashRef parse related error. .ie n .IP "1001 ""\s-1INI\s0 \- sep_char is equal to quote_char or escape_char""" 2 .el .IP "1001 ``\s-1INI\s0 \- sep_char is equal to quote_char or escape_char''" 2 .IX Item "1001 INI - 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. .ie n .IP "1002 ""\s-1INI\s0 \- allow_whitespace with escape_char or quote_char \s-1SP\s0 or \s-1TAB\s0""" 2 .el .IP "1002 ``\s-1INI\s0 \- allow_whitespace with escape_char or quote_char \s-1SP\s0 or \s-1TAB\s0''" 2 .IX Item "1002 INI - allow_whitespace with escape_char or quote_char SP or TAB" Using \f(CW\*(C`allow_whitespace\*(C'\fR when either \f(CW\*(C`escape_char\*(C'\fR or \f(CW\*(C`quote_char\*(C'\fR is equal to \s-1SPACE\s0 or \s-1TAB\s0 is too ambiguous to allow. .ie n .IP "1003 ""\s-1INI\s0 \- \er or \en in main attr not allowed""" 2 .el .IP "1003 ``\s-1INI\s0 \- \er or \en in main attr not allowed''" 2 .IX Item "1003 INI - r or n in main attr not allowed" Using default \f(CW\*(C`eol\*(C'\fR characters in either \f(CW\*(C`sep_char\*(C'\fR, \f(CW\*(C`quote_char\*(C'\fR, or \&\f(CW\*(C`escape_char\*(C'\fR is not allowed. .ie n .IP "2010 ""\s-1ECR\s0 \- \s-1QUO\s0 char inside quotes followed by \s-1CR\s0 not part of \s-1EOL\s0""" 2 .el .IP "2010 ``\s-1ECR\s0 \- \s-1QUO\s0 char inside quotes followed by \s-1CR\s0 not part of \s-1EOL\s0''" 2 .IX Item "2010 ECR - QUO char inside quotes followed by CR not part of EOL" When \f(CW\*(C`eol\*(C'\fR has been set to something specific, other than the default, like \f(CW"\er\et\en"\fR, and the \f(CW"\er"\fR is following the \fBsecond\fR (closing) \&\f(CW\*(C`quote_char\*(C'\fR, where the characters following the \f(CW"\er"\fR do not make up the \f(CW\*(C`eol\*(C'\fR sequence, this is an error. .ie n .IP "2011 ""\s-1ECR\s0 \- Characters after end of quoted field""" 2 .el .IP "2011 ``\s-1ECR\s0 \- Characters after end of quoted field''" 2 .IX Item "2011 ECR - Characters after end of quoted field" Sequences like \f(CW\*(C`1,foo,"bar"baz,2\*(C'\fR are not allowed. \f(CW"bar"\fR is a quoted field, and after the closing quote, there should be either a new-line sequence or a separation character. .ie n .IP "2012 ""\s-1EOF\s0 \- End of data in parsing input stream""" 2 .el .IP "2012 ``\s-1EOF\s0 \- End of data in parsing input stream''" 2 .IX Item "2012 EOF - End of data in parsing input stream" Self\-explaining. End-of-file while inside parsing a stream. Can only happen when reading from streams with \f(CW\*(C`getline ()\*(C'\fR, as using \f(CW\*(C`parse ()\*(C'\fR is done on strings that are not required to have a trailing \f(CW\*(C`eol\*(C'\fR. .ie n .IP "2021 ""\s-1EIQ\s0 \- \s-1NL\s0 char inside quotes, binary off""" 2 .el .IP "2021 ``\s-1EIQ\s0 \- \s-1NL\s0 char inside quotes, binary off''" 2 .IX Item "2021 EIQ - NL char inside quotes, binary off" Sequences like \f(CW\*(C`1,"foo\enbar",2\*(C'\fR are only allowed when the binary option has been selected with the constructor. .ie n .IP "2022 ""\s-1EIQ\s0 \- \s-1CR\s0 char inside quotes, binary off""" 2 .el .IP "2022 ``\s-1EIQ\s0 \- \s-1CR\s0 char inside quotes, binary off''" 2 .IX Item "2022 EIQ - CR char inside quotes, binary off" Sequences like \f(CW\*(C`1,"foo\erbar",2\*(C'\fR are only allowed when the binary option has been selected with the constructor. .ie n .IP "2023 ""\s-1EIQ\s0 \- \s-1QUO\s0 character not allowed""" 2 .el .IP "2023 ``\s-1EIQ\s0 \- \s-1QUO\s0 character not allowed''" 2 .IX Item "2023 EIQ - QUO character not allowed" Sequences like \f(CW\*(C`"foo "bar" baz",quux\*(C'\fR and \f(CW\*(C`2023,",2008\-04\-05,"Foo, Bar",\en\*(C'\fR will cause this error. .ie n .IP "2024 ""\s-1EIQ\s0 \- \s-1EOF\s0 cannot be escaped, not even inside quotes""" 2 .el .IP "2024 ``\s-1EIQ\s0 \- \s-1EOF\s0 cannot be escaped, not even inside quotes''" 2 .IX Item "2024 EIQ - EOF cannot be escaped, not even inside quotes" The escape character is not allowed as last character in an input stream. .ie n .IP "2025 ""\s-1EIQ\s0 \- Loose unescaped escape""" 2 .el .IP "2025 ``\s-1EIQ\s0 \- Loose unescaped escape''" 2 .IX Item "2025 EIQ - Loose unescaped escape" An escape character should escape only characters that need escaping. Allowing the escape for other characters is possible with the \f(CW\*(C`allow_loose_escape\*(C'\fR attribute. .ie n .IP "2026 ""\s-1EIQ\s0 \- Binary character inside quoted field, binary off""" 2 .el .IP "2026 ``\s-1EIQ\s0 \- Binary character inside quoted field, binary off''" 2 .IX Item "2026 EIQ - Binary character inside quoted field, binary off" Binary characters are not allowed by default. Exceptions are fields that contain valid \s-1UTF\-8\s0, that will automatically be upgraded is the content is valid \s-1UTF\-8\s0. Pass the \f(CW\*(C`binary\*(C'\fR attribute with a true value to accept binary characters. .ie n .IP "2027 ""\s-1EIQ\s0 \- Quoted field not terminated""" 2 .el .IP "2027 ``\s-1EIQ\s0 \- Quoted field not terminated''" 2 .IX Item "2027 EIQ - Quoted field not terminated" When parsing a field that started with a quotation character, the field is expected to be closed with a quotation character. When the parsed line is exhausted before the quote is found, that field is not terminated. .ie n .IP "2030 ""\s-1EIF\s0 \- \s-1NL\s0 char inside unquoted verbatim, binary off""" 2 .el .IP "2030 ``\s-1EIF\s0 \- \s-1NL\s0 char inside unquoted verbatim, binary off''" 2 .IX Item "2030 EIF - NL char inside unquoted verbatim, binary off" .PD 0 .ie n .IP "2031 ""\s-1EIF\s0 \- \s-1CR\s0 char is first char of field, not part of \s-1EOL\s0""" 2 .el .IP "2031 ``\s-1EIF\s0 \- \s-1CR\s0 char is first char of field, not part of \s-1EOL\s0''" 2 .IX Item "2031 EIF - CR char is first char of field, not part of EOL" .ie n .IP "2032 ""\s-1EIF\s0 \- \s-1CR\s0 char inside unquoted, not part of \s-1EOL\s0""" 2 .el .IP "2032 ``\s-1EIF\s0 \- \s-1CR\s0 char inside unquoted, not part of \s-1EOL\s0''" 2 .IX Item "2032 EIF - CR char inside unquoted, not part of EOL" .ie n .IP "2034 ""\s-1EIF\s0 \- Loose unescaped quote""" 2 .el .IP "2034 ``\s-1EIF\s0 \- Loose unescaped quote''" 2 .IX Item "2034 EIF - Loose unescaped quote" .ie n .IP "2035 ""\s-1EIF\s0 \- Escaped \s-1EOF\s0 in unquoted field""" 2 .el .IP "2035 ``\s-1EIF\s0 \- Escaped \s-1EOF\s0 in unquoted field''" 2 .IX Item "2035 EIF - Escaped EOF in unquoted field" .ie n .IP "2036 ""\s-1EIF\s0 \- \s-1ESC\s0 error""" 2 .el .IP "2036 ``\s-1EIF\s0 \- \s-1ESC\s0 error''" 2 .IX Item "2036 EIF - ESC error" .ie n .IP "2037 ""\s-1EIF\s0 \- Binary character in unquoted field, binary off""" 2 .el .IP "2037 ``\s-1EIF\s0 \- Binary character in unquoted field, binary off''" 2 .IX Item "2037 EIF - Binary character in unquoted field, binary off" .ie n .IP "2110 ""\s-1ECB\s0 \- Binary character in Combine, binary off""" 2 .el .IP "2110 ``\s-1ECB\s0 \- Binary character in Combine, binary off''" 2 .IX Item "2110 ECB - Binary character in Combine, binary off" .ie n .IP "2200 ""\s-1EIO\s0 \- print to \s-1IO\s0 failed. See errno""" 2 .el .IP "2200 ``\s-1EIO\s0 \- print to \s-1IO\s0 failed. See errno''" 2 .IX Item "2200 EIO - print to IO failed. See errno" .ie n .IP "3001 ""\s-1EHR\s0 \- Unsupported syntax for column_names ()""" 2 .el .IP "3001 ``\s-1EHR\s0 \- Unsupported syntax for column_names ()''" 2 .IX Item "3001 EHR - Unsupported syntax for column_names ()" .ie n .IP "3002 ""\s-1EHR\s0 \- getline_hr () called before column_names ()""" 2 .el .IP "3002 ``\s-1EHR\s0 \- getline_hr () called before column_names ()''" 2 .IX Item "3002 EHR - getline_hr () called before column_names ()" .ie n .IP "3003 ""\s-1EHR\s0 \- bind_columns () and column_names () fields count mismatch""" 2 .el .IP "3003 ``\s-1EHR\s0 \- bind_columns () and column_names () fields count mismatch''" 2 .IX Item "3003 EHR - bind_columns () and column_names () fields count mismatch" .ie n .IP "3004 ""\s-1EHR\s0 \- bind_columns () only accepts refs to scalars""" 2 .el .IP "3004 ``\s-1EHR\s0 \- bind_columns () only accepts refs to scalars''" 2 .IX Item "3004 EHR - bind_columns () only accepts refs to scalars" .ie n .IP "3006 ""\s-1EHR\s0 \- bind_columns () did not pass enough refs for parsed fields""" 2 .el .IP "3006 ``\s-1EHR\s0 \- bind_columns () did not pass enough refs for parsed fields''" 2 .IX Item "3006 EHR - bind_columns () did not pass enough refs for parsed fields" .ie n .IP "3007 ""\s-1EHR\s0 \- bind_columns needs refs to writeable scalars""" 2 .el .IP "3007 ``\s-1EHR\s0 \- bind_columns needs refs to writeable scalars''" 2 .IX Item "3007 EHR - bind_columns needs refs to writeable scalars" .ie n .IP "3008 ""\s-1EHR\s0 \- unexpected error in bound fields""" 2 .el .IP "3008 ``\s-1EHR\s0 \- unexpected error in bound fields''" 2 .IX Item "3008 EHR - unexpected error in bound fields" .PD .SH "SEE ALSO" .IX Header "SEE ALSO" perl, IO::File, IO::Handle, IO::Wrap, Text::CSV, Text::CSV_PP, Text::CSV::Encoded, Text::CSV::Separator, and Spreadsheet::Read. .SH "AUTHORS and MAINTAINERS" .IX Header "AUTHORS and MAINTAINERS" Alan Citterman \fI\fR wrote the original Perl module. Please don't send mail concerning Text::CSV_XS to Alan, as he's not involved in the C part which is now the main part of the module. .PP Jochen Wiedmann \fI\fR 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. .PP H.Merijn Brand \fI\fR cleaned up the code, added the field flags methods, wrote the major part of the test suite, completed the documentation, fixed some \s-1RT\s0 bugs and added all the allow flags. See ChangeLog releases 0.25 and on. .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright (C) 2007\-2010 H.Merijn Brand for \s-1PROCURA\s0 B.V. Copyright (C) 1998\-2001 Jochen Wiedmann. All rights reserved. Portions Copyright (C) 1997 Alan Citterman. All rights reserved. .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.