class IO::Pipe

Buffered inter-process string or binary stream

class IO::Pipe is IO::Handle {}

An IO::Pipe object closely corresponds to a UNIX pipe. It has one end where it consumes string or binary data, and another where it reproduces the same data. It is buffered, so that a write without a read doesn't immediately block.

Pipes can be easily constructed with sub run and Proc::Async.new.

Methods

method close

Defined as:

method close(IO::Pipe: --> Proc:D)

Closes the pipe and returns Proc object from which the pipe originates.

method IO

Defined as:

method IO(IO::Pipe: --> IO::Path:U)

Returns an IO::Path type object.

method path

Defined as:

method path(IO::Pipe: --> IO::Path:U)

Returns an IO::Path type object.

method proc

Defined as:

method proc(IO::Pipe: --> Proc:D)

Returns the Proc object from which the pipe originates.

Type Graph

Type relations for IO::Pipe
perl6-type-graph IO::Pipe IO::Pipe IO::Handle IO::Handle IO::Pipe->IO::Handle Mu Mu Any Any Any->Mu IO::Handle->Any

Stand-alone image: vector

Routines supplied by class IO::Handle

IO::Pipe inherits from class IO::Handle, which provides the following routines:

(IO::Handle) method open

Defined as:

method open(IO::Handle:D:
    :$bin:$enc:$chomp:$nl-inStr:D :$nl-out,
    Str :$mode,
    :$r:$w:$a:$x:$update:$rw:$rx:$ra,
    :$create:$append:$truncate:$exclusive,
    :$out-buffer,
    --> IO::Handle:D
)

Opens the handle in one of the modes. Fails with appropriate exception if the open fails.

See description of individual methods for the accepted values and behaviour of :$chomp, :$nl-in, :$nl-out, and :$enc. The values for parameters default to the invocant's attributes and if any of them are provided, the attributes will be updated to the new values. Specify :$bin set to True instead of :$enc to indicate the handle should be opened in binary mode. Specifying undefined value as :$enc is equivalent to not specifying :$enc at all. Specifying both a defined encoding as :$enc and :$bin set to true will cause X::IO::BinaryAndEncoding exception to be thrown.

The open mode defaults to non-exclusive, read only (same as specifying :r) and can be controlled by a mix of the following arguments:

:r      same as specifying   :mode<ro>  same as specifying nothing
 
:w      same as specifying   :mode<wo>:create:truncate
:a      same as specifying   :mode<wo>:create:append
:x      same as specifying   :mode<wo>:create:exclusive
 
:update same as specifying   :mode<rw>
:rw     same as specifying   :mode<rw>:create
:ra     same as specifying   :mode<rw>:create:append
:rx     same as specifying   :mode<rw>:create:exclusive

Support for combinations of modes other than what is listed above is implementation-dependent and should be assumed unsupported. That is, specifying, for example, .open(:r :create) or .open(:mode<wo> :append :truncate) might work or might cause the Universe to implode, depending on a particular implementation. This applies to reads/writes to a handle opened in such unsupported modes as well.

The mode details are:

:mode<ro>  means "read only"
:mode<wo>  means "write only"
:mode<rw>  means "read and write"
 
:create    means the file will be createdif it does not exist
:truncate  means the file will be emptiedif it exists
:exclusive means .open will fail if the file already exists
:append    means writes will be done at the end of file's current contents

Attempts to open a directory, write to a handle opened in read-only mode or read from a handle opened in write-only mode, or using text-reading methods on a handle opened in binary mode will fail or throw.

In 6.c language, it's possible to open path '-', which will cause open to open (if closed) the $*IN handle if opening in read-only mode or to open the $*OUT handle if opening in write-only mode. All other modes in this case will result in exception being thrown.

In future language versions, path '-' will likely have no special meaning and relying on this behaviour in new programs is not recommended.

The :out-buffer controls output buffering and by default behaves as if it were Nil. See method out-buffer for details.

Note (Rakudo versions before 2017.09): Filehandles are NOT flushed or 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 should 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 (Rakudo versions 2017.09 and after): Open filehandles are automatically closed on program exit, but it is still highly recommended that you close opened handles explicitly.

(IO::Handle) method comb

Defined as:

method comb(IO::Handle:D: Bool :$close|args --> Seq:D)

