eof ?

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
0
perl6
10/9/2018 8:02:42 AM
perl.perl6.users 1111 articles. 0 followers. Follow

15 Replies
69 Views

Similar Articles

[PageSpeed] 40

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

Please expand the question to include `read` and `readchars`.
0
perl6
10/9/2018 8:17:06 AM
--0000000000008855b60577ca2cb7
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

The eof method of the IO::Handle class returns True if you exhausted the
contents of the handle, but you generally don't need to use that, since
something like:

for 'input.txt'.IO.lines -> $line {
    # Do something with $line
}

will gracefully handle ends of files for you without you having to do
anything special.



Le mar. 9 oct. 2018 =C3=A0 10:03, ToddAndMargo via perl6-users <
perl6-users@perl.org> a =C3=A9crit :

> 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
>

--0000000000008855b60577ca2cb7
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>The eof method of the IO::Handle class returns True i=
f you=20
exhausted the contents of the handle, but you generally don&#39;t need to u=
se that, since something like:</div><div><br></div><div>
<pre class=3D"editor editor-colors"><div class=3D"gmail-line"><span class=
=3D"gmail-source gmail-perl6fe"><span class=3D"gmail-keyword gmail-control =
gmail-repeat gmail-perl6fe"><span>for</span></span><span>=C2=A0</span><span=
 class=3D"gmail-string gmail-quoted gmail-single gmail-single gmail-perl6fe=
"><span class=3D"gmail-punctuation gmail-definition gmail-string gmail-begi=
n gmail-perl6fe"><span>&#39;</span></span><span>input.txt</span><span class=
=3D"gmail-punctuation gmail-definition gmail-string end gmail-perl6fe"><spa=
n>&#39;</span></span></span><span class=3D"gmail-keyword gmail-operator gma=
il-generic gmail-perl6fe"><span>.</span></span><span class=3D"gmail-support=
 gmail-type gmail-perl6fe"><span>IO</span></span><span class=3D"gmail-keywo=
rd gmail-operator gmail-generic gmail-perl6fe"><span>.</span></span><span c=
lass=3D"gmail-support gmail-function gmail-perl6fe"><span>lines</span></spa=
n><span>=C2=A0</span><span class=3D"gmail-keyword gmail-operator gmail-mult=
i-symbol gmail-perl6fe"><span>-&gt;</span></span><span>=C2=A0</span><span c=
lass=3D"gmail-meta gmail-variable gmail-container gmail-perl6fe"><span clas=
s=3D"gmail-variable gmail-other gmail-identifier gmail-sigil gmail-perl6fe"=
><span>$</span></span><span class=3D"gmail-variable gmail-other gmail-ident=
ifier gmail-perl6fe"><span>line</span></span></span><span>=C2=A0</span><spa=
n class=3D"gmail-meta gmail-block gmail-perl6fe"><span class=3D"gmail-punct=
uation gmail-definition gmail-block gmail-perl6fe"><span>{</span></span></s=
pan></span></div><div class=3D"gmail-line"><span class=3D"gmail-source gmai=
l-perl6fe"><span class=3D"gmail-meta gmail-block gmail-perl6fe"><span class=
=3D"gmail-punctuation gmail-whitespace gmail-comment gmail-leading gmail-pe=
rl6fe"><span>=C2=A0=C2=A0=C2=A0=C2=A0</span></span><span class=3D"gmail-com=
ment gmail-line gmail-number-sign gmail-perl6fe"><span class=3D"gmail-punct=
uation gmail-definition gmail-comment gmail-perl6fe"><span>#</span></span><=
span>=C2=A0Do=C2=A0something=C2=A0with=C2=A0$line</span><span>=C2=A0</span>=
</span></span></span></div><div class=3D"gmail-line"><span class=3D"gmail-s=
ource gmail-perl6fe"><span class=3D"gmail-meta gmail-block gmail-perl6fe"><=
span class=3D"gmail-punctuation gmail-definition gmail-block gmail-perl6fe"=
><span>}</span></span></span></span></div><div class=3D"gmail-line"><span c=
lass=3D"gmail-source gmail-perl6fe"><span class=3D"gmail-meta gmail-block g=
mail-perl6fe"><span class=3D"gmail-punctuation gmail-definition gmail-block=
 gmail-perl6fe"><span><br></span></span></span></span></div><div class=3D"g=
