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

Disclaimer: cross-posted at StackOverflow.

In Perl5, I can quickly and easily print out the hex representation of the \r\n Windows-style line ending:

perl -nE '/([\r\n]{1,2})/; print(unpack("H*",$1))' in.txt 0d0a

update:To create a Windows-ending file on Unix, create a in.txt file with a single line and line ending. Then: perl -ni -e 's/\n/\r\n/g;print' in.txt. (or in vi/vim, create the file and just do :set ff=dos) /update

I have tried many things in Perl6 to do the same thing, but I can't get it to work no matter what I do. Here's my most recent test:

use v6; use experimental :pack; my $fn = 'in.txt'; my $fh = open $fn, chomp => False; # I've also tried :bin for $fh.lines -> $line { if $line ~~ /(<[\r\n]>**1..2)/ { $0.Str.encode('UTF-8').unpack("H*").say; } }

Outputs 0a, as do:

/(\n)/ /(\v)/

First, I don't even know if I'm using unpack() properly. Second, how do I capture both elements (\r\n) of the newline in P6?

Replies are listed 'Best First'.
Re: Perl6: Capturing newline in a string
by Laurent_R (Canon) on May 31, 2016 at 07:30 UTC
    Hm, not sure to fully understand what you are trying to do, but please note that the Perl 6 .lines method strips the newline characters.

    Under Unix:

    $ perl6 -e 'lines("a\nb").perl;' ("a", "b").Seq
    Under Windows:
    C:\Users\Laurent>perl6 -e "dd qq{a\nb\r\nc}.lines;" ("a", "b", "c").Seq

      Here's an example of what I'm trying to do:

      • open a Unix file on Windows (or vise-versa)
      • read in the file contents regardless of line-endings (works with lines)
      • write back to the file (either write or append), using the SAME line endings that were originally found in the file, as opposed to clobbering the file with the wrong endings

      For instance, if I start with a Unix file with one line and a unixy \n on a Windows system, then append to the file, I get one line with a nix ending, and one with a windows ending:

      10 10^M

      This kind of thing happens in both P5 and P6.

      I wrote a module that handles this in Perl 5, File::Edit::Portable. To learn about Perl6, I thought I'd port this module over, but the most fundamental aspect of figuring out what style of endings a file has seems to be a bit difficult in Perl6 :)

      Perhaps I should have stated what X was instead of asking about Y (XY Problem) :)

        I just noticed your SO has some answers. Perhaps those give you the info you need and what follows is redundant but maybe not, and I'd already written this, so:

        ETA: This bit from Sinan's post:

        We don't do any such translation when using .encode/.decode, and of course when reading/writing Bufs to files, providing an escape hatch from translation if needed.

        reads to me like a very direct claim that there's a way to read and write files without translating newlines.

        Hth.

        perl6.party (not mine)

        Unrelated to your original query, but your module doesn't do quite what it says on the tin. You may wish to emend the documentation.

        lines : foo<LF>bar<CR><LF>baz

        $ perl -e'use File::Edit::Portable;$r=File::Edit::Portable->new;$h=$r->read("lines");$t=$r->tempfile;print$t $_ while<$h>;$r->write(contents=>$t)'

        According to spec, lines should be the same after I ran that as before. In fact it was foo<LF>bar<LF>baz<LF> afterward.

        That's a nice way to learn about Perl 6. :-)

        I'll take a look at your module and see if I can have an idea which may help you.

        Update: fixed a typo.

Re: Perl6: Capturing newline in a string
by johngg (Canon) on May 30, 2016 at 23:14 UTC

    I haven't had a play with Perl 6 yet so can't help there. There is another way to create a Windows-ending file though.

    $ perl -Mstrict -Mwarnings -E ' open my $outFH, q{>:crlf}, \ my $outFile or die $!; say $outFH q{Hi}; close $outFH or die $!; say sprintf q{%02x}, ord for split m{}, $outFile;' 48 69 0d 0a $

    Update: The code above was run on Linux Mint 17 with the perl 5.18.2 interpreter.

    Cheers,

    JohnGG