# class IO::Path

File or directory path

The workhorse of IO operations.

Conceptually, an IO::Path object consists of a volume, a directory, and a basename. It supports both purely textual operations, and operations that access the file system, e.g. to resolve a path, or to read all content of a file.

## method dirname

Defined as:

Returns the directory name portion of the path object. That is, it returns the path excluding the volume and the base name.

## method volume

Defined as:

Returns the volume portion of the path object. On Unix system, this is always the empty string.

## method parts

Defined as:

Returns a Map with the keys volume, dirname, basename whose values are the same as available via methods .volume, .dirname, and .basename respectively.

## method perl

Defined as:

Returns a string that, when given passed through EVAL gives the original invocant back.

## method succ

Defined as:

Returns a new IO::Path constructed from the invocant, with .basename changed by calling Str.succ on it.

## method open

Defined as:

Opens the path as a file; the named options control the mode, and are the same as the open function accepts.

## method pred

Defined as:

Returns a new IO::Path constructed from the invocant, with .basename changed by calling Str.pred on it.

## method watch

Defined as:

Equivalent to calling IO::Notification.watch-path with the invocant as the argument.

## method is-absolute

Defined as:

Returns True if the path is an absolute path, and False otherwise.

Note that on Windows a path that starts with a slash or backslash is still considered absolute even if no volume was given, as it is absolute for that particular volume:

## method is-relative

Defined as:

Returns True if the path is a relative path, and False otherwise. Windows caveats for .is-absolute apply.

Defined as:

## method e

Defined as:

Returns True if the invocant is a path that exists.

TIP: use smartmatch with Pairs to perform multiple file tests.

## method d

Defined as:

Returns True if the invocant is a path that exists and is a directory. The method will fail with X::IO::DoesNotExist if the path points to a non-existent filesystem entity.

TIP: use smartmatch with Pairs to perform multiple file tests.

## method f

Defined as:

Returns True if the invocant is a path that exists and is a file. The method will fail with X::IO::DoesNotExist if the path points to a non-existent filesystem entity.

TIP: use smartmatch with Pairs to perform multiple file tests.

## method s

Defined as:

Returns the file size in bytes. May be called on paths that are directories, in which case the reported size is dependent on the operating system. The method will fail with X::IO::DoesNotExist if the path points to a non-existent filesystem entity.

TIP: use smartmatch with Pairs to perform multiple file tests.

## method l

Defined as:

Returns True if the invocant is a path that exists and is a symlink. The method will fail with X::IO::DoesNotExist if the path points to a non-existent filesystem entity.

TIP: use smartmatch with Pairs to perform multiple file tests.

## method r

Defined as:

Returns True if the invocant is a path that exists and is accessible. The method will fail with X::IO::DoesNotExist if the path points to a non-existent filesystem entity.

TIP: use smartmatch with Pairs to perform multiple file tests.

## method w

Defined as:

Returns True if the invocant is a path that exists and is writable. The method will fail with X::IO::DoesNotExist if the path points to a non-existent filesystem entity.

TIP: use smartmatch with Pairs to perform multiple file tests.

## method rw

Defined as:

Returns True if the invocant is a path that exists and is readable and writable. The method will fail with X::IO::DoesNotExist if the path points to a non-existent filesystem entity.

TIP: use smartmatch with Pairs to perform multiple file tests.

## method x

Defined as:

Returns True if the invocant is a path that exists and is executable. The method will fail with X::IO::DoesNotExist if the path points to a non-existent filesystem entity.

TIP: use smartmatch with Pairs to perform multiple file tests.

## method rwx

Defined as:

Returns True if the invocant is a path that exists and is executable, readable, and writable. The method will fail with X::IO::DoesNotExist if the path points to a non-existent filesystem entity.

TIP: use smartmatch with Pairs to perform multiple file tests.

## method z

Defined as:

Returns True if the invocant is a path that exists and has size of 0. May be called on paths that are directories, in which case the reported file size (and thus the result of this method) is dependent on the operating system. The method will fail with X::IO::DoesNotExist if the path points to a non-existent filesystem entity.

TIP: use smartmatch with Pairs to perform multiple file tests.

## method sibling

Defined as:

