Working with files and directories
Working with files in Perl 6, as well as in many other languages, is done via file handles. You get the file handle as soon as you open a file; later, you use the handle to write to a file or to read from it. All the other operations, such as flushing a buffer or closing a file, are also performed via the handle.
Opening a file
To open a file, use the open
function (it is supplied by the IO
role but can be used as a simple built-in function). It takes the path to the file and a number of optional parameters. The return value is a file handle, as shown:
my $fh = open '/etc/passwd';
By default, the file is opened in the read-only mode. It is possible to pass the mode name explicitly using the named arguments. The above example is equivalent to the following code:
my $fh = open '/etc/passwd', :r;
The following table lists the possible modes for opening a file:
Parameter | Description |
| Read-only mode. This is the default mode. |
| Write-only mode. Creates a file if it does... |