Thank you all for answering!

Regards

Ziping

> On Jun 16, 2018, at 1:13 PM, Elizabeth Mattijsen <l...@dijkmat.nl> wrote:
> 
>> On 16 Jun 2018, at 10:59, Xin Cheng <xinchen...@gmail.com> wrote:
>> I am wondering why the IO::Path class doesn't have a "close" method. After I 
>> read from a file by
>> 
>> $filename.IO.lines -> $line;
>> 
>> Am I supposed to close the file, or it is automatically closed for me after 
>> the reading?
> 
> If you read all of the lines from the file, then yes, the file will be closed 
> automatically for you.  This is *not* the case if you stop reading lines 
> after a while.  If you really want to be sure that the file will be closed in 
> that case, you will need to do that yourself by opening the file first, e.g. 
> like this:
> 
>    with open($filename) -> $handle {
>        LEAVE $handle.close;
>        for $handle.lines -> $line {
>            return if $line eq ‘foo’;  # will close
>        }
>    }
> 
> As soon as the scope of “with” is left, in whatever way, the file will be 
> closed by the LEAVE phaser.
> 
> 
> 
> Liz

Reply via email to