Web Development

Need help with reading files in PERL – JohnMcLernon_77



Posts: 11
From: Northern Ireland
Registered: 05-31-2004
Hi there. I'm trying to read info from a text file & store it in an array. Each bit of info is seperated by a ",". I tried using the split function to seperate them but will not work. When I go to print out the array, it just prints out my file location as a string rather than printing the file info. Writing to the file is fine, just can't read from it. Any help would be great.


sub process_form {
$surname = param('Surname');
$forename = param('Forename');
$exnumber = param('ExNumber');
$email = param('Email');
$department = param('Department');

$FullName = "$surname" . "_" . "$forename";
$TesterFile = "c:/progra~1/apache~1/apache/bin/list.txt";

open(LIST, ">>$TesterFile") || die "Can't open file: $!";

if ($email =~ m/@/) {
print LIST "$FullName,$exnumber,$email,$department\n";
close LIST;

open(LIST, "<$TesterFile") || die "Can't open file: $!";

@FileContents = split(/,/, $TesterFile);

close LIST;

print header, start_html( {-title=>'Beta Testers Application Results'} );

print "$FileContents[0]";

print end_html;


Come visit me @ my homepage - http://www.mclernon.com/

[This message has been edited by JohnMcLernon_77 (edited November 28, 2004).]


Junior Member

Posts: 2
From: Kansas City, MO
Registered: 12-01-2004

open(LIST, "<$TesterFile") || die "Can't open file: $!";

You open the file with the handle,

@FileContents = split(/,/, $TesterFile);

but then try to read from the file name.


@FileContents = <LIST>;

and then parse, or even

while ($line = <LIST> ){
...#line by line processing

You might even be able to pull off

@FileContents = split(/,/, <LIST> );

I'm not sure.

I did full time Perl development for 2 years, and I still alswys have to go back to some references for some things like filehandling.

This is the best tutorial/reference Ive found for perl.