Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Perl6: Capturing newline in a string

by Laurent_R (Canon)
on May 31, 2016 at 07:30 UTC ( [id://1164550]=note: print w/replies, xml ) Need Help??


in reply to Perl6: Capturing newline in a string

Hm, not sure to fully understand what you are trying to do, but please note that the Perl 6 .lines method strips the newline characters.

Under Unix:

$ perl6 -e 'lines("a\nb").perl;' ("a", "b").Seq
Under Windows:
C:\Users\Laurent>perl6 -e "dd qq{a\nb\r\nc}.lines;" ("a", "b", "c").Seq

Replies are listed 'Best First'.
Re^2: Perl6: Capturing newline in a string
by stevieb (Canon) on May 31, 2016 at 13:45 UTC

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

    • open a Unix file on Windows (or vise-versa)
    • read in the file contents regardless of line-endings (works with lines)
    • write back to the file (either write or append), using the SAME line endings that were originally found in the file, as opposed to clobbering the file with the wrong endings

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

      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)

      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!

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1164550]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-25 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found