Snail::CSV - Trying to understand how CSV work?

Tom Hukins tom at eborcom.com
Mon Jun 11 06:41:11 PDT 2007


Hi, Ravi.

On Fri, Jun 08, 2007 at 04:22:31PM +0000, Ravi Patel wrote:
> Can someone help me understand what the following Basic CSV example
> does?

It looks like you've lifted this example from Snail::CSV's
documentation.

What don't you understand about it?  How did the code's behaviour
differ from what you expected when you ran it?

Here's a brief explanation:

>    use Snail::CSV;
>    use Data::Dumper;

Here you define the modules you want to use.

>    my $csv = Snail::CSV->new();

And here you create a Snail::CSV object to work with.

>      $csv->setFile("lamps.csv", [ "id", "name", "pq" ]);
>      # or
>      $csv->setFile("lamps.csv", [ "id", "", "pq" ], { 'pq' => sub { my $pq = shift; $pq > 2 ? 1 : 0; } });

You call the setFile method on this object and pass some data to it.
The module's documentation tells us that the first argument,
lamps.csv, contains the filename you want to work with; the second
contains a list of field names, ('id', 'name' & 'pq' in the first
example; 'id' an unnamed field and 'pq' in the second example); and
the optional third argument contains a filter.  The documentation
neglects to mention what filters do, but I guess the allow you to
affect how you process the CSV.

>    my $lamps = $csv->parse;

Then you read the CSV file into a data structure called $lamps.

>    print Dumper($lamps);

And this shows you what that data structure looks like.

But before you get too involved with Snail::CSV, I'd suggest you take
a look at the more commonly used CSV parsing modules on CPAN.  I
prefer Text::xSV; others like Text::CSV_XS (or its Text::CSV_PP pure
perl alternative).

Also, please try to mail the list in plain text, or at least with a
plain text version of the HTML you send as it's easier to reply to and
helps subscribers who read the list's digest.

All the best,
Tom


More information about the MiltonKeynes-pm mailing list