On 10/9/18 5:42 AM, Fernando Santagata wrote:
The answer Laurent Roseenfeld gave you works for read and readchars as well.
Save the following lines in a file and run it (try and change .read into
.readchars too); it will output a series of 10-byte long Buf[uint8]s,
until it reaches the end of file.

#!/usr/bin/env perl6
given $*PROGRAM-NAME.IO.open {
    while my $bytes = .read: 10 {
      $bytes.say;
    }
}

On Tue, Oct 9, 2018 at 10:17 AM ToddAndMargo via perl6-users
<perl6-us...@perl.org <mailto:perl6-us...@perl.org>> wrote:

     On 10/9/18 1:02 AM, ToddAndMargo via perl6-users wrote:
      > Hi All,
      >
      > When reading a text file
      > https://docs.perl6.org/routine/lines
      > seems pretty straight forward.
      >
      > Question:  How do I tell when I when I have
      > reached the EOF (End Of File)?
      >
      > Many thanks,
      > -T

     Please expand the question to include `read` and `readchars`.



--
Fernando Santagata

Hi Frenando,

Thank you for the help!

I am not getting anywhere with `.lines`.  Read the whole thing in the
first line.

$ p6 'my $fh=open "/home/linuxutil/WhoIsMySub.pl6", :r;  while my $f =
$fh.lines { say "$f\n"}; $fh.close;'

#!/usr/bin/env perl6  sub f() { put &?ROUTINE.gist; };  sub abc () {
say "This subroutine's ID is ", f;     print "\n";      &?ROUTINE.gist
~~ m/' '(.*?)' '\(/;      my $SubName = $0;     say "This subroutine is
called $SubName"; }  abc;

-T


On 10/9/18 7:03 AM, Brad Gilbert wrote:
That isn't the syntax for a loop local variable in Perl 6.

You are trying to use the Perl 5 syntax, which is not going to work in Perl 6

This is the Perl 5 code you are trying to write

     while( my $f = readline $fh ){ say "$f\n"}

Which actually would turn into the following by Perl 5 compiler

     while( defined( my $f = readline $fh ) ){ say "$f\n"}

If you want something that works the same in Perl 6:

     while $fh.get -> $f { say $f }

---

The reason you use `while` in Perl 5 is to prevent `for` from reading
the entire file before looping.

     # Perl 5
     # reads in entire file before doing any work on it
     for my $f (readline $fh){ say $f }
     # (the reason is that readline is in list context)

This is not a problem in Perl 6

     # Perl 6
     # loops over a Seq which only reads enough data to get the next line
     for $fh.lines -> $f { say $f }
On Tue, Oct 9, 2018 at 7:49 AM ToddAndMargo via perl6-users
<perl6-us...@perl.org> wrote:

Hi Brad,

That explains it.  Thank you!

-T

Reply via email to