MOON
Server: Apache/2.2.34 (Unix) mod_ssl/2.2.34 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4 FrontPage/5.0.2.2635
System: Linux server.asjudinet.com 2.6.32-042stab141.3 #1 SMP Fri Nov 15 22:45:34 MSK 2019 i686
User: asjudine (504)
PHP: 5.2.17
Disabled: NONE
Upload Files
File: //usr/local/ssl/local/share/man/man3/Text::CSV_XS.3
.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32
.\"
.\" 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 "2011-09-07" "perl v5.8.8" "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_XS->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 \s-1ASCII\s0 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 \fB\f(BImust\fB\fR set \f(CW\*(C`binary => 1\*(C'\fR in the call to \*(L"new\*(R". 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
\&\*(L"parse\*(R" 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 does not 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"
.IX Subsection "Unicode"
On parsing (both for \*(L"getline\*(R" and \*(L"parse\*(R"), 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
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
.PP
On combining (\*(L"print\*(R" and \*(L"combine\*(R"), if any of the combining fields
was marked \s-1UTF8\s0, the resulting string will be marked \s-1UTF8\s0. Note however
that all fields \fIbefore\fR the first field that was marked \s-1UTF8\s0 and
contained 8\-bit characters that were not upgraded to \s-1UTF8\s0, 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:
.PP
.Vb 1
\& $csv->print ($fh, [ map { utf8::upgrade (my $x = $_); $x } @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 Xref "version"
.IX Subsection "version"
(Class method) Returns the current module version.
.Sh "new"
.IX Xref "new"
.IX Subsection "new"
(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.
.PP
.Vb 1
\& my $csv = Text::CSV_XS->new ({ attributes ... });
.Ve
.PP
Currently the following attributes are available:
.IP "eol" 4
.IX Xref "eol"
.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 \*(L"parse\*(R"d 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 Xref "sep_char"
.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 \*(L"\s-1CAVEATS\s0\*(R"
.IP "allow_whitespace" 4
.IX Xref "allow_whitespace"
.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 Xref "blank_is_undef"
.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 Xref "empty_is_undef"
.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 \fIreally\fR empty, not fields
that are empty after stripping allowed whitespace. \s-1YMMV\s0.
.IP "quote_char" 4
.IX Xref "quote_char"
.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 Xref "allow_loose_quotes"
.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 Xref "escape_char"
.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 Xref "allow_loose_escapes"
.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 Xref "binary"
.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 Xref "types"
.IX Item "types"
A set of column types; this attribute is immediately passed to the \*(L"types\*(R"
method. You must not set this attribute otherwise, except for using the
\&\*(L"types\*(R" method.
.IP "always_quote" 4
.IX Xref "always_quote"
.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 defined fields will be quoted. This is typically easier to
handle in external applications. (Poor creatures who are not using
Text::CSV_XS. :\-)
.IP "quote_space" 4
.IX Xref "quote_space"
.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
attribute to 0.
.IP "quote_null" 4
.IX Xref "quote_null"
.IX Item "quote_null"
By default, a \s-1NULL\s0 byte in a field would be escaped. This attribute enables
you to treat the \s-1NULL\s0 byte as a simple binary character in binary mode (the
\&\f(CW\*(C`{ binary => 1 }\*(C'\fR is set). The default is true.  You can prevent \s-1NULL\s0
escapes by setting this attribute to 0.
.IP "keep_meta_info" 4
.IX Xref "keep_meta_info"
.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 \*(L"meta_info\*(R", \*(L"is_quoted\*(R",
and \*(L"is_binary\*(R" described below.  Default is false.
.IP "verbatim" 4
.IX Xref "verbatim"
.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 \*(L"getline\*(R", \*(L"getline\*(R" 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 should not 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 Xref "auto_diag"
.IX Item "auto_diag"
Set to true will cause \*(L"error_diag\*(R" 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 18
\& $csv = Text::CSV_XS->new ({
\&     quote_char          => '"',
\&     escape_char         => '"',
\&     sep_char            => ',',
\&     eol                 => $\e,
\&     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,
\&     });
.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 \*(L"new\*(R" constructor call fails, it returns \f(CW\*(C`undef\*(C'\fR, and makes the
fail reason available through the \*(L"error_diag\*(R" method.
.PP
.Vb 2
\& $csv = Text::CSV_XS->new ({ ecs_char => 1 }) or
\&     die "".Text::CSV_XS->error_diag ();
.Ve
.PP
\&\*(L"error_diag\*(R" will return a string like
.PP
.Vb 1
\& "INI - Unknown attribute 'ecs_char'"
.Ve
.Sh "print"
.IX Xref "print"
.IX Subsection "print"
.Vb 1
\& $status = $csv->print ($io, $colref);
.Ve
.PP
Similar to \*(L"combine\*(R" + \*(L"string\*(R" + \*(L"print\*(R", but way 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 \*(L"print\*(R" 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
For performance reasons the print method does not create a result string.
In particular the \*(L"string\*(R", \*(L"status\*(R", \*(L"fields\*(R", and \*(L"error_input\*(R"
methods are meaningless after executing this method.
.PP
as in perl 5.005 and older, the glob \f(CW\*(C`\e*FILE\*(C'\fR 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 IO::File and IO::Wrap
for details.
.Sh "combine"
.IX Xref "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, \*(L"string\*(R" can be
called to retrieve the resultant \s-1CSV\s0 string.  Upon failure, the value
returned by \*(L"string\*(R" is undefined and \*(L"error_input\*(R" can be called to
retrieve an invalid argument.
.Sh "string"
.IX Xref "string"
.IX Subsection "string"
.Vb 1
\& $line = $csv->string ();
.Ve
.PP
This object function returns the input to \*(L"parse\*(R" or the resultant \s-1CSV\s0
string of \*(L"combine\*(R", whichever was called more recently.
.Sh "getline"
.IX Xref "getline"
.IX Subsection "getline"
.Vb 1
\& $colref = $csv->getline ($io);
.Ve
.PP
This is the counterpart to \*(L"print\*(R", like \*(L"parse\*(R" is the counterpart to
\&\*(L"combine\*(R": It reads a row from the \s-1IO\s0 object using \f(CW\*(C`$io\->getline\*(C'\fR
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 \*(L"bind_columns\*(R", the return value is a
reference to an empty list.
.PP
The \*(L"string\*(R", \*(L"fields\*(R", and \*(L"status\*(R" methods are meaningless, again.
.Sh "getline_all"
.IX Xref "getline_all"
.IX Subsection "getline_all"
.Vb 3
\& $arrayref = $csv->getline_all ($io);
\& $arrayref = $csv->getline_all ($io, $offset);
\& $arrayref = $csv->getline_all ($io, $offset, $length);
.Ve
.PP
This will return a reference to a list of getline ($io) results.
In this call, \f(CW\*(C`keep_meta_info\*(C'\fR is disabled. If \f(CW$offset\fR is negative, as
with \f(CW\*(C`splice\*(C'\fR, only the last \f(CW\*(C`abs ($offset)\*(C'\fR records of \f(CW$io\fR are taken
into consideration.
.PP
Given a \s-1CSV\s0 file with 10 lines:
.PP
.Vb 10
\& 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
.Ve
.Sh "parse"
.IX Xref "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, \*(L"fields\*(R" can be called to
retrieve the decomposed fields .  Upon failure, the value returned by
\&\*(L"fields\*(R" is undefined and \*(L"error_input\*(R" can be called to retrieve the
invalid argument.
.PP
You may use the \*(L"types\*(R" method for setting column types. See \*(L"types\*(R"'
description below.
.Sh "getline_hr"
.IX Xref "getline_hr"
.IX Subsection "getline_hr"
The \*(L"getline_hr\*(R" and \*(L"column_names\*(R" methods work together to allow you
to have rows returned as hashrefs. You must call \*(L"column_names\*(R" 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
\&\*(L"getline_hr\*(R" will croak if called before \*(L"column_names\*(R".
.Sh "getline_hr_all"
.IX Xref "getline_hr_all"
.IX Subsection "getline_hr_all"
.Vb 3
\& $arrayref = $csv->getline_hr_all ($io);
\& $arrayref = $csv->getline_hr_all ($io, $offset);
\& $arrayref = $csv->getline_hr_all ($io, $offset, $length);
.Ve
.PP
This will return a reference to a list of getline_hr ($io)
results.  In this call, \f(CW\*(C`keep_meta_info\*(C'\fR is disabled.
.Sh "column_names"
.IX Xref "column_names"
.IX Subsection "column_names"
Set the keys that will be used in the \*(L"getline_hr\*(R" calls. If no keys
(column names) are passed, it'll return the current setting.
.PP
\&\*(L"column_names\*(R" accepts a list of scalars (the column names) or a single
array_ref, so you can pass \*(L"getline\*(R"
.PP
.Vb 1
\& $csv->column_names ($csv->getline ($io));
.Ve
.PP
\&\*(L"column_names\*(R" 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\->{"\ecAUNDEF\ecA"}\*(C'\fR to the 1st field, \f(CW\*(C`$hr\->{""}\*(C'\fR to
the 2nd field, and \f(CW\*(C`$hr\->{name}\*(C'\fR to the 4th field, discarding the 3rd
field.
.PP
\&\*(L"column_names\*(R" croaks on invalid arguments.
.Sh "bind_columns"
.IX Xref "bind_columns"
.IX Subsection "bind_columns"
Takes a list of references to scalars to store the fields fetched
\&\*(L"getline\*(R" in. When you don't pass enough references to store the fetched
fields in, \*(L"getline\*(R" 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 Xref "eof"
.IX Subsection "eof"
.Vb 1
\& $eof = $csv->eof ();
.Ve
.PP
If \*(L"parse\*(R" or \*(L"getline\*(R" 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 Xref "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
\&\*(L"parse\*(R" and \*(L"getline\*(R" 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 Xref "IV"
.IX Item "IV"
Set field type to integer.
.IP "\s-1NV\s0" 4
.IX Xref "NV"
.IX Item "NV"
Set field type to numeric/float.
.IP "\s-1PV\s0" 4
.IX Xref "PV"
.IX Item "PV"
Set field type to string.
.Sh "fields"
.IX Xref "fields"
.IX Subsection "fields"
.Vb 1
\& @columns = $csv->fields ();
.Ve
.PP
This object function returns the input to \*(L"combine\*(R" or the resultant
decomposed fields of a successful \*(L"parse\*(R", whichever was called more
recently.
.PP
Note that the return value is undefined after using \*(L"getline\*(R", which does
not fill the data structures returned by \*(L"parse\*(R".
.Sh "meta_info"
.IX Xref "meta_info"
.IX Subsection "meta_info"
.Vb 1
\& @flags = $csv->meta_info ();
.Ve
.PP
This object function returns the flags of the input to \*(L"combine\*(R" or the
flags of the resultant decomposed fields of \*(L"parse\*(R", 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 \*(L"fields\*(R" method or passed to the \*(L"combine\*(R"
method. The flags are bit\-wise\-or'd like:
.ie n .IP """ ""0x0001" 2
.el .IP "\f(CW \fR0x0001" 2
.IX Item " 0x0001"
The field was quoted.
.ie n .IP """ ""0x0002" 2
.el .IP "\f(CW \fR0x0002" 2
.IX Item " 0x0002"
The field was binary.
.PP
See the \f(CW\*(C`is_***\*(C'\fR methods below.
.Sh "is_quoted"
.IX Xref "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 \*(L"parse\*(R".
.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 Xref "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 \*(L"parse\*(R".
.PP
This returns a true value if the data in the indicated column contained any
byte in the range \f(CW\*(C`[\ex00\-\ex08,\ex10\-\ex1F,\ex7F\-\exFF]\*(C'\fR.
.Sh "is_missing"
.IX Xref "is_missing"
.IX Subsection "is_missing"
.Vb 1
\& my $missing = $csv->is_missing ($column_idx);
.Ve
.PP
Where \f(CW$column_idx\fR is the (zero\-based) index of the column in the last
result of \*(L"getline_hr\*(R".
.PP
.Vb 3
\& while (my $hr = $csv->getline_hr ($fh)) {
\&     $csv->is_missing (0) and next; # This was an empty line
\&     }
.Ve
.PP
When using \*(L"getline_hr\*(R" for parsing, it is impossible to tell if the
fields are \f(CW\*(C`undef\*(C'\fR because they where not filled in the \s-1CSV\s0 stream or
because they were not read at all, as \fBall\fR the fields defined by
\&\*(L"column_names\*(R" are set in the hash\-ref. If you still need to know if all
fields in each row are provided, you should enable \f(CW\*(C`keep_meta_info\*(C'\fR so you
can check the flags.
.Sh "status"
.IX Xref "status"
.IX Subsection "status"
.Vb 1
\& $status = $csv->status ();
.Ve
.PP
This object function returns success (or failure) of \*(L"combine\*(R" or
\&\*(L"parse\*(R", whichever was called more recently.
.Sh "error_input"
.IX Xref "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
\&\*(L"combine\*(R" or \*(L"parse\*(R", whichever was called more recently.
.Sh "error_diag"
.IX Xref "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 occurred, 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 \fIexamples/csv\-check\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
diagnostics is that of the last \*(L"new\*(R" call.
.Sh "SetDiag"
.IX Xref "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"
.Sh "Reading a \s-1CSV\s0 file line by line:"
.IX Subsection "Reading a CSV file line by line:"
.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
.Sh "Parsing \s-1CSV\s0 strings:"
.IX Subsection "Parsing CSV strings:"
.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
.Sh "Printing \s-1CSV\s0 data"
.IX Subsection "Printing CSV data"
\fIThe fast way: using \*(L"print\*(R"\fR
.IX Subsection "The fast way: using print"
.PP
An example for creating \s-1CSV\s0 files using the \*(L"print\*(R" 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
\fIThe slow way: using \*(L"combine\*(R" and \*(L"string\*(R"\fR
.IX Subsection "The slow way: using combine and string"
.PP
or using the slower \*(L"combine\*(R" and \*(L"string\*(R" 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
.Sh "The examples folder"
.IX Subsection "The examples folder"
For more extended examples, see the \fIexamples/\fR (1) sub-directory in the
original distribution or the git repository (2).
.PP
.Vb 2
\& 1. http://repo.or.cz/w/Text-CSV_XS.git?a=tree;f=examples
\& 2. http://repo.or.cz/w/Text-CSV_XS.git
.Ve
.PP
The following files can be found there:
.IP "parser\-xs.pl" 2
.IX Xref "parser-xs.pl"
.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 Xref "csv-check"
.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.5 using Text::CSV_XS 0.81
\& OK: rows: 1, columns: 2
\&     sep = <,>, quo = <">, bin = <1>
.Ve
.IP "csv2xls" 2
.IX Xref "csv2xls"
.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 Xref "csvdiff"
.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 sub\-directory, 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 \*(L"meta_info\*(R", \*(L"is_quoted\*(R",
and \*(L"is_binary\*(R" to accept setting these flags for fields, so you can
specify which fields are quoted in the \*(L"combine\*(R"/\*(L"string\*(R" 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 \*(L"combine\*(R" and \*(L"string\*(R"
in a single call will \fBnot\fR be honored. Likewise for \*(L"parse\*(R" and
\&\*(L"fields\*(R". Given the trouble with embedded newlines, Using \*(L"getline\*(R" and
\&\*(L"print\*(R" instead is the preferred way to go.
.IP "Parse the whole file at once" 2
.IX Item "Parse the whole file at once"
Implement new methods that enable parsing of a complete file at once,
returning a list 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 7
\& [ { fields => [ 1, 2, "foo", 4.5, undef, "", 8 ],
\&     flags  => [ ... ],
\&     },
\&   { fields => [ ... ],
\&     .
\&     },
\&   ]
.Ve
.Sp
Note that \*(L"getline_all\*(R" already returns all rows for an open stream, but
this will not return flags.
.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.
.Sp
Opening \s-1EBCDIC\s0 encode files on \s-1ASCII+\s0 systems is likely to succeed using
Encode's cp37, cp1047, or posix\-bc:
.Sp
.Vb 1
\& open my $fh, "<:encoding(cp1047)", "ebcdic_file.csv" or die "...";
.Ve
.Sh "Release plan"
.IX Subsection "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 occurred, \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
\&\*(L"error_diag\*(R" in normal cases \- when no error occurred \- may cause
unexpected results.
.PP
If the constructor failed, the cause can be found using \*(L"error_diag\*(R" 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 have 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.
.PP
And below should be the complete list of error codes that can be returned:
.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 Xref "1001"
.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 Xref "1002"
.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 Xref "1003"
.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 Xref "2010"
.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 Xref "2011"
.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 Xref "2012"
.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 \*(L"getline\*(R", as using \*(L"parse\*(R" 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 Xref "2021"
.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 Xref "2022"
.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 Xref "2023"
.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 Xref "2024"
.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 Xref "2025"
.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 Xref "2026"
.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 Xref "2027"
.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 Xref "2030"
.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 Xref "2031"
.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 Xref "2032"
.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 Xref "2034"
.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 Xref "2035"
.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 Xref "2036"
.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 Xref "2037"
.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 Xref "2110"
.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 Xref "2200"
.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 Xref "3001"
.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 Xref "3002"
.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 Xref "3003"
.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 Xref "3004"
.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 Xref "3006"
.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 writable scalars""" 2
.el .IP "* 3007 ``\s-1EHR\s0 \- bind_columns needs refs to writable scalars''" 2
.IX Xref "3007"
.IX Item "3007 EHR - bind_columns needs refs to writable 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 Xref "3008"
.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<alan@mfgrtl.com>\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<joe@ispsoft.de>\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 \fIChangeLog\fR releases 0.10 through 0.23.
.PP
H.Merijn Brand \fI<h.m.brand@xs4all.nl>\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"
.Vb 3
\& Copyright (C) 2007-2011 H.Merijn Brand for PROCURA B.V. All rights reserved.
\& Copyright (C) 1998-2001 Jochen Wiedmann. All rights reserved.
\& Portions Copyright (C) 1997 Alan Citterman. All rights reserved.
.Ve
.PP
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.