Skip to content

Commit 2387ce3

Browse files
committed
[io grant] Re-write IO::Handle.close docs
1 parent 4074006 commit 2387ce3

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

doc/Type/IO.pod6

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -364,28 +364,6 @@ C<.lines> and C<.get>. Defaults to C<True>.
364364
my $fh = open("path-to-file", chomp => False);
365365
say $fh.get(); # returns line including newline char
366366
367-
=head2 method close
368-
369-
To close an open file handle, simply call its
370-
L<close|/type/IO::Handle#method_close> method:
371-
372-
my $fh = open("path-to-file");
373-
# ... do stuff with the file
374-
$fh.close;
375-
376-
It is also possible to call this as a sub, thus the example above can be
377-
written equivalently like so:
378-
379-
my $fh = open("path-to-file");
380-
# ... do stuff with the file
381-
close $fh;
382-
383-
When a file was opened for writing, closing it is important to ensure that all
384-
contents are actually written to the file. You may want to consider using a
385-
L<LEAVE|/language/phasers#LEAVE> phaser to guard against exceptions in
386-
your code that could prevent your program from reaching the line with
387-
C<$fh.close>.
388-
389367
=head2 sub slurp
390368
391369
Slurps the contents of the entire file into a C<Str> (or C<Buf> if C<:bin>).

doc/Type/IO/Handle.pod6

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,48 @@ to L<Str>.
333333
334334
=head2 method close
335335
336-
Will close a previously opened filehandle.
336+
Defined as:
337+
338+
method close(IO::Handle:D: --> Bool:D)
339+
multi sub close(IO::Handle $fh)
340+
341+
Closes an open file handle. It's not an error to call C<close> on an
342+
already-closed filehandle. Returns C<True> on success.
343+
344+
It's a common idiom to use L«C<LEAVE> phaser|/language/phasers#LEAVE» for
345+
closing the handles, which ensures the handle is closed regardless of how the
346+
block is left.
337347
338348
=for code :skip-test
339-
$fh.close;
349+
if $do-stuff-with-the-file {
350+
my $fh = open "path-to-file";
351+
LEAVE close $fh;
352+
# ... do stuff with the file
353+
}
354+
355+
sub do-stuff-with-the-file (IO $path-to-file)
356+
my $fh = $path-to-file.open;
357+
358+
# stick a `try` on it, since this will get run even when the sub is
359+
# called with wrong arguments, in which case the `$fh` will be an `Any`
360+
LEAVE try close $fh;
361+
362+
# ... do stuff with the file
363+
}
364+
365+
with "foo/bar".IO.open(:w) {
366+
.spurt: "I ♥ Perl 6!";
367+
.close;
368+
}
369+
370+
B<Note:> unlike some other languages, Perl 6 does not use reference counting,
371+
and so B<the file handles are NOT closed when they go out of scope>. They
372+
I<will> get closed when garbage collected, however an explicit C<close> is
373+
recommended, so that your program does not open too many files at the same time,
374+
triggering exceptions on further opens.
375+
376+
As a simpler alternative, the L<IO::Path> type provides many methods that let
377+
you work with files without dealing with file handles directly.
340378
341379
=head2 method flush
342380

0 commit comments

Comments
 (0)