I've finally got something that works. It's very sketchy and p6-newbish, but it does the job. Obviously there is a LOT more I need to learn, but I'm content that I've got a proof-of-concept done. The following code was written/tested on a FreeBSD system.

anonymonk above was right; :buf, Buf was the key here.

se v6; use experimental :pack; my $fn = 'in.txt'; my $outfile = 'out.txt'; # write a new file with win32 endings my $fh = open $fn, :w; $fh.print("ab\r\ndef\r\n"); $fh.close; # re-open the file for reading $fh = open $fn, :bin; my $eol_found = False; my Str $recsep = ''; # read one byte at a time, as we have no way to tell end of # line from EOF, and we don't want to slurp the whole thing while $fh.read(1) -> $buf { my $hex = $buf.unpack("H*"); if $hex ~~ /(0d|0a)/ { $eol_found = True; $recsep = $recsep ~ $hex; next; } if $eol_found { if $hex !~~ /(0d|0a)/ { last; } } } $fh.close; my %recseps = ( '0d0a' => "\r\n", '0d' => "\r", '0a' => "\n", ); my $nl = %recseps<<$recsep>>; # open a new file for writing with our new recsep $fh = open $outfile, :w; $fh.print('a' ~ $nl); $fh.close; # re-read the outfile and see if our saved recsep # was written $fh = open $outfile, :bin; my $buf = $fh.read(1000); say $buf; __END__ Buf[uint8]:0x<61 0d 0a>

Now I'll go back to reading the docs and practicing what I'm learning. I'm sure within a single day I can clean that code up a whole heck of a lot :)

After I'm more familiar with Perl6, I'll go back to attempting to convert my module.

Thanks for all the feedback here!


In reply to Re^9: Perl6: Capturing newline in a string by stevieb
in thread 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.