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

I have opened my CSV file in perl and was able to to go through it using the code below:
my $filename = 'filename.txt'; open(my $fh, '<', $filename) or die "Could not open file '$filename' $!"; while (my $row = <$fh>) { chomp $row; print "$row\n";
Now, i want to use fgets or any other function to break the file into individual records. What code can I use to implement this. Any help will be appreciated

Replies are listed 'Best First'.
Re: how to use fgets to Break file into individual records
by Corion (Patriarch) on Apr 08, 2015 at 15:14 UTC

    Have you looked at Text::CSV_XS? Alternatively, if you're hell-bent on rolling your own, see split.

Re: how to use fgets to Break file into individual records
by mr_mischief (Monsignor) on Apr 08, 2015 at 16:29 UTC

    I'm not sure I understand. In a CSV file a row is a record. Do you mean individual fields of the record? There's split, or m// with captures, or you could write a full parser with something like Parse::YAPP or Parse::RecDescent.

    There are a number of tools that do this correctly already, and very few first-time attempts get it right. Take a look at these:

    * These require DBI and to treat the data as if it was a database.**
    ** That's handy if you're already using DBI for something else in the application or intend to replace the CSV files with a database later.

Re: how to use fgets to Break file into individual records
by bitingduck (Deacon) on Apr 08, 2015 at 15:37 UTC

    Can you wrap <code> tags around the script to make it easier to read?

Re: how to use fgets to Break file into individual records
by Marshall (Canon) on Apr 09, 2015 at 05:43 UTC
    fgets() is not the right way..

    Show the data that you want to parse with Perl.

    there are other ways to do this.