Stevan Little writes: > Hello all, > > I am not even 100% sure these are bugs. I assume I should be able to do > this, but I dont know for sure. > > These two test groups (taken from t/builtins/io/io_in_for_loop.t) below > will fail with the error: > > pugs: tempfile: hGetLine: illegal operation (handle is closed) > > it seems that accessing the filehandle in the for loop is a bad thing. > > { # now read it in with the $fh controling the loop but call > # the =$fh inside the loop inside parens (is this list context??) > my $fh = open($filename); > my $num = 1; > for (=$fh) -> $line { > is($line, "$num\n", '... got the right line ((=$fh) controlled > loop)'); > $num++; > my $line2 = =$fh; > is($line2, "$num\n", '... got the right line2 ((=$fh) > controlled loop)'); > $num++; > } > $fh.close(); > } > > { # now read it in with the $fh controling the loop but call > # the =$fh inside the loop w/out parens (is this scalar context??) > my $fh = open($filename); > my $num = 1; > for =$fh -> $line { > is($line, "$num\n", '... got the right line (=$fh controlled > loop)'); > $num++; > my $line2 = =$fh; > is($line2, "$num\n", '... got the right line2 (=$fh controlled > loop)'); > $num++; > } > $fh.close(); > }
Hmm... I believe that the behavior in this case is undefined. It sure would be nice if it worked, but you see: for =$fh -> $line { ... } Evaluates `=$fh` in list context. In an eager world, this eats up the whole file in one go. In the ideal lazy world, it reads one line at a time (which would allow this example to work... I think). And perhaps that's how we need to define it. But currently, whether this works depends on how lazy $fh is, and whether it does any thunks (thunks are bad in this case). Luke