mail-line"><span style=3D"font-family:arial,helvetica,sans-serif"><span cla=
ss=3D"gmail-source gmail-perl6fe"><span class=3D"gmail-meta gmail-block gma=
il-perl6fe"><span class=3D"gmail-punctuation gmail-definition gmail-block g=
mail-perl6fe"><span>will gracefully handle ends of files for you without yo=
u having to do anything special.</span></span></span></span></span></div><d=
iv class=3D"gmail-line"><span class=3D"gmail-source gmail-perl6fe"><span cl=
ass=3D"gmail-meta gmail-block gmail-perl6fe"><span class=3D"gmail-punctuati=
on gmail-definition gmail-block gmail-perl6fe"><span><br></span></span></sp=
an></span></div><div class=3D"gmail-line"><span class=3D"gmail-source gmail=
-perl6fe"><span class=3D"gmail-meta gmail-block gmail-perl6fe"><span class=
=3D"gmail-punctuation gmail-definition gmail-block gmail-perl6fe"><span><br=
></span></span></span></span></div></pre>



</div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr">Le=C2=A0mar. 9 =
oct. 2018 =C3=A0=C2=A010:03, ToddAndMargo via perl6-users &lt;<a href=3D"ma=
ilto:perl6-users@perl.org">perl6-users@perl.org</a>&gt; a =C3=A9crit=C2=A0:=
<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;bord=
er-left:1px #ccc solid;padding-left:1ex">Hi All,<br>
<br>
When reading a text file<br>
=C2=A0 =C2=A0 =C2=A0<a href=3D"https://docs.perl6.org/routine/lines" rel=3D=
"noreferrer" target=3D"_blank">https://docs.perl6.org/routine/lines</a><br>
seems pretty straight forward.<br>
<br>
Question:=C2=A0 How do I tell when I when I have<br>
reached the EOF (End Of File)?<br>
<br>
Many thanks,<br>
-T<br>
</blockquote></div>

--0000000000008855b60577ca2cb7--
0
perl6
10/9/2018 11:38:06 AM
>> Le mar. 9 oct. 2018 =C3=A0 10:03, ToddAndMargo via perl6-users=20
>> <perl6-users@perl.org <mailto:perl6-users@perl.org>> a =C3=A9crit :
>>=20
>>     Hi All,
>>=20
>>     When reading a text file
>>     https://docs.perl6.org/routine/lines
>>     seems pretty straight forward.
>>=20
>>     Question:  How do I tell when I when I have
>>     reached the EOF (End Of File)?
>>=20
>>     Many thanks,
>>     -T
>>=20

On 10/9/18 4:38 AM, Laurent Rosenfeld via perl6-users wrote:
> The eof method of the IO::Handle class returns True if you exhausted th=
e=20
> contents of the handle, but you generally don't need to use that, since=
=20
> something like:
>=20
> for'input.txt'.IO.lines->$line{
> #=C2=A0Do=C2=A0something=C2=A0with=C2=A0$line
> }
>=20
> will gracefully handle ends of files for you without you having to do=20
> anything special.

Hi Laurent,

Thank you!

So loop until lines return false?

Is it just me, or is this not documented on

      https://docs.perl6.org/routine/lines
?

-T
0
perl6
10/9/2018 12:03:33 PM
--00000000000066861d0577cb11a5
Content-Type: text/plain; charset="UTF-8"

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> 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

--00000000000066861d0577cb11a5
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div cl=
ass=3D"gmail_default" style=3D"color:rgb(0,0,0)">The answer Laurent Roseenf=
eld gave you works for read and readchars as well.</div><div class=3D"gmail=
_default" style=3D"color:rgb(0,0,0)">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.<br></div><d=
iv class=3D"gmail_default" style=3D"color:rgb(0,0,0)"></div><div class=3D"g=
mail_default" style=3D"color:rgb(0,0,0)"><br></div><div class=3D"gmail_defa=
ult" style=3D"color:rgb(0,0,0)">#!/usr/bin/env perl6</div><div class=3D"gma=
il_default" style=3D"color:rgb(0,0,0)">given $*PROGRAM-NAME.IO.open {<br>=
=C2=A0 while my $bytes =3D .read: 10 {<br>=C2=A0=C2=A0=C2=A0 $bytes.say;<br=
>=C2=A0 }<br>}<br></div></div></div></div></div><br><div class=3D"gmail_quo=
te"><div dir=3D"ltr">On Tue, Oct 9, 2018 at 10:17 AM ToddAndMargo via perl6=
-users &lt;<a href=3D"mailto:perl6-users@perl.org">perl6-users@perl.org</a>=
&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 =
0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 10/9/18 1:02 AM, Tod=
dAndMargo via perl6-users wrote:<br>
&gt; Hi All,<br>
&gt; <br>
&gt; When reading a text file<br>
&gt;=C2=A0 =C2=A0=C2=A0=C2=A0 <a href=3D"https://docs.perl6.org/routine/lin=
es" rel=3D"noreferrer" target=3D"_blank">https://docs.perl6.org/routine/lin=
es</a><br>
&gt; seems pretty straight forward.<br>
&gt; <br>
&gt; Question:=C2=A0 How do I tell when I when I have<br>
&gt; reached the EOF (End Of File)?<br>
&gt; <br>
&gt; Many thanks,<br>
&gt; -T<br>
<br>
Please expand the question to include `read` and `readchars`.<br>
</blockquote></div><br clear=3D"all"><br>-- <br><div dir=3D"ltr" class=3D"g=
mail_signature" data-smartmail=3D"gmail_signature">Fernando Santagata</div>

--00000000000066861d0577cb11a5--
0
nando
10/9/2018 12:42:04 PM
On 10/9/18 5:03 AM, ToddAndMargo via perl6-users wrote:
>>> Le mar. 9 oct. 2018 =C3=A0 10:03, ToddAndMargo via perl6-users=20
>>> <perl6-users@perl.org <mailto:perl6-users@perl.org>> a =C3=A9crit :
>>>
>>> =C2=A0=C2=A0=C2=A0 Hi All,
>>>
>>> =C2=A0=C2=A0=C2=A0 When reading a text file
>>> =C2=A0=C2=A0=C2=A0 https://docs.perl6.org/routine/lines
>>> =C2=A0=C2=A0=C2=A0 seems pretty straight forward.
>>>
>>> =C2=A0=C2=A0=C2=A0 Question:=C2=A0 How do I tell when I when I have
>>> =C2=A0=C2=A0=C2=A0 reached the EOF (End Of File)?
>>>
>>> =C2=A0=C2=A0=C2=A0 Many thanks,
>>> =C2=A0=C2=A0=C2=A0 -T
>>>
>=20
> On 10/9/18 4:38 AM, Laurent Rosenfeld via perl6-users wrote:
>> The eof method of the IO::Handle class returns True if you exhausted=20
>> the contents of the handle, but you generally don't need to use that, =

>> since something like:
>>
>> for'input.txt'.IO.lines->$line{
>> #=C2=A0Do=C2=A0something=C2=A0with=C2=A0$line
>> }
>>
>> will gracefully handle ends of files for you without you having to do =

>> anything special.
>=20
> Hi Laurent,
>=20
> Thank you!
>=20
> So loop until lines return false?
>=20
> Is it just me, or is this not documented on
>=20
>  =C2=A0=C2=A0=C2=A0=C2=A0 https://docs.perl6.org/routine/lines
> ?
>=20
> -T


Where in the following would I put a echo of the True / False
return from `lines`?

$ p6 'my $fh=3Dopen "/home/linuxutil/WhoIsMySub.pl6", :r;  for $fh.lines =
{=20
say $_}; $fh.close;'
0
perl6
10/9/2018 12:44:08 PM
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 int=
o=20
> .readchars too); it will output a series of 10-byte long Buf[uint8]s,=20
> until it reaches the end of file.
>=20
> #!/usr/bin/env perl6
> given $*PROGRAM-NAME.IO.open {
>  =C2=A0 while my $bytes =3D .read: 10 {
>  =C2=A0=C2=A0=C2=A0 $bytes.say;
>  =C2=A0 }
> }
>=20
> On Tue, Oct 9, 2018 at 10:17 AM ToddAndMargo via perl6-users=20
> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>=20
>     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:=C2=A0 How do I tell when I when I have
>      > reached the EOF (End Of File)?
>      >
>      > Many thanks,
>      > -T
>=20
>     Please expand the question to include `read` and `readchars`.
>=20
>=20
>=20
> --=20
> Fernando Santagata

