Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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 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!!!!


In reply to splitting on win32 linefeeds aka pass the crackpipe! by mr.dunstan

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2022-05-16 19:48 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (63 votes). Check out past polls.