Read the handle and processes its contents the same way Str.comb does, taking the same arguments, closing the handle when done if $close is set to a true value. Implementations may slurp the file in its entirety when this method is called.

Attempting to call this method when the handle is in binary mode will result in X::IO::BinaryMode exception being thrown.

my $fh = 'path/to/file'.IO.open;
say "The file has {+$fh.comb: '':close} ♥s in it";

(IO::Handle) method chomp

Defined as:

has $.chomp is rw = True

One of the attributes that can be set via .new or open. Defaults to True. Takes a Bool specifying whether the line separators (as defined by .nl-in) should be removed from content when using .get or .lines methods.

(IO::Handle) routine get

Defined as:

method get(IO::Handle:D: --> Str:D)
multi sub get (IO::Handle $fh = $*ARGFILES --> Str:D)

Reads a single line of input from the handle, removing the trailing newline characters (as set by .nl-in) if the handle's .chomp attribute is set to True. Returns Nil, if no more input is available. The subroutine form defaults to $*ARGFILES if no handle is given.

Attempting to call this method when the handle is in binary mode will result in X::IO::BinaryMode exception being thrown.

$*IN.get.say;              # Read one line from the standard input 
 
my $fh = open 'filename';
$fh.get.say;               # Read one line from a file 
$fh.close;
 
say get;                   # Read one line from $*ARGFILES 

(IO::Handle) routine getc

Defined as:

method getc(IO::Handle:D: --> Str:D)
multi sub getc (IO::Handle $fh = $*ARGFILES --> Str:D)

Reads a single character from the input stream. Attempting to call this method when the handle is in binary mode will result in X::IO::BinaryMode exception being thrown. The subroutine form defaults to $*ARGFILES if no handle is given. Returns Nil, if no more input is available, otherwise operation will block, waiting for at least one character to be available; these caveats apply:

Buffering terminals

Using getc to get a single keypress from a terminal will only work properly if you've set the terminal to "unbuffered". Otherwise the terminal will wait for the return key to be struck or the buffer to be filled up before perl6 gets even a single byte of data.

Waiting for potential combiners

If your handle's encoding allows combining characters to be read, perl6 will wait for more data to be available before it provides a character. This means that inputting an "e" followed by a combining acute will give you an e with an acute rather than giving an "e" and letting the next reading function give you a dangling combiner. However, it also means that when the user inputs just an "e" and has no intention to also input a combining acute, your program will be waiting for another keypress before the initial "e" is returned.

(IO::Handle) submethod DESTROY

Defined as:

submethod DESTROY(IO::Handle:D:)

Closes the filehandle, unless its native-descriptor is 2 or lower. This ensures the standard filehandles do not get inadvertently closed.

Note that garbage collection is not guaranteed to happen, so you must NOT rely on DESTROY for closing the handles you write to and instead close them yourself. Programs that open a lot of files should close the handles explicitly as well, regardless of whether they were open for writing, since too many files might get opened before garbage collection happens and the no longer used handles get closed.

(IO::Handle) method gist

Defined as:

method gist(IO::Handle:D: --> Str:D)

Returns a string containing information which .path, if any, the handle is created for and whether it is .opened.

say IO::Handle.new# IO::Handle<(Any)>(closed) 
say "foo".IO.open;  # IO::Handle<"foo".IO>(opened) 

(IO::Handle) method eof

Defined as:

method eof(IO::Handle:D: --> Bool:D)

Non-blocking. Returns True if the read operations have exhausted the contents of the handle. For seekable handles, this means current position is at or beyond the end of file and seeking an exhausted handle back into the file's contents will result in eof returning False again.

On non-seekable handles and handles opened to zero-size files (including special files in /proc/), EOF won't be set until a read operation fails to read any bytes. For example, in this code, the first read consumes all of the data, but it's only until the second read that reads nothing would the EOF on a TTY handle be set:

$ echo "x" | perl6 -e 'with $*IN { .read: 10000; .eof.say; .read: 10; .eof.say }'
False
True

(IO::Handle) method encoding

Defined as:

multi method encoding(IO::Handle:D: --> Str:D)
multi method encoding(IO::Handle:D: $enc --> Str:D)

Returns a Str representing the encoding currently used by the handle, defaulting to "utf8". Nil indicates the filehandle is currently in binary mode. Specifying an optional positional $enc argument switches the encoding used by the handle; specify Nil as encoding to put the handle into binary mode.