Allows to reference a sibling file or directory. Returns a new IO::Path based on the invocant, with the .basename changed to $sibling. The $sibling is allowed to be a multi-part path fragment; see also .add.

## method words

Defined as:

Opens the invocant and returns its words.

The behaviour is equivalent to opening the file specified by the invocant, forwarding the :$chomp, :$enc, and :$nl-in arguments to IO::Handle.open, then calling IO::Handle.words on that handle, forwarding any of the remaining arguments to that method, and returning the resultant Seq. NOTE: words are lazily read. The handle used under the hood is not closed until the returned Seq is fully reified, and this could lead to leaking open file handles. It is possible to avoid leaking open file handles using the $limit argument to cut down the Seq of words to be generated.

## method lines

Defined as:

Opens the invocant and returns its lines.

The behaviour is equivalent to opening the file specified by the invocant, forwarding the :$chomp, :$enc, and :$nl-in arguments to IO::Handle.open, then calling IO::Handle.lines on that handle, forwarding any of the remaining arguments to that method, and returning the resultant Seq. NOTE: the lines are ready lazily and the handle used under the hood won't get closed until the returned Seq is fully reified, so ensure it is, or you'll be leaking open file handles. (TIP: use the $limit argument)

Defined as:

Read all of the file's content and return it as either Buf, if :$bin is True, or if not, as Str decoded with :$enc encoding, which defaults to utf8. See &open for valid values for :$enc. ## method spurt Defined as: 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: • :$enc — character encoding of the data. Takes same values as :$enc in IO::Handle.open. Defaults to utf8. Ignored if $data is a Blob.

• :$append — open the file in append mode, preserving existing contents, and appending data to the end of the file. • :$createonlyfail if the file already exists.

## method chdir

Defined as:

DEPRECATION NOTICE: this method will be deprecated in 6.d language and removed in 6.e. Do not use it for new code. Instead, create a new path or use add method. For altering current working directory see &chdir and &*chdir subroutines.

## routine rmdir

Defined as:

Remove the invocant, or in sub form, all of the provided directories in the given list, which can contain any Cool object. Only works on empty directories.

Method form returns True on success and throws an exception of type X::IO::Rmdir if the directory cannot be removed (e.g. the directory is not empty, or the path is not a directory). Subroutine form returns a list of directories that were successfully deleted.

To delete non-empty directory, see rmtree in File::Directory::Tree module.

Defined as:

Changes the POSIX permissions of a file or directory to $mode. Returns True on success; on failure, fails with X::IO::Chmod. The mode is expected as an integer following the standard numeric notation, and is best written as an octal number: Make sure you don't accidentally pass the intended octal digits as a decimal number (or string containing a decimal number): ## routine rename Defined as: Renames a file or directory. Returns True on success; fails with X::IO::Rename if :$createonly is True and the $to path already exists or if the operation failed for some other reason. Note: some renames will always fail, such as when the new name is on a different storage device. See also: move. ## routine copy Defined as: Copies a file. Returns True on success; fails with X::IO::Copy if :$createonly is True and the $to path already exists or if the operation failed for some other reason, such as when $to and $from are the same file. ## routine move Defined as: Copies a file and then removes the original. If removal fails, it's possible to end up with two copies of the file. Returns True on success; fails with X::IO::Move if :$createonly is True and the $to path already exists or if the operation failed for some other reason, such as when $to and $from are the same file. To avoid copying, you can use rename, if the files are on the same storage device. It also works with directories, while move does not. ## method Numeric Defined as: Coerces .basename to Numeric. Fails with X::Str::Numeric if base name is not numerical. ## method Int Defined as: Coerces .basename to Int. Fails with X::Str::Numeric if base name is not numerical. Defined as: Create a new symbolic link $link to existing $target. Returns True on success; fails with X::IO::Symlink if the symbolic link could not be created. If $target does not exist, creates a dangling symbolic link. To create a hard link, see link.

Note: on Windows, creation of symbolic links may require escalated privileges.

Defined as:

Create a new hard link $link to existing $target. Returns True on success; fails with X::IO::Link if the hard link could not be created. To create a symbolic link, see symlink.

Defined as:

Delete all specified ordinary files, links, or symbolic links. See rmdir to delete directories.

