Documentation for routine open
Documentation for routine open, assembled from the following types:
my = open(IO::Path() , :, :, :, :,:, :, :, :, :)
Open the file as read only, e.g.:
my = open("path/to/file", :r);
This is the default mode for
Write-related methods on the returned
IO::Handle object will fail in this mode:
my = open("test"); # the file "test" already exists.print("new text\n"); # failsCATCH ;# OUTPUT: «X::AdHoc: Failed to write bytes to filehandle: bad file descriptor␤»
Open the file for writing, creating it if it doesn't exist or overwriting the file if it does exist, e.g.:
my = open("path-to-file", :w);
Read-related methods will fail in this mode:
my = open("test", :w);.print("stuff\n");.print("more stuff\n");.seek(0); # return to the start of the file.get(); # failsCATCH ;# OUTPUT: «Reading from filehandle failed: bad file descriptor␤»
Open the file for reading and writing, creating the file if it doesn't exist or overwriting the file if it already exists.
my = open("path-to-file", :rw);
Open the file for appending. If the file does not exist, create it. If the file already exists, append data to it.
my = open("path-to-file", :a);
Open the file in binary mode (byte mode):
my = open("path-to-file", :bin);
text mode encoding,
The encoding to use if opened in text mode.
my = open 'path-to-file'; # default, utf-8my = open 'path-to-file', :enc<latin-1>; # explicit, latin-1
utf8. Attempting to set the encoding while
:bin is true will cause
X::IO::BinaryAndEncoding exception to be thrown. Note that specifying Str type object or
Nil as encoding is the same as not specifying the argument at all; use
:bin argument if you want to enable binary mode.
The values are case-insensitive. The available encodings vary by implementation and backend. On Rakudo MoarVM the following are supported:
Implementation may choose to also provide support for aliases, e.g. Rakudo allows aliases
iso-8859-1 encoding and dashed utf versions:
end-of-line (EOL) marker,
nl-in is the marker used to indicate the end of a line of text. Only used in text mode. Defaults to
["\n", "\r\n"] and does not include
nl-out defaults to
# explicitly use CR-LF as EOL charactermy = open("path-to-file", nl-in => "\r\n");
Whether or not to remove newline characters from text obtained with
.get. Defaults to
# don't remove newline characters from inputmy = open("path-to-file", chomp => False);say .get(); # returns line including newline char
method open(IO::CatHandle: --> IO::CatHandle)
Returns the invocant. The intent of this method is to merely make CatHandle workable with things that open IO::Handle. You never have to call this method intentionally.
method open(IO::Path: *)
Opens the path as a file; the named options control the mode, and are the same as the open function accepts.