Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8335857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:48:11+00:00 2026-06-09T03:48:11+00:00

I am new to Perl and still trying to figure out how to code

  • 0

I am new to Perl and still trying to figure out how to code in this language.

I am currently trying to split a long single string of csv into multiple lines.

Data example

a,b,c<br />x,y,x<br />

which I so far have manage to split up, adding in quotes, to add into a CSV file again later on:

"a,b,c""x,y,z"

By having the quotes it just signifies which sets of CSV are together as such.

The problem I am having is that when I try and create a CSV file, passing in data in a string i am getting an error

“Can’t call method “parse” on an undefined variable.

When I print out the string which I am passing in, it is defined and holds data. I am hoping that this is something simple which I am doing wrong through lack of experience.

The CSV code which I am using is:

use warnings;
use Text::CSV;
use Data::Dumper;
use constant debug => 0;
use Text::CSV;

print "Running CSV editor......\n";

#my $csv = Text::CSV->new({ sep_char => ',' });

my $file = $ARGV[0] or die "Need to get CSV file on the command line\n";

my $fileextension = substr($file, -4);

#If the file is a CSV file then read in the file.
if ($fileextension =~ m/csv/i)
{   
    print "Reading and formating: $ARGV[0] \n";

    open(my $data, '<', $file) or die "Could not open '$file' $!\n";

    my @fields;
    my $testline;
    my $line;

    while ($line = <$data>) 
    {       
        #Clears the white space at the end of the line.
        chomp $line;

        #Splits the line up and removes the <br />.
        $testline = join "\" \" ", split qr{<br\s?/>}, $line;

        #my $newStr = join $/, @lines;
        #print $newStr;
        my $q1 = "\"";
        $testline = join "", $q1,$testline,$q1;
        print "\n printing testline: \n $testline \n";

    } 

    $input_string = $testline;

    print "\n Testing input string line:\n $input_string";

    if ($csv->parse ($input_string)) 
    {
     my @field = $csv->fields;

     foreach my $col (0 .. $#field) {
         my $quo = $csv->is_binary ($col) ? $csv->{quote_char} : "";
         printf "%2d: %s%s%s\n", $col, $quo, $field[$col], $quo;#
        }
    }
    else 
    {
     print STDERR "parse () failed on argument: ",
         $csv->error_input, "\n";
     $csv->error_diag ();
     }
    #print $_,$/ for @lines;

print "\n Finished reading and formating: $ARGV[0] \n";
}else
{   
    print "Error: File is not a CSV file\n"
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:48:13+00:00Added an answer on June 9, 2026 at 3:48 am

    You did not create a Text::CSV object, but you try to use it.

    “Can’t call method “parse” on an undefined variable

    This means that your $csv is not there, thus it does not have a method called parse. Simply create a Text::CSV object first, at the top of your code below all the use lines.

    my $csv = Text::CSV->new;
    

    Pleae take a look at the CPAN documentation of Text::CSV.


    Also, did I mention you should use strict?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been spending the last hours trying to figure this out and now
I'm new to Perl and was experimenting around a bit. I have this code:
Im new to perl, so sorry if this is obvious, but i looked up
I am new to perl, and can't seem to find answers about this anywhere.
I have looked through a number of tutorials, but I still can't figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm stuck on this and have been all day.. I'm still pretty new to
This is the perl code ... use strict; use warnings; use LWP::UserAgent; use HTTP::Date;
I recently saw some Perl code that confused me. I took out all of
I am definitely new to Perl, and please forgive me if this seem like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.