Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cygwin vs. ActiveState Perl and Newlines

Status
Not open for further replies.

obscurifer

Technical User
Apr 3, 2001
53
US
I'm looking for a simple (or simple-minded) way to convert a text file from DOS-ish CR/LF end-of-line pairs to Unix-ish LF end-of-line characters. It should be pretty straightforward, since Perl contains a robust pattern matching facility. So here's my code.


#! /usr/bin/perl

while($line = <>) {
$line =~ s/\015\012/\012/g;
print "$line";
}


I know it can be simplified a bit by using $_ instead of teh $line assignment, but that's not relevant to my question. What this code is supposed to do is take each line of input, find the CR/LF pair, which is represented by the \015\012 octal characters, and replace it with the LF character.

I don't want to simply remove CR characters, because it's possible some files may have lone CR characters for some reason.

After I do the substitution, I print the modified line. Of course, this program will work as a filter with IO redirection to get the input and output files.

So, here's the question. Why does this work correctly on Cygwin's Perl build, but when I use ActiveState's Perl build, I actually get my file back with CR/LF pairs? Is ActiveState so smart that it knows that I really wanted a DOS-ish text file, so it automagically inserted CR characters before LFs? Is this cause for me to be concerned or angry?
 
I fixed it. I needed to use this line before the loop.

binmode STDOUT;

I'm still confused why this affects the explicit output of specific characters rather than simply affecting aliases like "\n" is something I'd be interested in finding out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top