homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

Newline (\n) doesn't appear when writing form output to disk
Happens only on the last line of a field

 2:14 am on Nov 1, 2009 (gmt 0)

My script accepts input from a web form with two TEXTAREAs, splits all the input into individual lines, then writes all the output to a file, putting a newline (\n) after each line.

Except the newline doesn't appear after the last line from each field.

For example, if TEXTAREA 1 contains:

and TEXTAREA 2 contains:

then my text file looks like this:
C 1

Here's my HTML code:
<form method=post action=test.cgi>
<textarea name=field1></textarea>
<textarea name=field2></textarea>
<input type=submit>

And my Perl code:
use CGI qw(:standard);

for $loop (1..2) {
$field = param('field'.$loop);
@items = split("\n",$field);
for $item (@items) { $output .= $item."\n"; }

print FILE $output;

Interestingly, if I print the output to the screen (Content-type:text/plain), it appears properly.

My text editor is TextWrangler. I chose "Show Invisibles" and it shows a red upside-down question mark between "C" and "1", and after "3".

Please don't tell me to just avoid splitting the input into individual lines. I really need to split the input, since in my final script I'll be performing some operations on each line individually.

Who understands newline issues?



 6:32 pm on Nov 1, 2009 (gmt 0)

then my text file looks like this:

In what, TextWrangler, which is showing you the \n's are there?

It's likely something system specific, and those differences are showing themselves in cross-platform examination. I say this because

if I print the output to the screen (Content-type:text/plain), it appears properly

... prints the output to the screen from your server, where it's still OK, but when you view it in a text editor, it's not. Unix-based servers have different end of line characters than Windows/Mac. Some things you can try:

- put the \n at the beginning of the lines instead
- experiment with \r or \r\n instead
- May or may not have any relevance, but be sure your editor can read Unix formatted files correctly
- Since you know your \n's are showing up where you expect them to be, and your task doesn't involve any reason for the files to be used off the server, forge onward with the rest of your task, as you don't really have a problem (maybe?)


 10:42 pm on Nov 1, 2009 (gmt 0)

Thanks, rocknbill. I heard what you were saying, but I was skeptical to think of it as a problem with my text editor, because when it came down to it, my output just wasn't what I set my Perl script to do. I should have had a \n at the end of each line, but I didn't.

So I looked a little harder, and I found the problem was with this line:

@items = split("\n",$field);

The input was actually coming in delimited with "\r\n", so setting the split pattern to that, fixed the problem.

But that raises another question: Does field input reliably come in with "\r\n" at the end of each line from any browser/OS combo? Or is sometimes it just "\r", and sometimes just "\n"?


 11:08 pm on Nov 1, 2009 (gmt 0)

i haven't tried it but something like this might solve the problem for the either/or/both cases:
@items = split(/[\n\r]+/,$field);


 12:36 am on Nov 2, 2009 (gmt 0)

Thanks, phranque, that does seem to work. But I'm just wondering if there's some kind of standard here for how form data is submitted, or if best practices is to just use the kind of regexp you suggested? I couldn't find anything in the search engines.


 10:32 pm on Nov 5, 2009 (gmt 0)

Nope, there's no standard. That's mainly because, as bill mentioned, different OSes have different line endings (\r\n for windows, \n for *ix and, iirc, \r for mac) and fortunately nobody came along and suggested an ascii-mode like in ftp (no really, ascii-mode is evil and I'm happy it's not in http).
If you need to split into lines, use phranques regexp. I think you might get along with \n as the splitter (or input seperator and using the string as a pseudo filehandle) and cho(m)p'ing each line, but while that might be faster, it's definetly more dangerous in terms of unwanted side effects.


 1:29 pm on Nov 8, 2009 (gmt 0)

I concur, everything I've ever seen comes down to the OS. There is no standard that I know of either, except for mail [webmasterworld.com]. Sometimes those other standards somehow slip into play when it comes to HTML, possibly due to (mis)interpretation by browser builders, but in this case I'm sure it is OS-specific.

The regex I often use for locating line endings is the carriage return first, making it optional \r?\n but if you are just looking to remove any and all of them the regex provided is going to meet your needs, as has already been suggested, confirmed and reconfirmed.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved