This:
my $f = $fh.lines;
will slurp all the lines into $f (but you can still access the individual
items with something like $f[4]).

So you don't want to use this in a while loop, since everything will be
consumed during the first loop iteration. Either use a for loop to process
the lines one by one (as shown in my previous answer), and the for loop
will stop once you've read the whole file, and this is probably the way to
go if your file is large:
for "test.txt".IO.lines -> $c { say $c }
or possibly
.say for "text.txt".IO.lines;
or you can dump all the lines into an array (and later process the array):
my @f =  "test.txt".IO.lines;


Le mar. 9 oct. 2018 à 14:49, ToddAndMargo via perl6-users <
perl6-users@perl.org> a écrit :

> 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-users@perl.org <mailto:perl6-users@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
>

Reply via email to