in reply to Writing a UNIX text line - the end character challenge

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

Replies are listed 'Best First'.
Re^2: Writing a UNIX text line - the end character challenge
by merrymonk (Hermit) on Aug 11, 2010 at 13:39 UTC
    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.

        That did it!!
        No binmode on input but binmode on output.
        Many thanks
      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.