Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using .lines on generated data is wrong (traps) #1472

Closed
AlexDaniel opened this issue Aug 27, 2017 · 5 comments
Closed

Using .lines on generated data is wrong (traps) #1472

AlexDaniel opened this issue Aug 27, 2017 · 5 comments
Labels
docs Documentation issue (primary issue type) trap

Comments

@AlexDaniel
Copy link
Member

Let's make a file:

perl6 -e 'spurt ‘file’, “foox\nfooy\rbar\nfooz\n”'

It is fair to say (on linux at least) that this file has three lines:

foox
fooy\rbar
fooz

And any proper command line tool in existence will treat it as such. wc -l says 3, grep foo will give the whole line also (your terminal may print it in a weird way, but that's still treated as a whole line).

So let's try reading this file in perl 6:

perl6 -e 'dd $_ for lines' < file
"foox"
"fooy\rbar"
"fooz"

↑ This is entirely right.

But here's a trap:

perl6 -e 'dd $_ for slurp.lines' < file
"foox"
"fooy"
"bar"
"fooz"

It turns out that lines and slurp.lines work differently, which is really surprising. Also, I managed to get into this trap at least once.

@AlexDaniel AlexDaniel added the docs Documentation issue (primary issue type) label Aug 27, 2017
@AlexDaniel
Copy link
Member Author

AlexDaniel commented Aug 27, 2017

Also note that Proc::Async.stdout.lines is totally useless in this regard.

my $proc = Proc::Async.new: cat, file; # same file as in OP
react {
    whenever $proc.stdout.lines { dd $_ }
    whenever $proc.start { done }
}
"foox"
"fooy"
"bar"
"fooz"

So if you're using Proc::Async, you're doomed. There's RT #131923 which asks for customizable nl-in or split, but we don't have that yet.

@AlexDaniel
Copy link
Member Author

From our docs:

If :translate-nl is set to True (default value), OS-specific newline terminators (e.g. \r\n on Windows) will be automatically translated to \n.

Let's see if it helps.

my $proc = Proc::Async.new: cat, file, :!translate-nl; # same file as in OP
react {
    whenever $proc.stdout.lines { dd $_ }
    whenever $proc.start { done }
}
"foox"
"fooy"
"bar"
"fooz"

Nope.

@zoffixznet
Copy link
Contributor

Is this issue mistitled?

I don't quite understand where the wrongness of calling .lines lies.

AlexDaniel added a commit that referenced this issue Sep 1, 2017
AlexDaniel added a commit that referenced this issue Sep 1, 2017
This should make it closable.
@AlexDaniel
Copy link
Member Author

Mistitled or not, I think this is now resolved.

@zoffixznet
Copy link
Contributor

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation issue (primary issue type) trap
Projects
None yet
Development

No branches or pull requests

2 participants