Skip navigation.
Home
Your source for Perl tips, howto's, faq and tutorials
( categories: )

A CSV (Comma-Separated Values) file is a text file that consists of any numbers of records separated by line breaks. Each record consists of several fields; the fields are separated by a predefined character o string. Usually, the separator character is the comma character (;) but other characters can be used instead.

In order to extract information from CSV files , you need to obtain, for each record, the values of the different fields. How to do this in Perl depends of whether or not the separator character is embedded in any of the fields.

-- CSV file with no embedded separator characters

If you know in advance the fields don't contain the separator character, then the best option is to use 'split'

Example:
#!/usr/bin/perl
 
open FILE, "<file.csv" or die "Can't open file.csv: $!\n";
 
#-- process CSV records
while ( )
{
  chomp;
 
  #-- split the record into fields (comma is the separator)
  my @fields = split ',';
 
  # do something here with @fields
}

-- CSV file with Character separator embedded in the fields

Sometimes you have a CSV files that could contain the character separator as part of the data, in this case, the field is usually enclosed in double quotes. For
example, you can have a CSV file with a record like this:

007,"Bond, James",1952,english

In this case, you can't use 'split()' (split would separate the above line in five fields instead of four), the best solution is to use the Text::CSV_XS perl mod
ule

Example:
#!/usr/bin/perl
  
use Text::CSV_XS;
  
#-- create a Text::CSV_XS instance
my $csv = Text::CSV_XS->new ({ binary => 1, eol => $/ });
  
#-- open CSV file
open my $fh, "<file.csv" or die "Can't open file.csv: $!\n";
  
#-- process records
while (my $row = $csv->getline ($fh))
{
  #-- obtain the fields
  my @fields = @$row;
  
  # do something here with @fields
}