Perl: Open and read file line by line

By Eric Downing. Filed in Perl, Programming, Scripting, Utilities  |  
TOP del.icio.us digg

I regularly have to process Comma Seperated Value or CSV files, usually for user lists. So the following perl code is a framework for processing a file line by line.

#!/usr/bin/perl
use strict;
my $line;

open(FILEIN, "filename.txt");

while(<FILEIN>)
{
  chomp();
  $line = $_;
  # Process $line for data
  # Example seperating line by commas: 
  my ($username,$firstname,$lastname,$email) = split(/,/,$line);
}

close(FILEIN);

Tags: ,

Leave a Reply