merrymonk has asked for the wisdom of the Perl Monks concerning the following question:

Yesterday kindly Monks gave some very useful advice about how to find out what ALL the characters were in a text file.
It was all part of my efforts to write UNIX files on a Windows based PC.
The next job is to write the UNIX text file (to be created by reading in a Windows file and writing out each line.
Therefore in the print statement I used \012 at the end of the line rather than the usual \n.
I even added binmode to the input and output files. The Perl extract is below.
I then processed this with the code that looked at each character in the line (having set the input file handle using binmode).
I found both ASCII 13 and ASCII 10. I was hoping to find just ASCII 10! I then found that without the \012 I still got an ASCII 13 character at the end of each input line.
How do I stop print giving me the ASCII 13 character at the end of each line?
binmode(TXTIN); binmode(UNIXOUT); while(defined($linein = <TXTIN>)) { chomp($linein); print UNIXOUT "$linein\012"; }

Replies are listed 'Best First'.
Re: Writing a UNIX text line - the end character challenge
by SuicideJunkie (Vicar) on Aug 11, 2010 at 13:28 UTC

    You need to do more debug printing.

    Read a very short file with a few characters over just two lines for simplicity. print " " . ord($_) foreach split //, $linein; And do that both before and after the chomp.

    Notice what's going on now?

    open my $fh, '<', 'short.txt'; binmode $fh; my $linein = <$fh>; print " " . ord($_) foreach split //, $linein; print "\n"; chomp $linein; print " " . ord($_) foreach split //, $linein; print "\n";
    Short.txt
    ab 123
    Output:
    97 98 13 10 97 98 13

    PS: See the first line in chomp

      Thanks for that. I can now see that the chomp removes just the Ascii 10 character.
      However, my problem concerns writing a file so that when I read it back in there will not be the ASCII 13 character present.
      This is because a UNIX text file does not want the ASCII 13 character to be there.

        What you probably want to do is read the file NOT in binmode. If there are CRLFs, they'll get compacted and chomped as normal.

        Writing out, you want to put it in binmode so you get exactly the bytes you specify, and don't have your line endings re-expanded for you.

        This is because a UNIX text file does not want the ASCII 13 character to be there.
        Rubbish. UNIX doesn't care what you put in your files. If you want the ASCII 13 character to be there, write it to the file, and it will be there. If you don't want it to be there, don't write it. UNIX is as simple as that.