ePages 6.10 - DE_EPAGES/Core/API/CSV.pm

Package DE_EPAGES::Core::API::CSV

Provides methods for creating and parsing CSV (comma separated values) files. This module wraps Text::CSV_XS and support for unicode by default. Therefore the Text::CSV_XS::new option "binary" is always on unless explicitely disabled. All function arguments and return values are assumed to be native UTF-8 perl strings.

Example
$aFields = ParseCSV( '/Shops/DemoShop/Products/"s k u"', { 'sep_char' => '/' } );
my $aFields = ['Shops', 'DemoShop', Products', 's k u'];
print CombineCSV( $aFields, { 'sep_char' => '/' } );
my $CSV = DE_EPAGES::Core::API::CSV->new;
my $fh = FileHandle->new;
$fh->open( $FileName, '<:encoding(utf-8)' ) or die "Can't read $FileName: $!";
while( $aFields = $CSV->getline($fh) ) {
    # process record
}
@EXPORT_OK
ParseCSV
CombineCSV
ParseCSVArray
CombineCSVArray

Functions

CombineCSV
CombineCSVArray
ParseCSV
ParseCSVArray
combine
converter
getline
new
parse

CombineCSV

Combines a list of fields to one CSV formatted line.

Syntax
$Line = CombineCSV( $aFields, $hOptions );
$Line = CombineCSV( $aFields );
Input
$aFields (ref.array.string)
parsed fields
$hOptions (ref.hash.string)
(optional) see new
Return
$Line (string)
CSV formatted string

CombineCSVArray

Combines a list of lines to one CSV formatted string.

Syntax
$Line = CombineCSVArray( $aaFields, $hOptions );
$Line = CombineCSVArray( $aaFields );
Input
$aaFields (ref.array.array.string)
array of lines, each line is an array of strings
$hOptions (ref.hash.string)
see new
The default end-of-line string (eol) is "\n".
Return
$Line (string)
CSV formatted string

ParseCSV

Parses one CSV formatted line and returns the parsed fields.

Syntax
$aFields = ParseCSV( $Line, $hOptions );
$aFields = ParseCSV( $Line );
Input
$Line (string)
CSV formatted string
$hOptions (ref.hash.string)
see new
Return
$aFields (ref.array.string)
parsed fields

ParseCSVArray

Combines a list of fields to a CSV formatted string.

Syntax
$aaFields = ParseCSVArray( $Content, $hOptions );
$aaFields = ParseCSVArray( $Content );
Example
$aaFields = ParseCSVArray( "Name,Value\nSmith,45\nSummers,21\n" );
Input
$Content (string)
CSV formatted string
$hOptions (ref.hash.string)
(optional) see new.
The default end-of-line string (eol) is "\n".
Other eol strings don't work (see perldoc Text::CSV_XS).
Return
$aaFields (ref.array.array.string)
parsed fields

combine

Combines a list of fields to one CSV formatted line.

Syntax
$Line = $CSV->combine( $aFields );
Input
$aFields (ref.array.string)
parsed fields
Return
$Line (string)
CSV formatted string

converter

Returns the underlying Text::CSV_XS object.

Syntax
$Converter = $CSV->converter;
Return
$Converter (object)
instance of Text::CSV_XS

getline

Reads one record from a CSV file. Returns the parsed fields or undef on end of file.

Syntax
$aFields = $CSV->getline( $FileHandle );
Input
$FileHandle (IO::Handle object)
file handle
Return
$aFields (ref.array.string)
parsed fields, undef on EOF

new

Create a CSV parser and formatter object.

Syntax
$CSV = DE_EPAGES::Core::API::CSV->new( $hOptions );
$CSV = DE_EPAGES::Core::API::CSV->new;
Example
$CSV = DE_EPAGES::Core::API::CSV->new( { 'sep_char' => '/' } );
Input
$hOptions
(optional) format options, see also Text::CSV_XS::new.
  • sep_char - seperator character (default: ',')
  • binary - allow embedded newlines and unicode (default: 1)
  • quote_char - quote character (default: '"')
  • escape_char - escape character (default: '"')
  • eol - line break character(s) (default: '')
  • always_quote - always quote fields (default: 0)
Return
$CSV (object)
CSV parser and formatter

parse

Parses one CSV formatted line and returns the parsed fields.

Syntax
$aFields = $CSV->parse( $Line );
Input
$Line (string)
CSV formatted string
Return
$aFields (ref.array.string)
parsed fields