http://www.perlmonks.org?node_id=145333

mr.dunstan has asked for the wisdom of the Perl Monks concerning the following question:

Hi - I'm 3 hours into this problem and I'm getting bummed out. Here's what I'm trying to do:

I have a cgi script I'm working on that allows me to upload a file into the perl script using a file input element and CGI.pm and a multipart form. No problem there, been there done that.

The problem is - once I have read the file contents into memory, I want to parse it line-by-line, hopefully by reading each line into an array (this is fixed-width data mixed in with some unstructured data, thanks silly vendor, and the document itself is only vaguely structured, but if I can read it line by line I'm good to go.) The thing is - once I've read it into a scalar, the line feeds disappear BUT if I read it out to a file on the local filesystem (not an option in production BTW), the line feeds are there?!

I've tried split on the line feeds but they're not there - see example below. Way confused. Anyway here's what I'm trying to do, in short. Yes I am using strict ...

use strict, CGI, blah blah ... my $cgi = new CGI; my $filename = $cgi->param('uploaded_file'); my $file = $filename; # can't remember why I do this, alway +s have $file =~ s!^.*(\\|\/)!!; # something about cleaning stuff up my $buffer; my $content_to_parse; while (my $bytesread = read($filename,$buffer,1024)) { $content_to_parse = $content_to_parse . $buffer; } # ok now file contents are in $content_to_parse # try splitting on ^M (windoze newlines) my @contentarray = split /\015$/, $content_to_parse; # doesn't work - @contentarray is empty # scratch head, get another diet coke # now do line by line parsing ... the end
I'm sure there's a better way, like reading the file into an array somehow. Oh perlmonks deliver me from this pain!!!!

-mr.dunstan