The accepted values for encoding are case-insensitive. The available encodings vary by implementation and backend. On Rakudo MoarVM the following are supported:

utf8
utf16
utf8-c8
iso-8859-1
windows-1251
windows-1252
windows-932
ascii

The default encoding is utf8, which undergoes normalization into Unicode NFC (normalization form canonical). In some cases you may want to ensure no normalization is done; for this you can use utf8-c8. Before using utf8-c8 please read Unicode: Filehandles and I/O for more information on utf8-c8 and NFC.

As of Rakudo 2018.04 windows-932 is also supported which is a variant of ShiftJIS.

Implementation may choose to also provide support for aliases, e.g. Rakudo allows aliases latin-1 for iso-8859-1 encoding and dashed utf versions: utf-8 and utf-16.

with 'foo'.IO {
    .spurt: "First line is text, then:\nBinary";
    my $fh will leave {.close} = .open;
    $fh.get.say;         # OUTPUT: «First line is text, then:␤» 
    $fh.encoding: Nil;
    $fh.slurp.say;       # OUTPUT: «Buf[uint8]:0x<42 69 6e 61 72 79>␤» 
}

(IO::Handle) routine lines

Defined as:

sub lines(IO::Handle:D $fh = $*ARGFILES$limit = Inf:$close --> Seq:D)
method lines(IO::Handle:D:               $limit = Inf:$close --> Seq:D)

Return a Seq each element of which is a line from the handle (that is chunks delineated by .nl-in). If the handle's .chomp attribute is set to True, then characters specified by .nl-in will be stripped from each line.

Reads up to $limit lines, where $limit can be a non-negative Int, Inf, or Whatever (which is interpreted to mean Inf). If :$close is set to True, will close the handle when the file ends or $limit is reached. Subroutine form defaults to $*ARGFILES, if no handle is provided.

Attempting to call this method when the handle is in binary mode will result in X::IO::BinaryMode exception being thrown.

NOTE: the lines are read lazily, so ensure the returned Seq is either fully reified or is no longer needed when you close the handle or attempt to use any other method that changes the file position.

say "The file contains ",
  '50GB-file'.IO.open.lines.grep(*.contains: 'Perl').elems,
  " lines that mention Perl";
# OUTPUT: «The file contains 72 lines that mention Perl␤» 

(IO::Handle) method lock

Defined as:

method lock(IO::Handle:D: Bool:D :$non-blocking = FalseBool:D :$shared = False --> True)

Places an advisory lock on the filehandle. If :$non-blocking is True will fail with X::IO::Lock if lock could not be obtained, otherwise will block until the lock can be placed. If :$shared is True will place a shared (read) lock, otherwise will place an exclusive (write) lock. On success, returns True; fails with X::IO::Lock if lock cannot be placed (e.g. when trying to place a shared lock on a filehandle opened in write mode or trying to place an exclusive lock on a filehandle opened in read mode).

You can use lock again to replace an existing lock with another one. To remove a lock, close the filehandle or use unlock.

# One program writes, the other reads, and thanks to locks either 
# will wait for the other to finish before proceeding to read/write 
 
# Writer 
given "foo".IO.open(:w{
    .lock;
    .spurt: "I ♥ Perl 6!";
    .close;
}
 
# Reader 
given "foo".IO.open {
    .lock: :shared;
    .slurp.say# OUTPUT: «I ♥ Perl 6!␤» 
    .close;
}

(IO::Handle) method unlock

Defined as:

method unlock(IO::Handle:D: --> True)

Removes a lock from the filehandle.

(IO::Handle) routine words

Defined as:

multi sub words(IO::Handle:D $fh = $*ARGFILES$limit = Inf:$close --> Seq:D)
multi method words(IO::Handle:D: $limit = Inf:$close --> Seq:D)

Similar to Str.words, separates the handle's stream on contiguous chunks of whitespace (as defined by Unicode) and returns a Seq of the resultant "words." Takes an optional $limit argument that can be a non-negative Int, Inf, or Whatever (which is interpreted to mean Inf), to indicate only up-to $limit words must be returned. If Bool :$close named argument is set to True, will automatically close the handle when the returned Seq is exhausted. Subroutine form defaults to $*ARGFILES, if no handle is provided.

Attempting to call this method when the handle is in binary mode will result in X::IO::BinaryMode exception being thrown.

my %dict := bag $*IN.words;
say "Most common words: "%dict.sort(-*.value).head: 5;

NOTE: implementations may read more data than necessary when a call to .words is made. That is, $handle.words(2) may read more data than two "words" worth of data and subsequent calls to read methods might not read from the place right after the two fetched words. After a call to .words, the file position should be treated as undefined.

(IO::Handle) method split

Defined as:

method split(IO::Handle:D: :$close|c)

Slurps the handle's content and calls Str.split on it, forwarding any of the given arguments. If :$close named parameter is set to True, will close the invocant after slurping.

Attempting to call this method when the handle is in binary mode will result in X::IO::BinaryMode exception being thrown.

my $fh = 'path/to/file'.IO.open;
$fh.split: '':close# Returns file content split on ♥ 

(IO::Handle) method spurt

Defined as:

multi method spurt(IO::Handle:D: Blob $data:$close = False)
multi method spurt(IO::Handle:D: Cool $data:$close = False)

Writes all of the $data into the filehandle, closing it when finished, if $close is True. For Cool $data, will use the encoding the handle is set to use (IO::Handle.open or IO::Handle.encoding).

Behaviour for spurting a Cool when the handle is in binary mode or spurting a Blob when the handle is NOT in binary mode is undefined.

(IO::Handle) method print

Defined as:

multi method print(**@text --> True)
multi method print(Junction:D --> True)

Writes the given @text to the handle, coercing any non-Str objects to Str by calling .Str method on them. Junction arguments autothread and the order of printed strings is not guaranteed. See write to write bytes.

Attempting to call this method when the handle is in binary mode will result in X::IO::BinaryMode exception being thrown.

my $fh = 'path/to/file'.IO.open: :w;
$fh.print: 'some text';
$fh.close;

(IO::Handle) method print-nl

Defined as:

method print-nl(IO::Handle:D: --> True)

Writes the value of $.nl-out attribute into the handle. This attribute, by default, is , but see the page on newline for the rules it follows in different platforms and environments.

Attempting to call this method when the handle is in binary mode will result in X::IO::BinaryMode exception being thrown.

my $fh = 'path/to/file'.IO.open: :w:nl-out("\r\n");
$fh.print: "some text";
$fh.print-nl# prints \r\n 
$fh.close;

(IO::Handle) method printf

Defined as:

multi method printf(IO::Handle:D: Cool $format*@args)

Formats a string based on the given format and arguments and .prints the result into the filehandle. See sub sprintf for details on acceptable format directives.

Attempting to call this method when the handle is in binary mode will result in X::IO::BinaryMode exception being thrown.

my $fh = open 'path/to/file':w;
$fh.printf: "The value is %d\n"32;
$fh.close;

(IO::Handle) method out-buffer

Defined as:

method out-buffer(--> Int:Dis rw

Controls output buffering and can be set via an argument to open. Takes an int as the size of the buffer to use (zero is acceptable). Can take a Bool: True means to use default, implementation-defined buffer size; False means to disable buffering (equivalent to using 0 as buffer size).

Lastly, can take a Nil to enable TTY-based buffering control: if the handle is a TTY, the buffering is disabled, otherwise, default, implementation-defined buffer size is used.

See flush to write out data currently in the buffer. Changing buffer size flushes the filehandle.

given 'foo'.IO.open: :w:1000out-buffer {
    .say: 'Hello world!'# buffered 
    .out-buffer = 42;       # buffer resized; previous print flushed 
    .say: 'And goodbye';
    .close# closing the handle flushes the buffer 
}

(IO::Handle) method put

Defined as:

multi method put(**@text --> True)
multi method put(Junction:D --> True)

Writes the given @text to the handle, coercing any non-Str objects to Str by calling .Str method on them, and appending the value of .nl-out at the end. Junction arguments autothread and the order of printed strings is not guaranteed.

Attempting to call this method when the handle is in binary mode will result in X::IO::BinaryMode exception being thrown.

my $fh = 'path/to/file'.IO.open: :w;
$fh.print: 'some text';
$fh.close;

(IO::Handle) method say

Defined as:

multi method say(IO::Handle:D: **@text --> True)

This method is identical to put except that it stringifies its arguments by calling .gist instead of .Str.

Attempting to call this method when the handle is in binary mode will result in X::IO::BinaryMode exception being thrown.

my $fh = open 'path/to/file':w;
$fh.say(Complex.new(34));        # RESULT: «3+4i\n» 
$fh.close;

(IO::Handle) method read

Defined as:

method read(IO::Handle:D: Int(Cool:D$bytes = 65536 --> Buf:D)

Binary reading; reads and returns up to $bytes bytes from the filehandle. $bytes defaults to an implementation-specific value (in Rakudo, the value of $*DEFAULT-READ-ELEMS, which by default is set to 65536). This method can be called even when the handle is not in binary mode.

(my $file = 'foo'.IO).spurt: 'I ♥ Perl';
given $file.open {
    say .read: 6# OUTPUT: «Buf[uint8]:0x<49 20 e2 99 a5 20>␤» 
    .close;
}

(IO::Handle) method readchars

Defined as:

method readchars(IO::Handle:D: Int(Cool:D$chars = 65536 --> Str:D)

Reading chars; reads and returns up to $chars chars (graphemes) from the filehandle. $chars defaults to an implementation-specific value (in Rakudo, the value of $*DEFAULT-READ-ELEMS, which by default is set to 65536). Attempting to call this method when the handle is in binary mode will result in X::IO::BinaryMode exception being thrown.

(my $file = 'foo'.IO).spurt: 'I ♥ Perl';
given $file.open {
    say .readchars: 5# OUTPUT: «I ♥ P␤» 
    .close;
}

(IO::Handle) method write

Defined as:

method write(IO::Handle:D: Blob:D $buf --> True)

Writes $buf to the filehandle. This method can be called even when the handle is not in binary mode.

(IO::Handle) method seek

Defined as:

method seek(IO::Handle:D: Int:D $offsetSeekType:D $whence --> True)

Move the file pointer (that is, the position at which any subsequent read or write operations will begin) to the byte position specified by $offset relative to the location specified by $whence which may be one of:

  • SeekFromBeginning

  • The beginning of the file.

  • SeekFromCurrent

  • The current position in the file.

  • SeekFromEnd

  • The end of the file. Please note that you need to specify a negative offset if you want to position before the end of the file.

    (IO::Handle) method tell

    Defined as:

    method tell(IO::Handle:D: --> Int:D)

    Return the current position of the file pointer in bytes.

    (IO::Handle) method slurp-rest

    Defined as:

    multi method slurp-rest(IO::Handle:D: :$bin! --> Buf)
    multi method slurp-rest(IO::Handle:D: :$enc --> Str)

    DEPRECATION NOTICE: this method will be deprecated in 6.d language. Do not use it for new code. Use .slurp method method instead.

    Return the remaining content of the file from the current file position (which may have been set by previous reads or by seek.) If the adverb :bin is provided a Buf will be returned, otherwise the return will be a Str with the optional encoding :enc.

    (IO::Handle) method slurp

    Defined as:

    method slurp(IO::Handle:D: :$close:$bin)

    Returns all the content from the current file position to the end. If the invocant is in binary mode or if $bin is set to True, will return a Buf, otherwise will decode the content using invocant's current .encoding and return a Str.

    If :$close is set to True, will close the handle when finished reading.

    Note: On Rakudo this method was introduced with release 2017.04 and $bin arg was added in 2017.10.

    (IO::Handle) method Supply

    Defined as:

    multi method Supply(IO::Handle:D: :$size = 65536)

    Returns a Supply that will emit the contents of the handle in chunks. The chunks will be Buf if the handle is in binary mode or, if it isn't, Str decoded using same encoding as IO::Handle.encoding.

    The size of the chunks is determined by the optional :size named parameter and 65536 bytes in binary mode or 65536 characters in non-binary mode.

    "foo".IO.open(:bin).Supply(:size<10>).tap: *.perl.say;
    # OUTPUT: 
    # Buf[uint8].new(73,32,226,153,165,32,80,101,114,108) 
    # Buf[uint8].new(32,54,33,10) 
     
    "foo".IO.open.Supply(:size<10>).tap: *.perl.say;
    # OUTPUT: 
    # "I ♥ Perl 6" 
    # "!\n" 

    (IO::Handle) method path

    Defined as:

    method path(IO::Handle:D:)

    For a handle opened on a file this returns the IO::Path that represents the file. For the standard I/O handles $*IN, $*OUT, and $*ERR it returns an IO::Special object.

    (IO::Handle) method IO

    Defined as:

    method IO(IO::Handle:D:)

    Alias for .path

    (IO::Handle) method Str

    Returns the value of .path, coerced to Str.

    say "foo".IO.open.path# OUTPUT: «"foo".IO␤» 

    (IO::Handle) routine close

    Defined as:

    method close(IO::Handle:D: --> Bool:D)
    multi sub close(IO::Handle $fh)

    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: $*IN, $*OUT, or $*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.

    if $do-stuff-with-the-file {
        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 
    }
     
    given "foo/bar".IO.open(:w{
        .spurt: "I ♥ Perl 6!";
        .close;
    }

    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 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 provides many reading and writing methods that let you work with files without dealing with filehandles directly.

    (IO::Handle) method flush

    Defined as:

    method flush(IO::Handle:D: --> True)

    Will flush the handle, writing any of the buffered data. Returns True on success; otherwise, fails with X::IO::Flush.

    given "foo".IO.open: :w {
        LEAVE .close;
        $fh.print: 'something';
        'foo'.IO.slurp.say# (if the data got buffered) OUTPUT: «␤» 
        $fh.flush;          # flush the handle 
        'foo'.IO.slurp.say# OUTPUT: «something␤» 
    }

    (IO::Handle) method native-descriptor

    Defined as:

    method native-descriptor()

    This returns a value that the operating system would understand as a "file descriptor" and is suitable for passing to a native function that requires a file descriptor as an argument such as fcntl or ioctl.

    (IO::Handle) method nl-in

    Defined as:

    method nl-in(--> Str:Dis rw

    One of the attributes that can be set via .new or open. Defaults to ["\x0A", "\r\n"]. Takes either a Str or Array of Str specifying input line ending(s) for this handle. If .chomp attribute is set to True, will strip these endings in routines that chomp, such as get and lines.

    with 'test'.IO {
        .spurt: '1foo2bar3foo'# write some data into our test file 
        my $fh will leave {.close} = .open# can also set .nl-in via .open arg 
        $fh.nl-in = [<foo bar>]; # set two possible line endings to use; 
        $fh.lines.say# OUTPUT: ("1", "2", "3").Seq 
    }

    (IO::Handle) method nl-out

    Defined as:

    has Str:D $.nl-out is rw = "\n";

    One of the attributes that can be set via .new or open. Defaults to "\n". Takes a Str specifying output line ending for this handle, to be used by methods .put and .say.

    with 'test'.IO {
        given .open: :w {
            .put: 42;
            .nl-out = 'foo';
            .put: 42;
            .close;
        }
        .slurp.perl.say# OUTPUT: «"42\n42foo"» 
    }

    (IO::Handle) method opened

    Defined as:

    method opened(IO::Handle:D: --> Bool:D)

    Returns True if the handle is open, False otherwise.

    (IO::Handle) method WRITE

    Defined as:

    method WRITE(IO::Handle:D: Blob:D \data --> Bool:D)

    Called whenever a write operation is performed on the handle. Always receives the data as a Blob, even if a textual writing method has been called.

    (IO::Handle) method READ

    Defined as:

    method READ(IO::Handle:D: Int:D \bytes --> Buf:D)

    Called whenever a read operation is performed on the handle. Receives the number of bytes requested to read. Returns a Buf with those bytes which can be used to either fill the decoder buffer or returned from reading methods directly. The result is allowed to have fewer than the requested number of bytes, including no bytes at all.

    If you provide your own .READ, you very likely need to provide your own .EOF as well, for all the features to behave correctly.

    The compiler may call .EOF method any number of times during a read operation to ascertain whether a call to .READ should be made. More bytes than necessary to satisfy a read operation may be requested from .READ, in which case the extra data may be buffered by the IO::Handle or the decoder it's using, to fullfill any subsequent reading operations, without necessarilly having to make another .READ call.

    (IO::Handle) method EOF

    Defined as:

    method EOF(IO::Handle:D: --> Bool:D)

    Indicates wether "end of file" has been reached for the data source of the handle; i.e. no more data can be obtained by calling .READ method. Note that this is not the same as eof method, which will return True only if .EOF returns True and all the decoder buffers, if any were used by the handle, are also empty.