Documentation for routine
close assembled from the following types:
Channel, normally. This makes subsequent
send calls die with X::Channel::SendOnClosed. Subsequent calls of
.receive may still drain any remaining items that were previously sent, but if the queue is empty, will throw an X::Channel::ReceiveOnClosed exception. Since you can produce a
Seq from a Channel by contextualizing to array with
@() or by calling the
.list method, these methods will not terminate until the channel has been closed. A whenever-block will also terminate properly on a closed channel.
my = Channel.new;.close;.send(1);CATCH ;# OUTPUT: «X::Channel::SendOnClosed: Cannot send a message on a closed channel␤»
Please note that any exception thrown may prevent
.close from being called, this may hang the receiving thread. Use a LEAVE phaser to enforce the
.close call in this case.
Closes the tap.
method close(IO::Pipe: --> Proc)
Closes the pipe and returns Proc object from which the pipe originates.
method close(IO::CatHandle: --> True)
Closes the currently active source handle, as well as any already-open source handles, and empties the source handle queue. Unlike a regular IO::Handle, an explicit call to
.close is often not necessary on a CatHandle, as merely exhausting all the input closes all the handles that need to be closed.
method close(IO::Handle: --> Bool)multi sub close(IO::Handle )
Closes an open filehandle. It's not an error to call
close on an already-closed filehandle. Returns
True on success. If you close one of the standard filehandles (by default:
$*ERR), that is any handle with native-descriptor
2 or lower, you won't be able to re-open such a handle.
It's a common idiom to use
LEAVE phaser for closing the handles, which ensures the handle is closed regardless of how the block is left.
ifsub do-stuff-with-the-file (IO )my = .open;# stick a `try` on it, since this will get run even when the sub is# called with wrong arguments, in which case the `$fh` will be an `Any`LEAVE try close ;# ... do stuff with the file}given "foo/bar".IO.open(:w)
Note: unlike some other languages, Perl 6 does not use reference counting, and so the filehandles are NOT closed when they go out of scope. While they will get closed when garbage collected, garbage collection isn't guaranteed to get run. This means you must use an explicit
close on handles opened for writing, to avoid data loss, and an explicit
close is recommended on handles opened for reading as well, so that your program does not open too many files at the same time, triggering exceptions on further
Note several methods allow for providing
:close argument, to close the handle after the operation invoked by the method completes. As a simpler alternative, the IO::Path type provides many reading and writing methods that let you work with files without dealing with filehandles directly.
Closes the socket.
Fails if the socket is not connected.
In order to close the underlying listening socket created by
listen you can
close the Tap. See
listen for examples.