Text::xSV

Text::xSV is a Perl module read character separated files.
Download

Text::xSV Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Ben Tilly
  • Publisher web site:
  • http://search.cpan.org/~tilly/Text-xSV-0.16/lib/Text/xSV.pm

Text::xSV Tags


Text::xSV Description

Text::xSV is a Perl module read character separated files. Text::xSV is a Perl module to read character separated files.SYNOPSIS use Text::xSV; my $csv = new Text::xSV; $csv->open_file("foo.csv"); $csv->read_header(); # Make the headers case insensitive foreach my $field ($csv->get_fields) { if (lc($field) ne $field) { $csv->alias($field, lc($field)); } } $csv->add_compute("message", sub { my $csv = shift; my ($name, $age) = $csv->extract(qw(name age)); return "$name is $age years oldn"; }); while ($csv->get_row()) { my ($name, $age) = $csv->extract(qw(name age)); print "$name is $age years oldn"; # Same as # print $csv->extract("message"); } # The file above could have been created with: my $csv = Text::xSV->new( filename => "foo.csv", header => , ); $csv->print_header(); $csv->print_row("Ben Tilly", 34, "M"); # Same thing. $csv->print_data( Age => 34, Name => "Ben Tilly", Sex => "M", );This module is for reading and writing a common variation of character separated data. The most common example is comma-separated. However that is far from the only possibility, the same basic format is exported by Microsoft products using tabs, colons, or other characters.The format is a series of rows separated by returns. Within each row you have a series of fields separated by your character separator. Fields may either be unquoted, in which case they do not contain a double-quote, separator, or return, or they are quoted, in which case they may contain anything, and will encode double-quotes by pairing them. In Microsoft products, quoted fields are strings and unquoted fields can be interpreted as being of various datatypes based on a set of heuristics. By and large this fact is irrelevant in Perl because Perl is largely untyped. The one exception that this module handles that empty unquoted fields are treated as nulls which are represented in Perl as undefined values. If you want a zero-length string, quote it.People usually naively solve this with split. A next step up is to read a line and parse it. Unfortunately this choice of interface (which is made by Text::CSV on CPAN) makes it difficult to handle returns embedded in a field. (Earlier versions of this document claimed impossible. That is false. But the calling code has to supply the logic to add lines until you have a valid row. To the extent that you don't do this consistently, your code will be buggy.) Therefore you it is good for the parsing logic to have access to the whole file.This module solves the problem by creating a CSV object with access to the filehandle, if in parsing it notices that a new line is needed, it can read at will. Requirements: · Perl


Text::xSV Related Software