The subroutine form returns the names of the files that were successfully deleted. The method form returns True on success, or fails with X::IO::Unlink if the operation could not be completed.

## method IO

Defined as:

Returns the invocant.

## method SPEC

Defined as:

Returns the IO::Spec object that was (implicitly) specified at object creation time.

# File timestamp retrieval

There are also 3 methods for fetching the 3 timestamps of a file (inode), on Operating Systems where these are available:

## method modified

Returns an Instant object indicating when the file was last modified.

## method accessed

Return an Instant object representing the timestamp when the file was last accessed. Note: depending on how the filesystem was mounted, the last accessed time may not update on each access to the file, but only on the first access after modifications.

## method changed

Returns an Instant object indicating the file or directory was last changed.

# File permissions retrieval

## method mode

Return an IntStr object representing the POSIX permissions of a file. The Str part of the result is the octal representation of the file permission, like the form accepted by the chmod(1) utility.

The result of this can be used in the other methods that take a mode as an argument.

# Routines supplied by role IO

IO::Path does role IO, which provides the following routines:

Defined as:

Creates a new directory; see mode for explanation and valid values for $mode. Returns the IO::Path object pointing to the newly created directory on success; fails with X::IO::Mkdir if directory cannot be created. Also creates parent directories, as needed (similar to *nix utility mkdir with -p option); that is, mkdir "foo/bar/ber/meow" will create foo, foo/bar, and foo/bar/ber directories if they do not exist, as well as foo/bar/ber/meow. ## (IO) sub chdir Defined as: Changes value of $*CWD variable to the provided $path, optionally ensuring the new path passes several file tests. NOTE: that this routine does NOT alter the process's current directory (see &*chdir). Returns IO::Path representing new $*CWD on success. On failure, returns Failure and leaves $*CWD untouched. The $path can be any object with an IO method that returns an IO::Path object. The available file tests are:

• :d — check .d returns True

• :r — check .r returns True

• :w — check .w returns True

• :x — check .x returns True

• By default, only :d test is performed.

Note that the following construct is a mistake:

Use indir instead.

## (IO) sub &*chdir

Defined as:

Changes value of $*CWD variable to the provided $path and sets the process's current directory to the value of $path.absolute. NOTE: that in most cases, you want to use chdir routine instead. Returns IO::Path representing new $*CWD on success. On failure, returns Failure and leaves $*CWD untouched. The $path can be any object with an IO method that returns an IO::Path object.

Note that unlike regular chdir, there are no arguments to specify which file tests to perform.

Note that the following construct is a mistake:

Use the following, instead; or see indir if you do not need to change process's current directory:

Defined as:

Coerces all @filenames to IO::Path and calls IO::Path.chmod with $mode on them. Returns a List containing a subset of @filenames for which chmod was successfully executed. ## (IO) sub indir Defined as: Takes Callable &code and executes it after locally (to &code) changing $*CWD variable to an IO::Path object based on $path, optionally ensuring the new path passes several file tests. If $path is relative, it will be turned into an absolute path, even if an IO::Path object was given. NOTE: that this routine does NOT alter the process's current directory (see &*chdir). The $*CWD outside of the &code is not affected, even if &code explicitly assigns a new value to $*CWD.

Returns the return value of &code on success. On failure to successfully change $*CWD, returns Failure. WARNING: keep in mind that lazily evaluated things might end up NOT having the $*CWD set by indir in their dynamic scope by the time they're actually evaluated. Either ensure the generators have their $*CWD set or eagerly evaluate them before returning the results from indir: The routine's $path argument can be any object with an IO method that returns an IO::Path object. The available file tests are:

• :d — check .d returns True

• :r — check .r returns True

• :w — check .w returns True

• :x — check .x returns True

• By default, only :d test is performed.

Defined as:

## (IO) sub say

Defined as:

Prints the "gist" of given objects. Same as put, except uses .gist method to obtain string representation of the object.

NOTE: the .gist method of some objects, such as Lists, returns only partial information about the object (hence the "gist"). If you mean to print textual information, you most likely want to use put instead.

Defined as:

## (Cool) routine log10

Defined as:

Coerces the invocant (or in the sub form, the invocant) to Numeric, and returns its Logarithm to base 10, that is, a number that approximately produces the original number when raised to the power of 10. Returns NaN for negative arguments and -Inf for 0.

