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

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

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) :)

Replies are listed 'Best First'.
Re^3: Perl6: Capturing newline in a string
by raiph (Deacon) on May 31, 2016 at 16:51 UTC
    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)
Re^3: Perl6: Capturing newline in a string
by msh210 (Monk) on May 31, 2016 at 16:31 UTC

    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.

      Nice :)

      Without looking yet, this is because it grabs the first ending it finds (LF, ie. the line ending at the end of the first line in the file), and uses that for all subsequent lines.

      I'll have to put some thought into whether I should review the code to see if there's a sane way to handle this, or update the docs with a warning.

      I can't see too many use cases out there where line endings would be mixed in a single file, but I digress.

      Cheers!

Re^3: Perl6: Capturing newline in a string
by Laurent_R (Canon) on May 31, 2016 at 16:00 UTC
    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.

      Thank you very much :)

      Let me know if you have any questions (or suggestions/feedback).

        Hi stevieb,

        Just a bit of feedback. I have tried a number of things and combinations (such as using the :bin and chomp => false options, using the lines, slurp, slurp-rest and get functions or methods), etc., but to no avail. Even when requested to keep new lines, Perl 6 is apparently converting \r\n sequences to \n on reading a Windows file, so that "un-chomped" lines end with \x0A (or ASCII 10), whether they come from a Unix or a Windows file. There doesn't seem to be a possibility to distinguish between EOL conventions this way. Or, at least, I haven't found any.

        Not a good news, and unfortunately not very helpful, but I thought I should let you know about my tests.