Mdc::TextCSV


Mdc::TextCSV


Name

 Mdc::TextCSV - Parse or construct a delimited string
 Version 1.041 - Released 12 March 2004


Synopsis

 use Mdc::TextCSV(Parse,Construct);
 @list   = Parse(\$record);
 $record = Construct(\@list);


Description

Provides a means to parse and construct a string of Comma Separated Values (CSV) or Tab Separated Values (TSV). This module will also create or parse values that are delimited with any other character or sequence of characters.

While constructing a delimited string, all string values will be automatically quoted with a double quote mark, however, Tab Separated Values (TSV) will use no quoting at all. Embedded TAB and new-line characters will be converted to the appropriate escape sequences.

The Parse routine will convert ``\n'' and ``\t'' escape sequences into their corresponding literal characters.


Parsing Rules

Will parse a line that contains any number of strings enclosed within single or double quotes as well as non-quoted strings and numeric values.

Values may contain embedded delimiter characters if they are enclosed within quotes.

Values may contain embedded quotes if the opposite quote character is used to enclose the string value.

Values may contain embedded quotes that are escaped with a backslash.

Double-double quote marks cannot be used to represent one double quote mark.


Routines

Parse

Parse a delimited line of text into a list of values. Returns a list of values. The optional second argument may define a delimiter. Any character or sequence of characters may be used.

If no delimiter is defined, will parse a single line of text that is contained within quote characters. If a comment is found, ( after a semicolon outside the quotes), it will be returned as the second item in the list. A delmited string may not contain comments.

 Parse( SCALAR reference, delimiter )

Construct

Construct a delimited line of text from a list of values. Returns a scalar string. The optional second argument may define a delimiter. Any character or sequence of characters may be used. If ommitted, the delimiter will default to a comma. If a SCALAR is passed instead of an ARRAY reference, the returned value will be quoted and escaped.

 Construct( ARRAY reference, delimiter )
 Construct( ARRAY reference )
 Construct( SCALAR )


Examples

Programming syntax

 use Mdc::TextCSV(Parse,Construct);
 my $record = "\"Fred Flinstone\", 12 Granite Dr., 'Little Rock, AR', 72904";
 my @list   = Parse(\$record);
 $record = Construct(\@list);
 # parse a tab delimited line
 my $record = "Fred Flinstone\t12 Granite Dr.\tLittle Rock, AR\t72904";
 my @list = Parse(\$record);
 # quote a SCALAR value
 my $str = Construct( 'Foo Bar' );              # result:  "Foo Bar"
 my $str = Construct( "Quote \"THIS!\"" );      # result:  'Quote "THIS!"'

Data syntax

The following are examples of quoting string values:

 "Mixed or none",Fred Flinstone,"12 Granite Dr.",'Little Rock, AR',72904
 "Embedded",Merl "The Perl" O'connor
 "Embedded","Merl \"The Perl\" O'connor"


Author and Copyright

 Parse Copyright (c) 2004, Mark K Mueller
 Mueller Design and Consulting http://www.markmueller.com/
 PO Box 576705, Modesto, CA 95357
 All Rights Reserved.

MkM 20:17 2003/12/12