## (Cool) method exp

Defined as:

Coerces the arguments (including the invocant in the method from) to Numeric, and returns $base raised to the power of the first number. If no $base is supplied, e (Euler's Number) is used.

## (Cool) method unpolar

Defined as:

Coerces the arguments (including the invocant in the method form) to Numeric, and returns a complex number from the given polar coordinates. The invocant (or the first argument in sub form) is the magnitude while the argument (i.e. the second argument in sub form) is the angle. The angle is assumed to be in radians.

## (Cool) routine round

Defined as:

Coerces the invocant (or in sub form, its argument) to Numeric, and rounds it to the unit of $unit. If $unit is 1, rounds to the nearest integer.

Always rounds up if the number is at mid-point:

## (Cool) routine floor

Defined as:

Coerces the invocant (or in sub form, its argument) to Numeric, and rounds it downwards to the nearest integer.

## (Cool) routine ceiling

Defined as:

Coerces the invocant (or in sub form, its argument) to Numeric, and rounds it upwards to the nearest integer.

## (Cool) routine truncate

Defined as:

Coerces the invocant (or in sub form, its argument) to Numeric, and rounds it towards zero.

## (Cool) routine ord

Defined as:

Coerces the invocant (or in sub form, its argument) to Str, and returns the Unicode code point number of the first code point.

The inverse operation is chr.

Mnemonic: returns an ordinal number

## (Cool) method path

Defined as:

DEPRECATED. Existed only in the Rakudo implementation and isn't part of any language released. Issues deprecation warnings in 6.d language and will be removed entirely when 6.e language is released.

Stringifies the invocant and converts it to IO::Path object. Use the .IO method instead.

## (Cool) routine chr

Defined as:

Coerces the invocant (or in sub form, its argument) to Int, interprets it as a Unicode code points, and returns a string made of that code point.

The inverse operation is ord.

Mnemonic: turns an integer into a character.

## (Cool) routine chars

Defined as:

Coerces the invocant (or in sub form, its argument) to Str, and returns the number of characters in the string. Please note that on the JVM, you currently get codepoints instead of graphemes.

Graphemes are user visible characters. That is, this is what the user thinks of as a “character”.

Graphemes can contain more than one codepoint. Typically the number of graphemes and codepoints differs when Prepend or Extend characters are involved (also known as Combining characters), but there are many other cases when this may happen. Another example is \c[ZWJ] (Zero-width joiner).

You can check Grapheme_Cluster_Break property of a character in order to see how it is going to behave:

You can read more about graphemes in the Unicode Standard, which Perl 6 tightly follows, using a method called NFG, normal form graphemes for efficiently representing them.

## (Cool) routine codes

Defined as:

Coerces the invocant (or in sub form, its argument) to Str, and returns the number of Unicode code points.

The same result will be obtained with

ords first obtains the actual codepoints, so there might be a difference in speed.

## (Cool) routine flip

Defined as:

Coerces the invocant (or in sub form, its argument) to Str, and returns a reversed version.

## (Cool) routine trim

Defined as:

Coerces the invocant (or in sub form, its argument) to Str, and returns the string with both leading and trailing whitespace stripped.

Defined as:

Coerces the invocant (or in sub form, its argument) to Str, and returns the string with leading whitespace stripped.

## (Cool) routine trim-trailing

Defined as:

Coerces the invocant (or in sub form, its argument) to Str, and returns the string with trailing whitespace stripped.

## (Cool) routine lc

Defined as:

Coerces the invocant (or in sub form, its argument) to Str, and returns it case-folded to lower case.

## (Cool) routine uc

Defined as:

Coerces the invocant (or in sub form, its argument) to Str, and returns it case-folded to upper case (capital letters).

## (Cool) routine fc

Defined as:

Coerces the invocant (or in sub form, its argument) to Str, and returns the result a Unicode "case fold" operation suitable for doing caseless string comparisons. (In general, the returned string is unlikely to be useful for any purpose other than comparison.)

## (Cool) routine tc

Defined as:

Coerces the invocant (or in sub form, its argument) to Str, and returns it with the first letter case-folded to title case (or where not available, upper case).

## (Cool) routine tclc

Defined as:

Coerces the invocant (or in sub form, its argument) to Str, and returns it with the first letter case-folded to title case (or where not available, upper case), and the rest of the string case-folded to lower case.

Defined as:

## (Cool) method uniprop

Defined as:

Returns the unicode property of the first character. If no property is specified returns the General Category. Returns a Bool for Boolean properties.

## (Cool) method uniprops

Defined as:

Interprets the invocant as a Str, and returns the unicode property for each character as a Seq. If no property is specified returns the General Category. Returns a Bool for Boolean properties. Similar to uniprop

## (Cool) method uniname

Defined as:

Interprets the invocant / first argument as a Str, and returns the Unicode codepoint name of the first codepoint of the first character. See uninames for a routine that works with multiple codepoints, and uniparse for the opposite direction.

## (Cool) method uninames

Defined as:

Returns of a Seq of Unicode names for the all the codepoints in the Str provided.

Note this example, which gets a Seq where each element is a Seq of all the codepoints in that character.

See uniparse for the opposite direction.

## (Cool) method unimatch

Defined as:

Checks if the given integer codepoint or the first letter of the string given have a unicode property equal to the value you give. If you supply the Unicode property to be checked it will only return True if that property matches the given value.

## (Cool) routine chop

Defined as:

Coerces the invocant (or in sub form, its argument) to Str, and returns it with the last character removed.

## (Cool) routine chomp

Defined as:

Coerces the invocant (or in sub form, its argument) to Str, and returns it with the last character removed, if it is a logical newline.

## (Cool) routine substr

Defined as:

Coerces the invocant (or in the sub form, the first argument) to Str, and calls Str.substr with the arguments.

## (Cool) routine ords

Defined as:

Coerces the invocant (or in the sub form, the first argument) to Str, and returns a list of Unicode codepoints for each character.

This is the list-returning version of ord. The inverse operation in chrs. If you are only interested in the number of codepoints, codes is a possibly faster option.

## (Cool) routine chrs

Defined as:

Coerces the invocant (or in the sub form, the argument list) to a list of integers, and returns the string created by interpreting each integer as a Unicode codepoint, and joining the characters.

This is the list-input version of chr. The inverse operation is ords.

## (Cool) routine split

Defined as:

Coerces the invocant (or in the sub form, the second argument) to Str, and splits it into pieces based on delimiters found in the string.

Coerces the invocant and first argument to Str, and searches for $needle in the string starting from $start. Returns True if $needle is found. Note that because of how a List or Array is coerced into a Str, the results may sometimes be surprising. See traps. ## (Cool) routine index Defined as: Coerces the first two arguments (in method form, also counting the invocant) to Str, and searches for $needle in the string starting from $startpos. It returns the offset into the string where $needle was found, and an undefined value if it was not found.

See the documentation in type Str for examples.

Defined as:

## (Cool) routine roots

Defined as:

Coerces the first argument (and in method form, the invocant) to Numeric and the second ($n) to Int, and produces a list of $n Complex $n-roots, which means numbers that, raised to the $nth power, approximately produce the original number.

For example

## (Cool) method IO

Defined as:

Coerces the invocant to IO::Path.

## (Cool) routine EVAL

Defined as:

Method form calls subroutine form with invocant as $code, passing along named args, if any. Subroutine form coerces Cool $code to Str. If $code is a Blob, it'll be processed using the same encoding as the $lang compiler would: for perl6, uses the encoding specified via --encoding command line argument, or utf-8 if none were given; for Perl5, processes using same rules as perl.

This works as-is with a literal string parameter. More complex input, such as a variable or string with embedded code, is illegal by default. This can be overridden in any of several ways:

Symbols in the current lexical scope are visible to code in an EVAL.

However, since the set of symbols in a lexical scope is immutable after compile time, an EVAL can never introduce symbols into the surrounding scope.

Furthermore, the EVAL is evaluated in the current package:

And also the current language, meaning any added syntax is available:

An EVAL statement evaluates to the result of the last statement:

EVAL is also a gateway for executing code in other languages:

## (Cool) routine EVALFILE

Defined as:

Slurps the specified file and evaluates it. Behaves the same way as EVAL with regard to Blob decoding, scoping, and the \$lang parameter. Evaluates to the value produced by the final statement in the file.