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 | |
by stevieb (Canon) on May 31, 2016 at 13:45 UTC | |
by raiph (Deacon) on May 31, 2016 at 16:51 UTC | |
by msh210 (Monk) on May 31, 2016 at 16:31 UTC | |
by stevieb (Canon) on May 31, 2016 at 16:41 UTC | |
by Laurent_R (Canon) on May 31, 2016 at 16:00 UTC | |
by stevieb (Canon) on May 31, 2016 at 16:43 UTC | |
by Laurent_R (Canon) on May 31, 2016 at 22:19 UTC | |
| |
|
Re: Perl6: Capturing newline in a string
by johngg (Canon) on May 30, 2016 at 23:14 UTC |