Documentation for routine spurt

Documentation for routine spurt, assembled from the following types:

role IO

From IO

(IO) sub spurt

sub spurt($where$what,
    Str  :$enc        = 'utf8',
    Bool :$bin        = False,
    Bool :$append      = False,
    Bool :$createonly = False
    --> Bool )

Writes the indicated contents (2nd positional parameter, $what) to the location indicated by the first positional parameter, $where (which can either be a string or an IO::Path object). To write to an IO::Handle, use the print method.

If a file needs to be opened for writing, it will also be closed. Returns True on success, or the appropriate Failure if something went wrong.

These named parameters are optional and only have meaning if the first positional parameter was not an IO::Handle:

Options

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.

Examples

# 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::Path

From IO::Path

(IO::Path) routine spurt

multi method spurt(IO::Path:D: Blob $contents:$bin|c)
multi method spurt(IO::Path:D: Cool $contents:$bin|c)

Write $contents to this path, like spurt.