Hi Frenando,

Thank you for the help!

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

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

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

-T
0
perl6
10/9/2018 12:48:50 PM
--000000000000e6bbb30577cba078
Content-Type: text/plain; charset="UTF-8"

On Tue, Oct 9, 2018 at 8:49 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

>
> 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;'
>

..lines() creates Seq (https://docs.perl6.org/type/Seq) that is a lazy list
of all the lines in the file.

Your while loop calls it once, which makes that Seq, Inside your first
loop, which prints it, the Stringify iterates the Seq, printing all the
lines.
The second call to .lines() returns nothing because you're now at eof, no
more lines.

Note the Seq that .lines returns itself is lazy.  It doesn't (necessarily)
have all the lines, just the capability to get them.
As you iterate the Seq, it pulls in all the lines.

Curt

--000000000000e6bbb30577cba078
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><br><div class=3D"gmail_quote"><div dir=
=3D"ltr">On Tue, Oct 9, 2018 at 8:49 AM ToddAndMargo via perl6-users &lt;<a=
 href=3D"mailto:perl6-users@perl.org">perl6-users@perl.org</a>&gt; wrote:</=
div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;bor=
der-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
I am not getting anywhere with `.lines`.=C2=A0 Read the whole thing in the =
<br>
first line.<br>
<br>
$ p6 &#39;my $fh=3Dopen &quot;/home/linuxutil/WhoIsMySub.pl6&quot;, :r;=C2=
=A0 while my $f =3D <br>
$fh.lines { say &quot;$f\n&quot;}; $fh.close;&#39;<br></blockquote><div><br=
></div><div>.lines() creates Seq (<a href=3D"https://docs.perl6.org/type/Se=
q">https://docs.perl6.org/type/Seq</a>) that is a lazy list of all the line=
s in the file.</div><div><br></div><div>Your while loop calls it once, whic=
h makes that Seq, Inside your first loop, which prints it, the Stringify it=
erates the Seq, printing all the lines.</div><div>The second call to .lines=
() returns nothing because you&#39;re now at eof, no more lines.</div><div>=
<br></div><div>Note the Seq that .lines returns itself is lazy.=C2=A0 It do=
esn&#39;t (necessarily) have all the lines, just the capability to get them=
..</div><div>As you iterate the Seq, it pulls in all the lines.</div><div><b=
r></div><div>Curt</div><div><br></div></div></div></div>

--000000000000e6bbb30577cba078--
0
curt
10/9/2018 1:22:12 PM
--000000000000d5bc280577cbb05a
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

This:
my $f =3D $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 =3D  "test.txt".IO.lines;


Le mar. 9 oct. 2018 =C3=A0 14:49, ToddAndMargo via perl6-users <
perl6-users@perl.org> a =C3=A9crit :

> 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 int=
o
> > .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 =3D .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=3Dopen "/home/linuxutil/WhoIsMySub.pl6", :r;  while my $f =
=3D
> $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 =3D $0;     say "This subroutine is
> called $SubName"; }  abc;
>
> -T
>

--000000000000d5bc280577cbb05a
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div>This:</div><div>
my $f =3D $fh.lines;</div><div>will slurp all the lines into $f (but you ca=
n still access the individual items with something like $f[4]).</div><div><=
br></div><div>So you don&#39;t want to use this in a while loop, since ever=
ything will be consumed during the first loop iteration. Either use a for l=
oop to process the lines one by one (as shown in my previous answer), and t=
he for loop will stop once you&#39;ve read the whole file, and this is prob=
ably the way to go if your file is large:</div><div>for &quot;test.txt&quot=
;.IO.lines -&gt; $c { say $c }</div><div>or possibly<br></div><div>.say for=
 &quot;text.txt&quot;.IO.lines;<br></div><div> or you can dump all the line=
s into an array (and later process the array):</div><div>
my @f =3D=C2=A0
&quot;test.txt&quot;.IO.lines;

</div><div><br>

</div></div></div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr">Le=
=C2=A0mar. 9 oct. 2018 =C3=A0=C2=A014:49, ToddAndMargo via perl6-users &lt;=
<a href=3D"mailto:perl6-users@perl.org">perl6-users@perl.org</a>&gt; a =C3=
=A9crit=C2=A0:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0=
 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 10/9/18 5:42 AM, =
Fernando Santagata wrote:<br>
&gt; The answer Laurent Roseenfeld gave you works for read and readchars as=
 well.<br>
&gt; Save the following lines in a file and run it (try and change .read in=
to <br>
&gt; .readchars too); it will output a series of 10-byte long Buf[uint8]s, =
<br>
&gt; until it reaches the end of file.<br>
&gt; <br>
&gt; #!/usr/bin/env perl6<br>
&gt; given $*PROGRAM-NAME.IO.open {<br>
&gt;=C2=A0 =C2=A0 while my $bytes =3D .read: 10 {<br>
&gt;=C2=A0 =C2=A0=C2=A0=C2=A0 $bytes.say;<br>
&gt;=C2=A0 =C2=A0 }<br>
&gt; }<br>
&gt; <br>
&gt; On Tue, Oct 9, 2018 at 10:17 AM ToddAndMargo via perl6-users <br>
&gt; &lt;<a href=3D"mailto:perl6-users@perl.org" target=3D"_blank">perl6-us=
ers@perl.org</a> &lt;mailto:<a href=3D"mailto:perl6-users@perl.org" target=
=3D"_blank">perl6-users@perl.org</a>&gt;&gt; wrote:<br>
&gt; <br>
&gt;=C2=A0 =C2=A0 =C2=A0On 10/9/18 1:02 AM, ToddAndMargo via perl6-users wr=
ote:<br>
&gt;=C2=A0 =C2=A0 =C2=A0 &gt; Hi All,<br>
&gt;=C2=A0 =C2=A0 =C2=A0 &gt;<br>
&gt;=C2=A0 =C2=A0 =C2=A0 &gt; When reading a text file<br>
&gt;=C2=A0 =C2=A0 =C2=A0 &gt; <a href=3D"https://docs.perl6.org/routine/lin=
es" rel=3D"noreferrer" target=3D"_blank">https://docs.perl6.org/routine/lin=
es</a><br>
&gt;=C2=A0 =C2=A0 =C2=A0 &gt; seems pretty straight forward.<br>
&gt;=C2=A0 =C2=A0 =C2=A0 &gt;<br>
&gt;=C2=A0 =C2=A0 =C2=A0 &gt; Question:=C2=A0 How do I tell when I when I h=
ave<br>
&gt;=C2=A0 =C2=A0 =C2=A0 &gt; reached the EOF (End Of File)?<br>
&gt;=C2=A0 =C2=A0 =C2=A0 &gt;<br>
&gt;=C2=A0 =C2=A0 =C2=A0 &gt; Many thanks,<br>
&gt;=C2=A0 =C2=A0 =C2=A0 &gt; -T<br>
&gt; <br>
&gt;=C2=A0 =C2=A0 =C2=A0Please expand the question to include `read` and `r=
eadchars`.<br>
&gt; <br>
&gt; <br>
&gt; <br>
&gt; -- <br>
&gt; Fernando Santagata<br>
<br>
Hi Frenando,<br>
<br>
Thank you for the help!<br>
<br>
I am not getting anywhere with `.lines`.=C2=A0 Read the whole thing in the =
<br>
first line.<br>
<br>
$ p6 &#39;my $fh=3Dopen &quot;/home/linuxutil/WhoIsMySub.pl6&quot;, :r;=C2=
=A0 while my $f =3D <br>
$fh.lines { say &quot;$f\n&quot;}; $fh.close;&#39;<br>
<br>
#!/usr/bin/env perl6=C2=A0 sub f() { put &amp;?ROUTINE.gist; };=C2=A0 sub a=
bc () { <br>
say &quot;This subroutine&#39;s ID is &quot;, f;=C2=A0 =C2=A0 =C2=A0print &=
quot;\n&quot;;=C2=A0 =C2=A0 =C2=A0 &amp;?ROUTINE.gist <br>
~~ m/&#39; &#39;(.*?)&#39; &#39;\(/;=C2=A0 =C2=A0 =C2=A0 my $SubName =3D $0=
;=C2=A0 =C2=A0 =C2=A0say &quot;This subroutine is <br>
called $SubName&quot;; }=C2=A0 abc;<br>
<br>
-T<br>
</blockquote></div>

--000000000000d5bc280577cbb05a--
0
perl6
10/9/2018 1:26:40 PM
--000000000000df79f10577cbbe7c
Content-Type: text/plain; charset="UTF-8"

On Tue, Oct 9, 2018 at 9:27 AM Laurent Rosenfeld via perl6-users <
perl6-users@perl.org> wrote:

> 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]).
>

Is that true?  I supposed that it would hold the Seq as a scalar
(un-iterated) Seq.

I know that my @f = $fh.lines will slurp it all, but I thought you could
avoid that by assigning it to a scalar.

Curt

--000000000000df79f10577cbbe7c
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_quote"><div dir=3D"ltr">On Tue, Oct 9,=
 2018 at 9:27 AM Laurent Rosenfeld via perl6-users &lt;<a href=3D"mailto:pe=
rl6-users@perl.org">perl6-users@perl.org</a>&gt; wrote:<br></div><blockquot=
e class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc sol=
id;padding-left:1ex"><div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><di=
v>This:</div><div>
my $f =3D $fh.lines;</div><div>will slurp all the lines into $f (but you ca=
n still access the individual items with something like $f[4]).</div></div>=
</div></div></blockquote><div><br></div><div>Is that true?=C2=A0 I supposed=
 that it would hold the Seq as a scalar (un-iterated) Seq.</div><div><br></=
div><div>I know that my=C2=A0@f =3D $fh.lines will slurp it all, but I thou=
ght you could avoid that by assigning it to a scalar.</div><div><br></div><=
div>Curt</div><div>=C2=A0</div></div></div>

--000000000000df79f10577cbbe7c--
0
curt
10/9/2018 1:30:35 PM
--00000000000097d0380577cbdfae
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Yes, you're right, it is a Seq. I was trying to be pedagogical, but
probably wasn't very accurate. It is a Seq, and the "slurping" will be lazy=
..



