On Thu, 07 Jan 2016 12:50:43 -0800, pmqs wrote: > Given 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"; > spurt $filename, "abcdefg"; > > my $fh = open($filename, :r, :bin); > > for $fh.read(1024) -> $block { say $block } > > Actual output is > > > 97 > 98 > 99 > 100 > 101 > 102 > 103 > > 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> *read used to > > <TimToady> m: for ($*IN.read(1024),) -> $block { say $block.WHAT } > <camelia> rakudo-moar ed6ec7: OUTPUT«(Buf[uint8])» > <TimToady> it's calling .list on the buf
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} } }' Buf[uint8]:0x<61 62> Buf[uint8]:0x<63 64> Buf[uint8]:0x<65 66> Buf[uint8]:0x<67> $ perl6 -e 'with "/tmp/readtest".IO { .spurt: "abcdefg"; with .open: :bin -> $fh { react whenever $fh.Supply: :2size { .say } } }' Buf[uint8]:0x<61 62> Buf[uint8]:0x<63 64> Buf[uint8]:0x<65 66> Buf[uint8]:0x<67>