Documentation for routine read

Documentation for routine read, assembled from the following types:

class IO::Handle

From IO::Handle

(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;
}

class IO::CatHandle

From IO::CatHandle

(IO::CatHandle) method read

Defined as:

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

Reads up to $bytes bytes from the handle and returns them in a Buf. $bytes defaults to an implementation-specific value (in Rakudo, the value of $*DEFAULT-READ-ELEMS, which by default is set to 65536). It is permitted to call this method on handles that are not in binary mode.

(my $f1 = 'foo'.IO).spurt: 'meow';
(my $f2 = 'bar'.IO).spurt: Blob.new: 456;
with IO::CatHandle.new: :bin$f1$f2 {
    say .read: 2;    # OUTPUT: «Buf[uint8]:0x<6d 65>␤» 
    say .read: 2000# OUTPUT: «Buf[uint8]:0x<6f 77 04 05 06>␤» 
}
 
# Non-binary mode is OK too: 
with IO::CatHandle.new: $f1$f2 {
    say .get;        # OUTPUT: «meow␤» 
    say .read: 2000# OUTPUT: «Buf[uint8]:0x<04 05 06>␤» 
}

role IO::Socket

From IO::Socket

(IO::Socket) method read

method read(IO::Socket:D: Int(Cool$bytes)

Reads $bytes bytes from the socket and returns them in a Blob.

Fails if the socket is not connected.