I want to use Text::xSV to handle some csv-type data. Problem is: the data is coming in via LWP::Simple, and I'd rather not take the trouble to save it to disk so that I can read it back as a file with Text::xSV.

It appears (based on the module's pod), that the only way to start the csv parsing process with this module is to "open a csv file". So, since the "file" starts out as a scalar variable in my case, I'd like to know if there's some way to associate a "file handle" with the scalar value, so that I can initialize the Text::xSV object with that file handle as input.

In other words, I want to fill in the blank in the example snippet below:

use strict; use Encode; # I'm getting utf8 data use LWP::Simple; use Text::xSV; my $fh; my $csv_url = "http://someplace/that/sends/data.csv"; my $content = decode( 'utf8', get( $csv_url )); # HERE IS THE BLANK TO FILL IN: # associate $fh (as file handle) with $content (as file data) # then: my $csv = Text::xSV( fh => $fh ); # or something like that(?) $csv->read_header; while ( $csv->get_row ) { # and so on }
BTW, please don't mention Text::CSV. That module has not been updated since it was uploaded to CPAN nine years ago. It's old, it's ugly, and it can only handle ASCII data (even accented characters in Latin1 will produce a "parse error", let alone anything like wide-characters). Unless/until Text::CSV is updated with significant improvements, I won't use or recommend it (and I regret having recommended it in the past). I wish I had time to fix it, since it's often the first one that folks find for doing csv work, but Text::xSV is already there, and hard to improve on.

I've already established that Text::xSV will handle utf8 wide character data without difficulty. I just would like to feed it input from a scalar (or an array of lines, whatever).

TIA

(updated the code snippet to fix the "Text::xSV->new" call, which originally had a couple different mistakes in it.)

(updated commentary on Text::CSV thanks to the behavior-modification powers of XP)


In reply to Treating a scalar as an "input file" by graff

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.