Documentation for routine open
Documentation for routine open, assembled from the following types:
my = open(IO::Path() , :, :, :, :,:, :, :, :, :)
$path (by default in text mode) with the given options, returning an IO::Handle object.
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.
# open explicitly as utf8my = open("path-to-file", enc => "utf8");my = open("path-to-file", enc => "utf-8"); # this form also works# open with latin1 encodingmy = open("path-to-file", enc => "latin1");
Defaults to "utf8".
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::Path: *)
Opens the path as a file; the named options control the mode, and are the same as the open function accepts.