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

I just installed Text::CSV, my code is below, and here is my error:
Can't call method "getline" without a package or object reference at C:/Perl/site/lib/Text/CSV_XS.pm line 555, <FILE> line 2.
This might seem trivial, but I can't find the answer when I google my error.
I have also tried, use Text::CSV & Text::CSV->new and still had the same error.
#!C:\Perl\bin\perl use warnings; use strict; use Text::CSV_XS; my $file = "file.csv"; open FILE, $file or die "cannot open file\n"; my $first = <FILE>; my @columns = split(",", $first); my $csv = Text::CSV_XS->new ( { binary => 1 } ) or die "Cannot use CSV +: ".Text::CSV->error_diag (); $csv->column_names(@columns); my $lineNum = 1; while (my $line = <FILE>) { my $hr = $csv->getline_hr($line); my $sku = $hr->{ 'sku' }; print "$sku\n"; $lineNum++; } close FILE

Replies are listed 'Best First'.
Re: missing package or object reference
by Eliya (Vicar) on Dec 23, 2011 at 22:17 UTC

    I think you meant:

    ... my $lineNum = 1; while ( my $hr = $csv->getline_hr(*FILE) ) { my $sku = $hr->{ 'sku' }; print "$sku\n"; $lineNum++; }

    The getline_hr() method - just as the underlying getline() - expects an IO object (Perl file handle), not a string.

    To parse lines directly from strings, you'd say $csv->parse($line) — but there's no parse_hr() method  (you don't need it for what you're doing, as the above should work fine).

    (When using $csv->getline_hr($line), I do get the exact same error.)

      Eliya++

      I independently came to the same solution after reviewing this node, and confirm that it works.

                  "Battle not with trolls, lest ye become a troll; and if you gaze into the Internet, the Internet gazes also into you."
              -Friedrich Nietzsche: A Dynamic Translation

        Thank you this worked.
Re: missing package or object reference
by Anonymous Monk on Dec 23, 2011 at 19:34 UTC

    How did you install it? Which version? Did you run the test suite , and what were the results?

      I installed it using Active State PPM. It looks like I have version 1.21. I did not use test suite.

        Note: Text::CSV and Text::CSV_XS are different modules by different authors. Sure you installed the correct one?

        Also, a few XS modules wont correctly work when instaklled with PPM. Try this on the command line:

        perl -MCPAN -e shell install Text::CSV_XS

        Answer all the install (temporary) this or that questions with yes. Post any errors you encounter.

        BREW /very/strong/coffee HTTP/1.1
        Host: goodmorning.example.com
        
        418 I'm a teapot