in reply to Re^5: Perl6: Capturing newline in a string
in thread Perl6: Capturing newline in a string

It's very helpful Laurent_R, thanks! Combined with what raiph said in Re^3: Perl6: Capturing newline in a string that I've reviewed, I'm thinking this is a bug (or at least a function that's lacking... I can't see me being the only person who will want to identify which endings a file has, as perl6 takes off).

There should be a way to get this information, or else there's no way to ensure the proper line endings get replaced (by proper I mean the original ones) when the endings aren't that of the local platform.

I'm going to do a bit more testing and review. If I can't figure anything out, I'll open a ticket.

  • Comment on Re^6: Perl6: Capturing newline in a string

Replies are listed 'Best First'.
Re^7: Perl6: Capturing newline in a string
by Anonymous Monk on Jun 01, 2016 at 05:21 UTC
    So don't use .lines and instead open(:bin) parse the Buf like you normally would. It's really pretty simple if you quit trying to use a higher level convenience method to do this
      I'd be glad i you could show a working example. From my (very limited) tests, even that does not appear to work. But perhaps I failed to use the right function.

        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!