in reply to Unix \n vs. DOS \n

I believe the fellow at work used chop because he wanted to have Perl return the line-ending character; chomp seems to return only a result code and not the "chomped" character.

I created a four-line text file in which two lines had CR/LF line endings and the other two had LF-only line endings. Then, a small script that reads each line of the file. Following is the business end of it. (All lines in the file have "F" immediately before the line boundary.)

# TWO LINES IN THE FILE MEET THE FOLLOWING CRITERIA: print "ends CRLF\n" if /F\x0D\x0A$/; print "ends CRLF\n" if /F\r\n/; print "contains CR\n" if /\x0D/; print "contains CR\n" if /\r/; # AND THE OTHER TWO LINES IN THE FILE MATCH THIS: print "ends LF only\n" if /F\x0A$/;

But the script printed only this: ends LF only. It never did print ends CRLF or contains CR.

If perl doesn't make some internal translation of the carriage-return characters when it's reading a file, then why that result? Are the tests above not sufficient?

Replies are listed 'Best First'.
(chromatic) RE: Re: Unix \n vs. DOS \n
by chromatic (Archbishop) on Jul 17, 2000 at 01:51 UTC
    chomp returns the number of characters removed. It removes whatever's in $/, so he can just check that.

    Update: Yes, $/, as jlp pointed out.