develooper Front page | perl.perl6.users | Postings from June 2018

Re: IO ???

Thread Previous | Thread Next
From:
Xin Cheng
Date:
June 16, 2018 17:45
Subject:
Re: IO ???
Message ID:
87F1D8D9-ACB2-4F31-A4CE-7AAD06996735@gmail.com
Thank you all for answering!

Regards

Ziping

> On Jun 16, 2018, at 1:13 PM, Elizabeth Mattijsen <liz@dijkmat.nl> wrote:
> 
>> On 16 Jun 2018, at 10:59, Xin Cheng <xincheng99@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


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About