txt: ## close.md
  close
  
  Combined from primary sources listed below.
  
  # [In IO::Socket::Async](#___top "go to top of document")[§](#(IO::Socket::Async)_method_close "direct link")
  
  See primary documentation
  [in context](/type/IO/Socket/Async#method_close)
  for **method close**.
  
  ```raku
  method close(IO::Socket::Async:D: )
  ```
  
  Close the connected client `IO::Socket::Async` which will have been obtained from the `listen` [`Supply`](/type/Supply) or the `connect` [`Promise`](/type/Promise).
  
  In order to close the underlying listening socket created by `listen` you can `close` the [`IO::Socket::Async::ListenSocket`](/type/IO/Socket/Async/ListenSocket). See [listen](#method_listen) for examples.
  
  # [In IO::Handle](#___top "go to top of document")[§](#(IO::Handle)_routine_close "direct link")
  
  See primary documentation
  [in context](/type/IO/Handle#routine_close)
  for **routine close**.
  
  ```raku
  method close(IO::Handle:D: --> Bool:D)
  multi  close(IO::Handle $fh)
  ```
  
  Closes an open filehandle, returning `True` on success. No error is thrown if the filehandle is already closed, although if you close one of the standard filehandles (by default: `$*IN`, `$*OUT`, or `$*ERR`: any handle with [native-descriptor](/routine/native-descriptor) `2` or lower), you won't be able to re-open them.
  
  ```raku
  given "foo/bar".IO.open(:w) {
      .spurt: "I ♥ Raku!";
      .close;
  }
  
  ```
  
  It's a common idiom to use [`LEAVE` phaser](/language/phasers#LEAVE) for closing the handles, which ensures the handle is closed regardless of how the block is left.
  
  ```raku
  do {
      my $fh = open "path-to-file";
      LEAVE close $fh;
      # ... do stuff with the file
  }
  
  sub do-stuff-with-the-file (IO $path-to-file) {
    my $fh = $path-to-file.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 $fh;
  
    # ... do stuff with the file
  }
  
  ```
  
  **Note:** unlike some other languages, Raku 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 `open` calls.
  
  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/IO/Path) type provides many reading and writing methods that let you work with files without dealing with filehandles directly.
  
  # [In role IO::Socket](#___top "go to top of document")[§](#(role_IO::Socket)_method_close "direct link")
  
  See primary documentation
  [in context](/type/IO/Socket#method_close)
  for **method close**.
  
  ```raku
  method close(IO::Socket:D)
  ```
  
  Closes the socket.
  
  Fails if the socket is not connected.
  
  # [In IO::CatHandle](#___top "go to top of document")[§](#(IO::CatHandle)_method_close "direct link")
  
  See primary documentation
  [in context](/type/IO/CatHandle#method_close)
  for **method close**.
  
  ```raku
  method close(IO::CatHandle:D: --> 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`](/type/IO/Handle), an explicit call to `.close` is often not necessary on an `IO::CatHandle`, as merely exhausting all the input closes all the handles that need to be closed.
  
  ```raku
  with IO::CatHandle.new: @bunch-of-handles {
      say .readchars: 42;
      .close; # we are done; close all the open handles
  }
  
  ```
  
  # [In Tap](#___top "go to top of document")[§](#(Tap)_method_close "direct link")
  
  See primary documentation
  [in context](/type/Tap#method_close)
  for **method close**.
  
  ```raku
  method close(Tap:D:)
  ```
  
  Closes the tap.
  
  # [In IO::Pipe](#___top "go to top of document")[§](#(IO::Pipe)_method_close "direct link")
  
  See primary documentation
  [in context](/type/IO/Pipe#method_close)
  for **method close**.
  
  ```raku
  method close(IO::Pipe: --> Proc:D)
  ```
  
  Closes the pipe and returns [`Proc`](/type/Proc) object from which the pipe originates.
  
  # [In Channel](#___top "go to top of document")[§](#(Channel)_method_close "direct link")
  
  See primary documentation
  [in context](/type/Channel#method_close)
  for **method close**.
  
  ```raku
  method close(Channel:D:)
  ```
  
  Close the `Channel`, normally. This makes subsequent `send` calls die with [`X::Channel::SendOnClosed`](/type/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`](/type/X/Channel/ReceiveOnClosed) exception. Since you can produce a [`Seq`](/type/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](/language/concurrency#whenever)-block will also terminate properly on a closed `Channel`.
  
  ```raku
  my $c = Channel.new;
  $c.close;
  $c.send(1);
  CATCH { default { put .^name, ': ', .Str } };
  # 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](/language/phasers#LEAVE) phaser to enforce the `.close` call in this case.
  