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
for $fh.read(1024) -> $blob #5016
Comments
From @pmqsGiven this can be used to walk though a file for $fh.lines -> $line I assumed I would be able to iterate through a file using "read" like this my $filename = "/tmp/readtest"; my $fh = open($filename, :r, :bin); for $fh.read(1024) -> $block { say $block } Actual output is 97 Alternative is to use while $fh.read(1024) -> $block { say $block } IRC Dialog <TimToady> I think used to force a binary interpretation of a filehandle, but maybe that broke <TimToady> m: for ($*IN.read(1024),) -> $block { say $block.WHAT } |
From @smlsUnless I'm missing something, there is no bug here. Here's how each example works: for $fh.lines -> $line { ... } The .lines method is only called once, and returns a lazy sequence of lines (represented as a Seq object). The `for` loop then iterates this sequence. for $fh.read(1024) -> $byte { ... } The .read method is only called once, and returns a binary buffer (represented as a Buf object). The `for` loop then iterates this buffer. It so happens that iterating a Buf iterates over its bytes, each represented as a number. while $fh.read(1024) -> $block { ... } The .read method is called repeatedly, once for each iteration of the while loop. Each iteration gets a Buf object. The while loop stops when `read` returns an empty Buf (which evaluates to False in boolean context). |
The RT System itself - Status changed from 'new' to 'open' |
From @zoffixznetOn Thu, 07 Jan 2016 12:50:43 -0800, pmqs wrote:
Thanks for the report, but this is not a bug. .lines returns a Seq object with *all* the lines of the file, while .read reads a chunk (in rakudo, default size is 65536) as a Buf object. Your .lines code then iterates over each line, while the `for...read` code iterates over each element of that Buf object. If you wanted to get creative, you could construct your own Seq from multiple .read()s or use the .Supply method, but the `while` approach is a more appropriate construct: $ perl6 -e 'with "/tmp/readtest".IO { .spurt: "abcdefg"; with .open: :bin -> $fh { for {$fh.read(2)} …^ !+* {.say} } }' $ perl6 -e 'with "/tmp/readtest".IO { .spurt: "abcdefg"; with .open: :bin -> $fh { react whenever $fh.Supply: :2size { .say } } }' |
@zoffixznet - Status changed from 'open' to 'rejected' |
Migrated from rt.perl.org#127204 (status was 'rejected')
Searchable as RT127204$
The text was updated successfully, but these errors were encountered: