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

(I'm using ActiveState's perl port on a Win XP machine.)

I've opened and am using read FH, $nextchr, 1 to read one character at a time from a data file.

I've looked at the data file from within a hex editor and the lines are definately termined with 0x0D followed by 0x0A - but when the read gets to the relevant place it only reads one 0x0A and there is no 0x0D to be seen.

Is there some kind of weirdness to do with how the machinery underneath read goes about dealing with carriage returns and new lines on different platforms?

-Andrew Tomazos <andrew@tomazos.com>

Replies are listed 'Best First'.
Re: read and EOL weirdness on win32
by eyepopslikeamosquito (Archbishop) on Jul 28, 2004 at 09:23 UTC
    On Windows, to read files in binary mode, immediately after opening the file (with open or sysopen), you must use the binmode functon. For example:
    open(FH, $fname) or die "error: open '$fname': $!"; binmode(FH); # ...
Re: read and EOL weirdness on win32
by BUU (Prior) on Jul 28, 2004 at 10:10 UTC
    To elaborate slightly, in win32, lines are terminated with \r\n. So when perl, on win32, wants to read a line, it reads until it finds a \r\n pair and translates it to a \n when your script sees it. This is because \n in perl is a masical character that gets translated to whatever the appropiate EOL for that platform is. As the poster above noted, binmode changes this so you read exactly what is written.