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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.