Documentation for routine spurt

Documentation for routine spurt, assembled from the following types:

role IO

From IO

(IO) sub spurt

Defined as:

multi spurt(IO() $path|c)

The $path can be any object with an IO method that returns an IO::Path object. Calls IO::Path.spurt on the $path, forwarding any of the remaining arguments.


The encoding with which the contents will be written.

Open the file in binary mode.

Boolean indicating whether to append to a (potentially) existing file. If the file did not exist yet, it will be created. Defaults to False.

Boolean indicating whether to fail if the file already exists. Defaults to False.


# write directly to a file 
#spurt "path/to/file", "default text, directly written"; 
# write directly with a non-Unicode encoding 
#spurt "path/to/latin1_file", "latin1 text: äöüß", enc => "latin1"; 
# append to a pre-existing file 
#spurt "file_already_exists", "some text"; 
#spurt "file_already_exists", "new text", :append; 
#slurp "file_already_exists";   # some text␤new text 
# fail when writing to a pre-existing file 
#spurt "file_already_exists", "new text", :createonly; 
CATCH { default { put .^name''.Str } };
# OUTPUT: «X::Cannot::Empty: Cannot pop from an empty Array␤» 

class IO::Handle

From IO::Handle

(IO::Handle) method spurt

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

class IO::Path

From IO::Path

(IO::Path) method spurt

method spurt(IO::Path:D: $data:$enc:$append:$createonly)

Opens the file path for writing, and writes all of the $data into it. Will fail if it cannot succeed for any reason. The $data can be any Cool type or any Blob type. Arguments are as follows: