Solved

Perl is writting \n as CRLF.  Why?

Posted on 2004-09-16
6
998 Views
Last Modified: 2008-03-04
I thought that \n represented just a line feed character.
Why does the following program result in outputing a CR LF pair, rather than just the LF.

#!/usr/bin/perl
print "\n";

I'm using Active Perl on XP.  I wondered if there was an environment variable I had to set to force Perl to treat \n as just a LF?

Thanks
Gary
0
Comment
Question by:countytechnologies
6 Comments
 
LVL 6

Accepted Solution

by:
holli earned 500 total points
ID: 12073535
\n stands for the standard newline depending on the os.

unix -> \x0A
dos/win -> \x0D\x0A
mac -> \x0D
0
 

Author Comment

by:countytechnologies
ID: 12073595
But even

print "\x0A";

outputs a \x0D\x0A pair.

How can I output just the \x0A running on a DOS system?
Maybe the print command is not the way to do it.

0
 

Author Comment

by:countytechnologies
ID: 12073692
Ultimately, I am trying to replace all the \r\n pairs in a file with just \n but I seem to be unable to write out just the \n character or even get the \n to be written in a search and replace (e.g. $string =~ s/\r\n/\n/g;), presumably because of the OS dependancy.



0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 6

Expert Comment

by:holli
ID: 12074334
open OUT, ">test";
binmode OUT;
print OUT "\x0A";
0
 
LVL 4

Expert Comment

by:divt
ID: 12076495
Did the result get converted by other intermediary functions/browsers/objects/functions?

Doing what holli suggest can identify this.

0
 
LVL 8

Expert Comment

by:davorg
ID: 12082305
All of these issues are covered in detail in the "perlport" manual page.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question