Mdc::TextCSV - Parse or construct a delimited string
Version 1.041 - Released 12 March 2004
use Mdc::TextCSV(Parse,Construct); @list = Parse(\$record); $record = Construct(\@list);
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.
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.
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 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 )
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!"'
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"
Parse Copyright (c) 2004, Mark K Mueller Mueller Design and Consulting http://www.224.com/ PO Box 576705, Modesto, CA 95357 All Rights Reserved.