method WRITE

Documentation for method WRITE assembled from the following types:

class IO::Handle

From IO::Handle

(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.

class IO::Store is IO::Handle {
    has @.lines = [];
 
    submethod TWEAK {
        self.encoding: 'utf8'# set up encoder/decoder 
    }
 
    method WRITE(IO::Handle:D: Blob:D \data --> Bool:D{
        @!lines.push: data.decode();
        True;
    }
 
    method gist() {
        return @!lines.join("\n" );
    }
}
my $store = IO::Store.new();
my $output = $PROCESS::OUT;
$PROCESS::OUT = $store;
.say for <one two three>;
$PROCESS::OUT = $output;
say $store.lines(); # OUTPUT «[one␤ two␤ three␤]» 

In this example we are creating a simple WRITE redirection which stores anything written to the filehandle to an array. Se need to save the standard output first, which we do in $output, and then everything that is printed or said (through say) gets stored in the defined IO::Store class. Two things should be taken into account in this class. By default, IO::Handles are in binary mode, so we need to TWEAK the objects if we want them to work with text. Second, a WRITE operation should return True if successful. It will fail if it does not.

class IO::CatHandle

From IO::CatHandle

(IO::CatHandle) method WRITE

Defined as:

multi method WRITE(|)

The IO::CatHandle type overrides this method to throw a X::NYI exception. If you have a good idea for how this method should behave, tell Rakudo developers about it!