NAME
Parse::Stallion::CSV - Comma Separated Values
SYNOPSIS
This is primarily for demonstrating Parse::Stallion.
use Parse::Stallion::CSV;
my $csv_stallion = new Parse::Stallion::CSV;
my $input_string = 'header1,header2,header3'."\n";
$input_string .= 'field_1_1,field_1_2,field_1_3'."\n";
$input_string .=
'"field_2_1 3 words",field_2_2 3 words,\"field3_2 x\"'."\n";
my $result = eval {$csv_stallion->
parse_and_evaluate({parse_this=>$input_string})};
if ($@) {
if ($csv_stallion->parse_failed) {#parse failed};
}
# $result should contain reference to a hase same as
{'header' => [ 'header1', 'header2', 'header3' ],
'records' => [
[ 'field_1_1', 'field_1_2', 'field_1_3' ],
[ 'field_2_1 3 words', 'field_2_2 3 words', '"field3_2 x"' ]
]
};
DESCRIPTION
Reads a comma separated value string, returning a reference to a hash containing the headers and the data.
The source of the grammar from the RFC and the implementation follow to demonstrate how one can use Parse::Stallion.
GRAMMAR SOURCE
The grammar used here is based on RFC 4180, see for example http://tools.ietf.org/html/rfc41801. The grammar represented by an ABNF grammar:
file = [header CRLF] record *(CRLF record) [CRLF]
header = name *(COMMA name)
record = field *(COMMA field)
name = field
field = (escaped / non-escaped)
escaped = DQUOTE *(TEXTDATA / COMMA / CR / LF / 2DQUOTE) DQUOTE
non-escaped = *TEXTDATA
COMMA = %x2C
CR = %x0D
DQUOTE = %x22
LF = %x0A
CRLF = CR LF
TEXTDATA = %x20-21 / %x23-2B / %x2D-7E