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?


In reply to Perl6: Capturing newline in a string by stevieb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.