Le mar. 9 oct. 2018 =C3=A0 15:30, Curt Tilmes <curt@tilmes.org> a =C3=A9cri=
t :

> On Tue, Oct 9, 2018 at 9:27 AM Laurent Rosenfeld via perl6-users <
> perl6-users@perl.org> wrote:
>
>> This:
>> my $f =3D $fh.lines;
>> will slurp all the lines into $f (but you can still access the individua=
l
>> items with something like $f[4]).
>>
>
> Is that true?  I supposed that it would hold the Seq as a scalar
> (un-iterated) Seq.
>
> I know that my @f =3D $fh.lines will slurp it all, but I thought you coul=
d
> avoid that by assigning it to a scalar.
>
> Curt
>
>

--00000000000097d0380577cbdfae
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Yes, you&#39;re right, it is a Seq. I was trying to b=
e pedagogical, but probably wasn&#39;t very accurate. It is a Seq, and the =
&quot;slurping&quot; will be lazy.</div><div><br></div><div><br></div></div=
><br><div class=3D"gmail_quote"><div dir=3D"ltr">Le=C2=A0mar. 9 oct. 2018 =
=C3=A0=C2=A015:30, Curt Tilmes &lt;<a href=3D"mailto:curt@tilmes.org">curt@=
tilmes.org</a>&gt; a =C3=A9crit=C2=A0:<br></div><blockquote class=3D"gmail_=
quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1=
ex"><div dir=3D"ltr"><div class=3D"gmail_quote"><div dir=3D"ltr">On Tue, Oc=
t 9, 2018 at 9:27 AM Laurent Rosenfeld via perl6-users &lt;<a href=3D"mailt=
o:perl6-users@perl.org" target=3D"_blank">perl6-users@perl.org</a>&gt; wrot=
e:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;bo=
rder-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div dir=3D"ltr=
"><div dir=3D"ltr"><div>This:</div><div>
my $f =3D $fh.lines;</div><div>will slurp all the lines into $f (but you ca=
n still access the individual items with something like $f[4]).</div></div>=
</div></div></blockquote><div><br></div><div>Is that true?=C2=A0 I supposed=
 that it would hold the Seq as a scalar (un-iterated) Seq.</div><div><br></=
div><div>I know that my=C2=A0@f =3D $fh.lines will slurp it all, but I thou=
ght you could avoid that by assigning it to a scalar.</div><div><br></div><=
div>Curt</div><div>=C2=A0</div></div></div>
</blockquote></div>

