Computer Science Canada

Perl Help

Author:  Droozle [ Tue Feb 08, 2011 8:31 pm ]
Post subject:  Perl Help

In a university lab we were given this piece of code:


#!/usr/bin/perl

use strict;
use warnings;
use Text::CSV;

my $file = $ARGV[0];
my $csv = Text::CSV->new();


open (CSV, $file) or die $!;

my @fields;

while ( my $record = <CSV>) {

if ( $csv->parse($record) ) {

@fields = $csv->fields();

print "$fields[0] $fields[1] $fields[2] \n";
}

else

{
my $err = $csv->error_input;

print "Failed to parse line: $err";
}
}

close CSV;



my issue is this is the first time i had ever used perl and we were asked to modify the code. Can i please have this piece of code explained line by line.

I know in general what the lines do but not the details of how to use them. I also know that the program is supposed to parse a CSV file.

I am using windows to do this, using "Context" to check notation and notepad to program so there will be no errors when it is transfered to a mac.

Thank you for any help you can give me.

Author:  Tony [ Tue Feb 08, 2011 9:11 pm ]
Post subject:  Re: Perl Help

Droozle @ Tue Feb 08, 2011 8:31 pm wrote:
my issue is this is the first time i had ever used perl and we were asked to modify the code.

Well, there's a first time for most things. Welcome to University! Your complementary copy of Perl documentation is available at http://perldoc.perl.org/

Your friendly TAs likely have their office hours posted on the course webpage, and will probably steer you in the direction of the intention of the assignment.


: