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

I'm revisiting line endings on both Windows and Linux, and found something that I'm not sure whether it's a bug or if I've overlooked something.

This is in relation to the \R match character in perlrebackslash.

Using this code (note that win.txt is definitely a Windows-ending file):

perl -nE '/(\R)/; say (unpack "H*", $1);' win.txt

...on many variants of Unix, produces:

0d0a

...which I expect. However, on Windows, it prints:

0a

...on literally the exact same file. In fact, I've even set up a VirtualBox shared directory between a Windows VM and a Linux host, and put the file there, as well as testing on a copy. The results are the same.

I've tested perl v5.18 and 5.22 on nix, and Strawberry v5.22 on Windows.

Shouldn't Windows perl also print 0d0a or am I missing something? If this isn't the correct behaviour, could I get one of our Monks who use ActiveState perl test as well?

Update: note that interestingly, I get the same result (0a on win and 0d0a on nix) when I change the regex to /(\s+$)/, so if this is a problem, it isn't only related to \R. Also, on Windows, /\r\n/ does match.

Replies are listed 'Best First'.
Re: \R not working as (I) expect on Windows Strawberry perl
by choroba (Cardinal) on Sep 26, 2015 at 17:53 UTC
    On MSWin, all filehandles are opened with the :crlf layer by default. Therefore, the $_ coming from <> doesn't contain the \x0d anymore. To get the Unix behaviour, do
    binmode FH, ':raw';

    Update: I wasn't able to make this work with -n and filename argument - somehow, you have to change the bimode of an open filehandle, but -n opens it and reads from it at the same time or something (anyone?). Therefore, I had to write the loop myself:

    perl -wE 'use open IN => ":raw"; while (<>) { /(\R)/; say (unpack "H*" +, $1); }' file

    Update 2: -M works, thanks Anonymous Monk:

    perl -Mopen=IN,:raw -wnE '/(\R)/; say unpack "H*", $1' file
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Thanks choroba, that indeed fixed it. I'll have a good read of open and many things associated with it (such as filers) today.

      Cheers,

      -stevieb