--00000000000097d0380577cbdfae--
0
perl6
10/9/2018 1:39:45 PM
On Tue, Oct 9, 2018 at 8:31 AM Curt Tilmes <curt@tilmes.org> wrote:
>
> On Tue, Oct 9, 2018 at 9:27 AM Laurent Rosenfeld via perl6-users <perl6-users@perl.org> wrote:
>>
>> 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]).
>
>
> Is that true?  I supposed that it would hold the Seq as a scalar (un-iterated) Seq.

No it isn't true.
It stores the Seq in $f.

The following doesn't print anything.

    my $f = 'example.txt'.IO.lines.map(*.say); Nil

(Note that I added `Nil` so that if someone tried this in the REPL, it
doesn't try to `say` what's in `$f`.)

What it does do is make the Seq marked as an item, so this only runs
one loop, and doesn't run the `.map` code.

    for $f { Nil }

But both of these will:

    for @$f { Nil }
    for $f<> { Nil }


> I know that my @f = $fh.lines will slurp it all, but I thought you could avoid that by assigning it to a scalar.
>
> Curt
>
0
b2gills
10/9/2018 1:40:58 PM
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-users@perl.org> wrote:
>
> 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
0
b2gills
10/9/2018 2:03:26 PM
On 10/9/18 6:22 AM, Curt Tilmes wrote:
>=20
> On Tue, Oct 9, 2018 at 8:49 AM ToddAndMargo via perl6-users=20
> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>=20
>=20
>     I am not getting anywhere with `.lines`.=C2=A0 Read the whole thing=
 in the
>     first line.
>=20
>     $ p6 'my $fh=3Dopen "/home/linuxutil/WhoIsMySub.pl6", :r;=C2=A0 whi=
le my $f =3D
>     $fh.lines { say "$f\n"}; $fh.close;'
>=20
>=20
> .lines() creates Seq (https://docs.perl6.org/type/Seq) that is a lazy=20
> list of all the lines in the file.
>=20
> Your while loop calls it once, which makes that Seq, Inside your first =

> loop, which prints it, the Stringify iterates the Seq, printing all the=
=20
> lines.
> The second call to .lines() returns nothing because you're now at eof, =

> no more lines.
>=20
> Note the Seq that .lines returns itself is lazy.=C2=A0 It doesn't=20
> (necessarily) have all the lines, just the capability to get them.
> As you iterate the Seq, it pulls in all the lines.
>=20
> Curt
>=20

Hi Curt,

I was thinking that .lines read them one at a time.

Thank you!

-T
--=20
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
perl6
10/9/2018 11:46:37 PM
>> Le mar. 9 oct. 2018 =C3=A0 14:49, ToddAndMargo via perl6-users=20
>> <perl6-users@perl.org <mailto:perl6-users@perl.org>> a =C3=A9crit :
>>=20
>>     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 =3D .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>
>>     <mailto: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
>>=20
>>     Hi Frenando,
>>=20
>>     Thank you for the help!
>>=20
>>     I am not getting anywhere with `.lines`.  Read the whole thing in =
the
>>     first line.
>>=20
>>     $ p6 'my $fh=3Dopen "/home/linuxutil/WhoIsMySub.pl6", :r;  while m=
y $f =3D
>>     $fh.lines { say "$f\n"}; $fh.close;'
>>=20
>>     #!/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 =3D $0;     say "This subrou=
tine is
>>     called $SubName"; }  abc;
>>=20
>>     -T
>>=20

On 10/9/18 6:26 AM, Laurent Rosenfeld via perl6-users wrote:
> This:
> my $f =3D $fh.lines;
> will slurp all the lines into $f (but you can still access the=20
> individual items with something like $f[4]).
>=20
> So you don't want to use this in a while loop, since everything will be=
=20
> consumed during the first loop iteration. Either use a for loop to=20
> 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 probabl=
y=20
> 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 arra=
y):
> my @f =3D "test.txt".IO.lines;

My misunderstanding of .lines.  Thank you!
0
perl6
10/9/2018 11:47:24 PM
>>>
>>> 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


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

Hi Brad,

That explains it.  Thank you!

-T
0
perl6
10/9/2018 11:48:49 PM
Reply: