normskib has asked for the wisdom of the Perl Monks concerning the following question:

Hi Folks

I have a MySQL BLOB field that stores data in CSV format. I want to parse this data using Text::CSV to remove any embedded line feeds. What's the best way to create an IO handle for the data that will work with the Text:CSV methods?

Any advice greatly appreciated.

Norman

Replies are listed 'Best First'.
Re: CSV data processing
by GrandFather (Saint) on Dec 09, 2008 at 23:00 UTC

    Your question doesn't make sense. Text::CSV parses lines (which is weakness of the module), not files.

    Maybe what you want is Text::xSV. Consider:

    use strict; use warnings; use Text::xSV; my $str = <<FILE; a,2,"3 a",4,5,6 b,2,3,4,5,6 FILE open my $inFile, '<', \$str; my $xsv = Text::xSV->new (fh => $inFile); $xsv->bind_fields (qw(1 2 3 4 5 6)); while (my @parts = $xsv->get_row ()) { s!\n!!g for @parts; print "@parts\n"; } close $inFile;

    Prints:

    a 2 3a 4 5 6 b 2 3 4 5 6

    Perl's payment curve coincides with its learning curve.
      Grandfather,

      Strange as it may be, Text::CSV allows you to do

      $colref = $csv->getline ($io); # Read a line from file $io, # parse it and return an array # ref of fields
      It is like an iterator over the file on a line-by-line basis.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        Indeed, that is so with later versions of Text::CSV (although not with earlier versions). But Text::CSV doesn't solve the OP's problem which is that some of the fields straddle lines. Text::xSV handles that case (so long as the field is suitably quoted), Text::CSV does not.


        Perl's payment curve coincides with its learning curve.
Re: CSV data processing
by ikegami (Patriarch) on Dec 09, 2008 at 22:47 UTC
    It expects a Perl file handle (which doesn't mean it needs to be connected to a file). open will do. Any descendant of IO::Handle (such as IO::File) will do as well. A tied file handle should work as well.

    Update: If you want to create a file handle that accesses a scalar, then you can do the following in ≥5.8.0:

    open(my $fh, '<', \$blob)
Re: CSV data processing
by CountZero (Bishop) on Dec 09, 2008 at 22:57 UTC
    Do you really need an IO-handle? $csv->parse ($line); can work with strings too.

    Getting the data into or out of a database and then connecting this string of data to an IO-handle before giving it to Text::CSV seems complicated.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James