IO::Path Reference
File or directory path
What it is #
Position in the hierarchy #
| Inherits from | — |
| Does | — |
| Inherited by | IO::Path::Unix, IO::Path::Win32, IO::Path::Cygwin, IO::Path::QNX |
Routines #
Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.
method new #
multi method new(Str:D $path, IO::Spec :$SPEC = $*SPEC, Str() :$CWD = $*CWD)
multi method new(
Creates a new IO::Path object from a path string (which is being parsed for volume, directory name and basename), or from volume, directory name and basename passed as named arguments. The path's operation will be performed using :$SPEC semantics (defaults to current $*SPEC) and will use :$CWD as the directory the path is relative to (defaults to
attribute CWD #
Read-only. Contains implicit or explicit value of :$CWD argument to .new.
attribute SPEC #
Read-only. Contains implicit or explicit value of :$SPEC argument to .new.
attribute path #
Read-only. Returns the string the object was constructed from or the value of $SPEC.join($volume, $dirname, $basename) if multi-part version of .new was used. NOTE: this does not include the $.CWD; see IO::Path.absolute and IO::Path.relative for stringification options that include $.CWD. NOTE: Implementations may cache operations done with this attribute, so
method ACCEPTS #
multi method ACCEPTS(IO::Path:D: Cool:D $other --> Bool:D)
Returns Bool:D.
Coerces the argument to IO::Path, if necessary. Returns True if .absolute method on both paths returns the same string. NOTE: it's possible for two paths that superficially point to the same resource to NOT smartmatch as True, if they were constructed differently and were never fully resolved: The reason is the two paths above may point to different resources when fully
method basename #
method basename(IO::Path:D:)
Returns the basename part of the path object, which is the name of the filesystem object itself that is referenced by the path. Note that in IO::Spec::Win32 semantics, the basename of a Windows share is \, not the name of the share itself:
method add #
method add(IO::Path:D: Str() $what --> IO::Path:D)
Returns IO::Path:D.
Concatenates a path fragment to the invocant and returns the resultant IO::Path. If adding ../ to paths that end with a file, you may need to call resolve for the resultant path to be accessible by other IO::Path methods like dir or open. See also sibling and parent. As of release 2021.07 of the Rakudo compiler, it is also possible to
method child #
method child(IO::Path:D: Str() $childname --> IO::Path:D)
Returns IO::Path:D.
Alias for .add.
method cleanup #
method cleanup(IO::Path:D: --> IO::Path:D)
Returns IO::Path:D.
Returns a new path that is a canonical representation of the invocant path, cleaning up any extraneous path parts: Note that no filesystem access is made. See also resolve.
method comb #
method comb(IO::Path:D: |args --> Seq:D)
Returns Seq:D.
Opens the file and processes its contents the same way Str.comb does, taking the same arguments. Implementations may slurp the file in its entirety when this method is called.
method split #
method split(IO::Path:D: |args --> Seq:D)
Returns Seq:D.
Opens the file and processes its contents the same way Str.split does, taking the same arguments. Implementations may slurp the file in its entirety when this method is called.
method extension #
multi method extension(IO::Path:D: --> Str:D)
multi method extension(IO::Path:D: Int :$parts --> Str:D)
multi method extension(IO::Path:D: Range :$parts --> Str:D)
multi method extension(IO::Path:D: Str $subst, Int :$parts, Str :$joiner --> IO::Path:D)
multi method extension(IO::Path:D: Str $subst, Range :$parts, Str :$joiner --> IO::Path:D)
Returns Str:D or IO::Path:D.
Returns the extension consisting of $parts parts (defaults to 1), where a "part" is defined as a dot followed by possibly-empty string up to the end of the string, or previous part. That is "foo.tar.gz" has an extension of two parts: first part is "gz" and second part is "tar" and calling "foo.tar.gz".IO.extension: :2parts gives "tar.gz". If an extension with the specified number of $parts is not found, returns an empty string.
method dirname #
method dirname(IO::Path:D:)
Returns the directory name portion of the path object. That is, it returns the path excluding the volume and the base name. Unless the dirname consist of only the directory separator (i.e. it's the top directory), the trailing directory separator will not be included in the return value.
method volume #
method volume(IO::Path:D:)
Returns the volume portion of the path object. On Unix system, this is always the empty string.
method parts #
method parts(IO::Path:D:)
Returns an IO::Path::Parts for the invocant. Note: Before Rakudo release 2020.06 a Map was returned, with the keys volume, dirname, basename whose values were the respective invocant parts.
method raku #
method raku(IO::Path:D: --> Str:D)
Returns Str:D.
Returns a string that, when given passed through EVAL gives the original invocant back. Note that this string includes the value of the .CWD attribute that is set to $*CWD when the path object was created, by default.
method gist #
method gist(IO::Path:D: --> Str:D)
Returns Str:D.
Returns a string, part of which contains either the value of .absolute (if path is absolute) or .path. Note that no escaping of special characters is made, so e.g. "\b" means a path contains a backslash and letter "b", not a backspace.
method Str #
method Str(IO::Path:D: --> Str)
Returns Str.
Alias for IO::Path.path. In particular, note that default stringification of an IO::Path does NOT use the value of $.CWD attribute. To stringify while retaining full path information use .absolute or .relative methods.
method succ #
method succ(IO::Path:D: --> IO::Path:D)
Returns IO::Path:D.
Returns a new IO::Path constructed from the invocant, with .basename changed by calling Str.succ on it.
method open #
method open(IO::Path:D: *%opts)
Opens the path as a file; the named options control the mode, and are the same as the open function accepts.
method pred #
method pred(IO::Path:D: --> IO::Path:D)
Returns IO::Path:D.
Returns a new IO::Path constructed from the invocant, with .basename changed by calling Str.pred on it.
method watch #
method watch(IO::Path:D: --> Supply:D)
Returns Supply:D.
Equivalent to calling IO::Notification.watch-path with the invocant as the argument.
method is-absolute #
method is-absolute(IO::Path:D: --> Bool)
Returns Bool.
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 #
method is-relative(IO::Path:D: --> Bool)
Returns Bool.
Returns True if the path is a relative path, and False otherwise. Windows caveats for .is-absolute apply.
method absolute #
multi method absolute(IO::Path:D: --> Str)
multi method absolute(IO::Path:D: $base --> Str)
Returns Str.
Returns a new Str object that is an absolute path. If the invocant is not already an absolute path, it is first made absolute using $base as base, if it is provided, or the .CWD attribute the object was created with if it is not.
method relative #
method relative(IO::Path:D: $base = $*CWD --> Str)
Returns Str.
Returns a new Str object with the path relative to the $base. If $base is not provided, $*CWD is used in its place. If the invocant is not an absolute path, it's first made to be absolute using the .CWD attribute the object was created with, and then is made relative to $base.
method parent #
multi method parent(IO::Path:D:)
multi method parent(IO::Path:D: UInt:D $level)
Returns the parent path of the invocant. Note that no actual filesystem access is made, so the returned parent is physical and not the logical parent of symlinked directories. If $level is specified, the call is equivalent to calling .parent() $level times:
method resolve #
method resolve(IO::Path:D: :$completely --> IO::Path)
Returns IO::Path.
Returns a new IO::Path object with all symbolic links and references to the parent directory (..) resolved. This means that the filesystem is examined for each directory in the path, and any symlinks found are followed. If :$completely, which defaults to False, is set to a true value, the method will fail with X::IO::Resolve if it cannot completely resolve the path, otherwise, it will resolve as much as possible, and
routine dir #
multi dir(*%_)
multi dir(IO::Path:D $path, |c)
multi dir(IO() $path, |c)
method dir(IO::Path:D: Mu :$test = $*SPEC.curupdir)
Returns a lazy list of IO::Path objects corresponding to the entries in a directory, optionally filtered by smartmatching their names as strings per the :test parameter. The order in which the filesystem returns entries determines the order of the entries/objects in the list. Objects corresponding to special directory entries . and .. are not included. $path determines whether the objects' paths are absolute or relative.
method e #
method e(IO::Path:D: --> Bool:D)
Returns Bool:D.
Returns True if the invocant is a path that exists.
method d #
method d(IO::Path:D: --> Bool:D)
Returns Bool:D.
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.
method f #
method f(IO::Path:D: --> Bool:D)
Returns Bool:D.
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.
method s #
method s(IO::Path:D: --> Int:D)
Returns Int:D.
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.
method l #
method l(IO::Path:D: --> Bool:D)
Returns Bool:D.
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.
method r #
method r(IO::Path:D: --> Bool:D)
Returns Bool:D.
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.
method w #
method w(IO::Path:D: --> Bool:D)
Returns Bool:D.
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.
method rw #
method rw(IO::Path:D: --> Bool:D)
Returns Bool:D.
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.
method x #
method x(IO::Path:D: --> Bool:D)
Returns Bool:D.
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. NOTE: If the file is a script (an executable text file and not a native executable), and the file has only executable permissions and no read permissions, this method will return True but trying to execute will fail. That is a
method rwx #
method rwx(IO::Path:D: --> Bool:D)
Returns Bool:D.
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.
method z #
method z(IO::Path:D: --> Bool:D)
Returns Bool:D.
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.
method sibling #
method sibling(IO::Path:D: Str() $sibling --> IO::Path:D)
Returns IO::Path:D.
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 #
method words(IO::Path:D: :$chomp = True, :$enc = 'utf8', :$nl-in = ["\x0A", "\r\n"], |c --> Seq:D)
Returns Seq:D.
Opens the invocant and returns its words. The behavior 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
method lines #
method lines(IO::Path:D: :$chomp = True, :$enc = 'utf8', :$nl-in = ["\x0A", "\r\n"], |c --> Seq:D)
Returns Seq:D.
Opens the invocant and returns its lines. The behavior 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
routine slurp #
multi method slurp(IO::Path:D: :$bin, :$enc)
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. File will be closed afterwards. See &open for valid values for :$enc.
method spurt #
method spurt(IO::Path:D: $data, :$enc, :$append, :$createonly)
Opens the path for writing, and writes all of the $data into it. File will be closed afterwards. Will fail if it cannot succeed for any reason. The $data can be any Cool type or any Blob type. Arguments are as follows: in IO::Handle.open. Defaults to utf8. Ignored if $data is a Blob. contents, and appending data to the end of the file.
method chdir #
multi method chdir(IO::Path:D: IO $path, |c)
multi method chdir(IO::Path:D: Str() $path, :$d = True, :$r, :$w, :$x)
Contrary to the name, the .chdir method does not change any directories, but merely concatenates the given $path to the invocant and returns the resultant IO::Path. Optional file tests can be performed by providing :d, :r, :w, or :x Bool named arguments; when set to True, they'll perform .d, .r, .w, and .x tests respectively. By default,
method mkdir #
method mkdir(IO::Path:D: Int() $mode = 0o777 --> IO::Path:D)
Returns IO::Path:D.
Creates a new directory, including its 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 as well if they do not exist. Returns the IO::Path object pointing to the newly created directory on success; fails with X::IO::Mkdir if directory cannot be created.
routine rmdir #
sub rmdir(*@dirs --> List:D)
method rmdir(IO::Path:D: --> True)
Returns List:D or True.
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 returns a Failure 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
method chmod #
method chmod(IO::Path:D: Int() $mode --> Bool)
Returns Bool.
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
routine rename #
method rename(IO::Path:D: IO() $to, :$createonly = False --> Bool:D)
sub rename(IO() $from, IO() $to, :$createonly = False --> Bool:D)
Returns Bool:D.
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 #
method copy(IO::Path:D: IO() $to, :$createonly --> Bool:D)
sub copy(IO() $from, IO() $to, :$createonly --> Bool:D)
Returns Bool:D.
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 #
method move(IO::Path:D: IO() $to, :$createonly --> Bool:D)
sub move(IO() $from, IO() $to, :$createonly --> Bool:D)
Returns Bool:D.
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
method Numeric #
method Numeric(IO::Path:D: --> Numeric:D)
Returns Numeric:D.
Coerces .basename to Numeric. Fails with X::Str::Numeric if base name is not numerical.
method Int #
method Int(IO::Path:D: --> Int:D)
Returns Int:D.
Coerces .basename to Int. Fails with X::Str::Numeric if base name is not numerical.
routine symlink #
method symlink(IO::Path:D $target: IO() $link, Bool :$absolute = True --> Bool:D)
sub symlink( IO() $target, IO() $link, Bool :$absolute = True --> Bool:D)
Returns Bool:D.
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. symlink creates a symbolic link using an absolute path by default. To create a relative symlink set the absolute parameter to False e.g. :!absolute. This flag was
routine link #
method link(IO::Path:D $target: IO() $link --> Bool:D)
sub link( IO() $target, IO() $link --> Bool:D)
Returns Bool:D.
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.
routine unlink #
method unlink(IO::Path:D: --> True)
sub unlink(*@filenames --> List:D)
Returns True or List:D.
Delete all specified ordinary files, links, or symbolic links for which there are privileges to do so. See rmdir to delete directories. The subroutine form returns the names of all the files in the list, excluding those for which the filesystem raised some error; since trying to delete a file that does not exist does not raise any error at that level, this list will include the names of the files in the list that do not exist.
routine chown #
method chown(IO::Path:D: :$uid, :$gid --> True)
sub chown(*@filenames, :$uid, :$gid --> List:D)
Returns True or List:D.
Available as of release 2022.12 of the Rakudo compiler. Change the owner and/or group of all specified ordinary files, links, or symbolic links for which there are privileges to do so. The subroutine form returns the names of all the files in the list, excluding those for which the filesystem raised some error. The method form returns True on success, or fails with X::IO::Chown if the operation could not be completed.
method IO #
method IO(IO::Path:D: --> IO::Path)
Returns IO::Path.
Returns the invocant.
method SPEC #
method SPEC(IO::Path:D: --> IO::Spec)
Returns IO::Spec.
Returns the IO::Spec object that was (implicitly) specified at object creation time. There are also 3 methods for fetching the 3 timestamps of a file (inode), on Operating Systems where these are available:
method created #
Returns an Instant object indicating when the file was created. Available as of the 2022.12 release of the Rakudo compiler.
method modified #
Returns an Instant object indicating when the content of the file was last modified. Compare with changed.
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 metadata of the file or directory was last changed (e.g. permissions, or files created/deleted in directory). Compare with modified.
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.
method user #
Available as of the 2021.04 Rakudo compiler release. The user method returns the numeric user ID (aka "uid") of the path on operating systems that support such a notion.
method group #
Available as of the 2021.04 Rakudo compiler release. The group method returns the numeric group ID (aka "gid") of the path on operating systems that support such a notion.
method dir-with-entries #
Available as of the 2022.04 Rakudo compiler release. Returns a Bool indicating whether the path is a directory with any entries in it. Throws an exception if the path is not a directory, or the path doesn't exist.
method inode #
Available as of the 2022.07 Rakudo compiler release. Returns an Int object representing the inode of the path on the filesystem (if the filesystem supports such a notion).
method dev #
Available as of the 2022.07 Rakudo compiler release. Returns an Int object representing the dev (the st_dev field of the POSIX stat function) of the path on the filesystem (if the filesystem supports such a notion).
method devtype #
Available as of the 2022.07 Rakudo compiler release. Returns an Int object representing the devtype (the st_rdev field of the POSIX stat function) of the path on the filesystem (if the filesystem supports such a notion).
Examples, run three ways #
Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.
5 all-differ · 32 no-output · 1 not-runnable · 27 ok · 1 rakupp-differs
class IO::Path is Cool does IO { }Not executed: the documentation states no expected output for this example.
:$basename!, :$dirname = '.', :$volume = '' IO::Spec :$SPEC = $*SPEC, Str() :$CWD = $*CWD )
Not executed: the documentation states no expected output for this example.
IO::Path.new("foo", :CWD</home/camelia>)
.CWD.say; # OUTPUT: «/home/camelia»/home/camelia
Documentation, Rakudo and Raku++ all agree.
IO::Path.new("foo", :SPEC(IO::Spec::Unix.new))\
.SPEC.^name.say; # OUTPUT: «IO::Spec::Unix»IO::Spec::Unix
Documentation, Rakudo and Raku++ all agree.
IO::Path.new("foo").path.say; # OUTPUT: «foo»foo
Documentation, Rakudo and Raku++ all agree.
say "foo/../bar".IO ~~ "bar".IO # False
Not executed: the documentation states no expected output for this example.
say "foo/../bar".IO.resolve(:completely) ~~ "bar".IO.resolve(:completely) # True
Not executed: the documentation states no expected output for this example.
"docs/README.pod".IO.basename.say; # OUTPUT: «README.pod» "/tmp/".IO.basename.say; # OUTPUT: «tmp»
README.pod
tmp
Documentation, Rakudo and Raku++ all agree.
IO::Path::Win32.new('//server/share').basename.say; # OUTPUT: «\»\
Documentation, Rakudo and Raku++ all agree.
"foo/bar".IO.mkdir;
"foo/bar".IO.add("meow") .resolve.relative.say; # OUTPUT: «foo/bar/meow»
"foo/bar".IO.add("/meow") .resolve.relative.say; # OUTPUT: «foo/bar/meow»
"foo/bar".IO.add("meow.txt").resolve.relative.say; # OUTPUT: «foo/bar/meow.txt»
"foo/bar".IO.add("../meow") .resolve.relative.say; # OUTPUT: «foo/meow»
"foo/bar".IO.add("../../") .resolve.relative.say; # OUTPUT: «.»foo/bar/meow
foo/bar/meow
foo/bar/meow.txt
foo/meow
.
foo/bar/meow
foo/bar//meow
foo/bar/meow.txt
foo/bar/../meow
foo/bar/../../
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
method add(IO::Path:D: *@parts --> IO::Path:D)
Not executed: the documentation states no expected output for this example.
"foo".IO.add(<bar baz>).resolve.relative.say; # OUTPUT: «foo/bar/baz»
foo/bar/baz
Documentation, Rakudo and Raku++ all agree.
"foo/./././..////bar".IO.cleanup.say; # OUTPUT: «"foo/../bar".IO»
IO::Path::Win32.new("foo/./././..////bar")
.cleanup.say; "foo\..\bar".IO; # OUTPUT: «"foo\..\bar".IO»"foo/../bar".IO
"foo\..\bar".IO
Documentation, Rakudo and Raku++ all agree.
# Getting an extension: say "foo.tar.gz".IO.extension; # OUTPUT: «gz» say "foo.tar.gz".IO.extension: :2parts; # OUTPUT: «tar.gz» say "foo.tar.gz".IO.extension: :parts(^5); # OUTPUT: «tar.gz» say "foo.tar.gz".IO.extension: :parts(0..1); # OUTPUT: «gz»
gz
tar.gz
tar.gz
gz
Documentation, Rakudo and Raku++ all agree.
# Replacing an extension say "foo.tar.gz".IO.extension: ''; # OUTPUT: «"foo.tar".IO» say "foo.tar.gz".IO.extension: 'ZIP'; # OUTPUT: «"foo.tar.ZIP".IO» say "foo.tar.gz".IO.extension: 'ZIP', :0parts; # OUTPUT: «"foo.tar.gz.ZIP".IO» say "foo.tar.gz".IO.extension: 'ZIP', :2parts; # OUTPUT: «"foo.ZIP".IO» say "foo.tar.gz".IO.extension: 'ZIP', :parts(^5); # OUTPUT: «"foo.ZIP".IO»
"foo.tar".IO
"foo.tar.ZIP".IO
"foo.tar.gz.ZIP".IO
"foo.ZIP".IO
"foo.ZIP".IO
Documentation, Rakudo and Raku++ all agree.
# Replacing an extension using non-standard joiner:
say "foo.tar.gz".IO.extension: '', :joiner<_>; # OUTPUT: «"foo.tar_".IO»
say "foo.tar.gz".IO.extension: 'ZIP', :joiner<_>; # OUTPUT: «"foo.tar_ZIP".IO»
say "foo.tar.gz".IO.extension: 'ZIP', :joiner<_>,
:2parts; # OUTPUT: «"foo_ZIP".IO»
say "foo.tar.gz".IO.extension: 'ZIP', :joiner<_>,
:parts(^5); # OUTPUT: «"foo_ZIP".IO»"foo.tar_".IO
"foo.tar_ZIP".IO
"foo_ZIP".IO
"foo_ZIP".IO
Documentation, Rakudo and Raku++ all agree.
# EDGE CASES:
Not executed: the documentation states no expected output for this example.
# There is no 5-part extension, so returned value is an empty string say "foo.tar.gz".IO.extension: :5parts; # OUTPUT: «»
Documentation, Rakudo and Raku++ all agree.
# There is no 5-part extension, so we replaced nothing: say "foo.tar.gz".IO.extension: 'ZIP', :5parts; # OUTPUT: «"foo.tar.gz".IO»
"foo.tar.gz".IO
Documentation, Rakudo and Raku++ all agree.
# Replacing a 0-part extension is just appending: say "foo.tar.gz".IO.extension: 'ZIP', :0parts; # OUTPUT: «"foo.tar.gz.ZIP".IO»
"foo.tar.gz.ZIP".IO
Documentation, Rakudo and Raku++ all agree.
# Replace 1-part of the extension, using '.' joiner say "...".IO.extension: 'tar'; # OUTPUT: «"...tar".IO»
"...tar".IO
Documentation, Rakudo and Raku++ all agree.
# Replace 1-part of the extension, using empty string joiner
say "...".IO.extension: 'tar', :joiner(''); # OUTPUT: «"..tar".IO»"..tar".IO
Documentation, Rakudo and Raku++ all agree.
# Remove 1-part extension; results in empty basename, so result is ".".IO say ".".IO.extension: ''; # OUTPUT: «".".IO»
".".IO
Documentation, Rakudo and Raku++ all agree.
say IO::Path.new("/home/camelia/myfile.raku").dirname; # OUTPUT: «/home/camelia»
say IO::Path::Win32.new("C:/home/camelia").dirname; # OUTPUT: «/home»
say IO::Path.new("/home").dirname; # OUTPUT: «/»/home/camelia
/home
/
Documentation, Rakudo and Raku++ all agree.
say IO::Path::Win32.new("C:\\Windows\\registry.ini").volume; # OUTPUT: «C:»C:
Documentation, Rakudo and Raku++ all agree.
say IO::Path::Win32.new("C:/rakudo/raku.bat").parts.raku;
# OUTPUT: «IO::Path::Parts.new("C:","/rakudo","raku.bat")»IO::Path::Parts.new("C:","/rakudo","raku.bat")
Documentation, Rakudo and Raku++ all agree.
"foo/bar".IO.raku.say;
# OUTPUT: IO::Path.new("foo/bar", :SPEC(IO::Spec::Unix), :CWD("/home/camelia"))Not executed: the documentation states no expected output for this example.
say "foo/bar".IO; # OUTPUT: «"foo/bar".IO» say IO::Path::Win32.new: 「C:\foo/bar\」; # OUTPUT: «"C:\foo/bar\".IO»
"foo/bar".IO
"C:\foo/bar\".IO
Documentation, Rakudo and Raku++ all agree.
"foo/file_02.txt".IO.succ.say; # OUTPUT: «"foo/file_03.txt".IO»
"foo/file_03.txt".IO
Documentation, Rakudo and Raku++ all agree.
"foo/file02.txt".IO.pred.say; # OUTPUT: «"foo/file01.txt".IO»
"foo/file01.txt".IO
Documentation, Rakudo and Raku++ all agree.
"/foo".IO.is-absolute.say; # OUTPUT: «True» "bars".IO.is-absolute.say; # OUTPUT: «False»
True
False
Documentation, Rakudo and Raku++ all agree.
IO::Path::Win32.new("/foo" ).is-absolute.say; # OUTPUT: «True»
IO::Path::Win32.new("C:/foo").is-absolute.say; # OUTPUT: «True»
IO::Path::Win32.new("C:foo" ).is-absolute.say; # OUTPUT: «False»True
True
False
Documentation, Rakudo and Raku++ all agree.
'/etc/foo'.IO.parent.say; # OUTPUT: «"/etc".IO»
'/etc/..' .IO.parent.say; # OUTPUT: «"/etc".IO»
'/etc/../'.IO.parent.say; # OUTPUT: «"/etc".IO»
'./' .IO.parent.say; # OUTPUT: «"..".IO»
'foo' .IO.parent.say; # OUTPUT: «".".IO»
'/' .IO.parent.say; # OUTPUT: «"/".IO»
IO::Path::Win32.new('C:/').parent.say; # OUTPUT: «"C:/".IO»"/etc".IO
"/etc".IO
"/etc".IO
"..".IO
".".IO
"/".IO
"C:/".IO
"/etc".IO
"/etc".IO
"/etc".IO
"..".IO
".".IO
"/".IO
"C:/".IO
"/etc".IO
"/etc".IO
"/etc".IO
".".IO
".".IO
"/".IO
".".IO
Raku++ disagrees with both the documentation and Rakudo — a defect.
say "/etc/foo".IO.parent(2) eqv "/etc/foo".IO.parent.parent; # OUTPUT: «True»
True
Documentation, Rakudo and Raku++ all agree.
# bar is a symlink pointing to "/baz" my $io = "foo/./bar/..".IO.resolve; # now "/" (the parent of "/baz")
Not executed: the documentation states no expected output for this example.
dir('mydir', test => { .IO.d })Not executed: the documentation states no expected output for this example.
dir('mydir', test => { "mydir/$_".IO.d })Not executed: the documentation states no expected output for this example.
# To iterate over the contents of the current directory:
for dir() -> $file {
say $file;
}Not executed: the documentation states no expected output for this example.
# As before, but include even '.' and '..' which are filtered out by
# the default :test matcher:
for dir(test => *) -> $file {
say $file;
}Not executed: the documentation states no expected output for this example.
# To get the names of all .jpg and .jpeg files in the home directory of the current user: my @jpegs = $*HOME.dir: test => /:i '.' jpe?g $/;
Not executed: the documentation states no expected output for this example.
sub MAIN($dir = '.') {
my @todo = $dir.IO;
while @todo {
for @todo.pop.dir -> $path {
say $path.Str;
@todo.push: $path if $path.d;
}
}
}Not executed: the documentation states no expected output for this example.
my @stack = '.'.IO;
my $raku-files = gather while @stack {
with @stack.pop {
when :d { @stack.append: .dir }
.take when .extension.lc eq 'raku'
}
}
.put for $raku-files[^3];Not executed: the documentation states no expected output for this example.
'/path/to/file'.IO ~~ :r;
Not executed: the documentation states no expected output for this example.
say :d & :x; # OUTPUT: «all(d => True, x => True)» say '/tmp'.IO ~~ :d & :x; # OUTPUT: «True» say '/'.IO ~~ :d & :rw; # OUTPUT: «False»
all(d => True, x => True)
True
False
Documentation, Rakudo and Raku++ all agree.
$fh.path ~~ :r; $fh.path.r; # method form
Not executed: the documentation states no expected output for this example.
say $*EXECUTABLE.IO.s; # OUTPUT: «467»
467
15344
9185848
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
say '.bashrc'.IO.sibling: '.bash_aliases'; # OUTPUT: «.bash_aliases".IO» say '/home/camelia/.bashrc'.IO.sibling: '.bash_aliases'; # OUTPUT: «/home/camelia/.bash_aliases".IO»
.bash_aliases".IO
/home/camelia/.bash_aliases".IO
".bash_aliases".IO
"/home/camelia/.bash_aliases".IO
"./.bash_aliases".IO
"/home/camelia/.bash_aliases".IO
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
say '/foo/' .IO.sibling: 'bar'; # OUTPUT: «/bar".IO» say '/foo/.'.IO.sibling: 'bar'; # OUTPUT: «/foo/bar".IO»
/bar".IO
/foo/bar".IO
"/bar".IO
"/foo/bar".IO
"//bar".IO
"/foo/bar".IO
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
my %dict := bag 'my-file.txt'.IO.words; say "Most common words: ", %dict.sort(-*.value).head: 5;
Not executed: the documentation states no expected output for this example.
say "The file contains ", '50GB-file'.IO.lines.grep(*.contains: 'Raku').elems, " lines that mention Raku"; # OUTPUT: «The file contains 72 lines that mention Raku»
Neither engine can run this in isolation — the example depends on context from the surrounding text.
method spurt(IO::Path:D:)
Not executed: the documentation states no expected output for this example.
'myfile'.IO.chmod(0o444); # make a file read-only 'somedir'.IO.chmod(0o777); # set 0777 permissions on a directory
Not executed: the documentation states no expected output for this example.
'myfile'.IO.chmod: '0444'; # BAD!!! (interpreted as mode 0o674) 'myfile'.IO.chmod: '0o444'; # OK (an octal in a string) 'myfile'.IO.chmod: 0o444; # Also OK (an octal literal)
Not executed: the documentation states no expected output for this example.
'foo.txt'.IO.open(:w).close; 'bar'.IO.mkdir; say unlink <foo.txt bar not-there.txt>; # OUTPUT: «[foo.txt not-there.txt]» # `bar` is not in output because it failed to delete (it's a directory) # `not-there.txt` is present. It never existed, so that's deemed a success. # Method form `fail`s: say .exception.message without 'bar'.IO.unlink; # OUTPUT: «Failed to remove the file […] illegal operation on a directory»
[foo.txt not-there.txt]
Failed to remove the file […] illegal operation on a directory
[foo.txt not-there.txt]
Failed to remove the file '/private/tmp/typerun-sandbox/bar': Failed to delete file: operation not permitted
True
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
say "success" if 'foo.txt'.IO.chown(:uid(137));
Not executed: the documentation states no expected output for this example.
my $io = IO::Path.new("/bin/bash");
say $io.SPEC; # OUTPUT: «(Unix)»
say $io.SPEC.dir-sep; # OUTPUT: «/»(Unix)
/
Documentation, Rakudo and Raku++ all agree.
say "path/to/file".IO.created; # Instant:1424089165 say "path/to/file".IO.created.DateTime; # 2015-02-16T12:18:50Z
Not executed: the documentation states no expected output for this example.
say "path/to/file".IO.modified; # Instant:1424089165 say "path/to/file".IO.modified.DateTime; # 2015-02-16T12:18:50Z
Not executed: the documentation states no expected output for this example.
say "path/to/file".IO.accessed; # Instant:1424353577 say "path/to/file".IO.accessed.DateTime; # 2015-02-19T13:45:42Z
Not executed: the documentation states no expected output for this example.
say "path/to/file".IO.changed; # Instant:1424089165 say "path/to/file".IO.changed.DateTime; # 2015-02-16T12:18:50Z
Not executed: the documentation states no expected output for this example.
say ~"path/to/file".IO.mode; # e.g. '0644'
say +"path/to/file".IO.mode; # e.g. 420, where sprintf('%04o', 420) eq '0644'Not executed: the documentation states no expected output for this example.
"path/to/file1".IO.chmod("path/to/file2".IO.mode); # will change the
# permissions of file1
# to be the same as file2Not executed: the documentation states no expected output for this example.
say "'$_' has entries" if .d && .dir-with-entries given "path/to/dir".IO;
Not executed: the documentation states no expected output for this example.
say "path/to/file".IO.inode; # e.g. 9003678
Not executed: the documentation states no expected output for this example.
say "path/to/file".IO.dev; # e.g. 16777233
Not executed: the documentation states no expected output for this example.
say "path/to/file".IO.devtype; # e.g. 0
Not executed: the documentation states no expected output for this example.