http://qs321.pair.com?node_id=657446

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

I am attempting to parse a CSV file in my script. I have installed the Tie::CSV_file module and have attempted in the code only to tie the file and print out some data to ensure I am on the right track with the following code...
my($result) = GetOptions ("date=s" => \$inDate); my($infile) = "USSTSF$inDate.txt"; tie my @csvdata, 'Tie::CSV_File', $infile; print "Line 5, column 1: ", $csvdata[4][0] . "\n";
However, I get the following error when attempting to run...
"Can't locate object method "parse" via package "quote_char" (perhaps you forgot to load "quote_char"?) at C:/Perl/lib/tie/CSV_File.pm line 198, <FH> line 5."
I checked via CPAN and the module Parse::CSV is up to date. Am I missing a different module. Any ideas would be greatly appreciated.

Replies are listed 'Best First'.
Re: Parse error
by derby (Abbot) on Dec 17, 2007 at 18:20 UTC

    Looks like you're missing

    use Tie::CSV_File;

    -derby

    Update: Actually it looks like you don't have Text::CSV_XS installed.

      I do have "use Tie::CSV_File;" and I do have Text::CSV XS installed if I look via the PPM gui. But it is noted as upgradeable. I marked it as install again via PPM in hopes of it upgrading with the latest module but after installing, it is still marked as upgradeable. If I try to install via the CPAN i get a nmake error and it states
      "make had a bad status, install seems impossible"
      What parse is the error referring to? I have the text::CSV_XS parse. What does the statement "Can't locate object method 'parse' via package 'quote_char' mean? What is the relevance of "quote_char"?
        ..still marked as upgradeable. If I try to install via the CPAN ...
        Which version do you have? Does the test-suite pass?

        What parse is the error referring to?
        trace the program (perl -d) or grep the source to find out

        What does the statement "Can't locate object method 'parse' via package 'quote_char' mean?
        to find out use diagnostics;

        What is the relevance of "quote_char"?
        trace or grep

Re: Parse error
by naChoZ (Curate) on Dec 18, 2007 at 14:34 UTC

    I'm sure Tie::CSV_file is a fine module, but you might also give Parse::CSV a spin. Here's some cutting and pasting of some code I wrote recently.

    use Parse::CSV; # Define the names of our csv fields # my $csv_fields = [ 'CoordinatorID', 'CompanyName', 'FirstName', 'LastName', 'City', 'State', 'csgaccount', 'Username', 'DomainName' ]; # Populate these hashrefs with data for comparison with the # list of names. # my $provisioned = fetch_csv_accounts({ filename => $opts->{prov}, fields => $csv_fields }); # {{{ sub fetch_csv_accounts # sub fetch_csv_accounts { my $args = shift; die "No field names provided...\n" unless defined $args->{fields}; die "No filename provided...\n" unless defined $args->{filename +}; # Default the keyfield to Username if nothing # is passed to this sub. # my $keyfield = $args->{keyfield} ? $args->{keyfield} : 'Username'; # # Create a Parse::CSV object for easy csv iteration # my $prov_csv = Parse::CSV->new( file => $args->{filename}, fields => $args->{fields}, ); die "Error: " . $prov_csv->errstr . "\n" if $prov_csv->errstr; # Loop through each row of the csv file and populate the # hashref. The resulting hashref will look like: # # Username1 -> CoordinatorID # -> CompanyName # -> FirstName # -> Lastname # -> City # -> State # -> csgaccount # -> Username # -> DomainName # Username2 -> CoordinatorID # -> CompanyName # ... etc ... my $return_hash = {}; while ( my $row = $prov_csv->fetch ) { foreach ( keys %{$row} ) { $return_hash->{ $row->{$keyfield} }->{$_} = $row->{$_}; } } return $return_hash; } # }}}

    --
    naChoZ

    Therapy is expensive. Popping bubble wrap is cheap. You choose.