role IO::FileTestable

Common role for paths that can be tested for filesystem metadata

role IO::FileTestable does IO { }


File Test operators

-f Does not exist in Perl 6. See :e, :f.
-M Does not exist in Perl 6. See C<modified>.
-A Does not exist in Perl 6. See C<accessed>.
-C Does not exist in Perl 6. See C<changed>.
:e Exists
:d Directory
:f File
:l Symbolic link
:r Readable
:w Writable
:x Executable
:s Size
:z Zero size


If you have a string - a path to something in the filesystem:

if "path/to/file".IO ~~ :e {
    say 'file exists';
my $file = "path/to/file";
if $file.IO ~~ :e {
    say 'file exists';

If you already have an IO object in $file, either by creating one yourself, or by getting it from another subroutine, such as dir, you can write this:

my $file = "path/to/file".IO;
if $file ~~ :e {
    say 'file exists';

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

method modified

Timestamp when the file was last modified.


method accessed

Timestamp when the file was last accessed.


method changed

Timestamp when the inode was last changed.


Type graph

Below you should see an image showing the type relations for IO::FileTestable. If not, try the PNG version.

perl6-type-graph IO::FileTestable IO::FileTestable IO IO IO::FileTestable->IO

Methods supplied by role IO

IO::FileTestable does role IO, which provides the following methods:

method prompt

sub prompt($msg)

Prints $msg to the standard output and waits for the user to type in something and finish with an ENTER. Returns the string typed in without the trailing newline.

my $name = prompt("Hi, what's your name? ");

method dir

sub dir(Cool $path = '.', Mu :$test = none('.', '..'))

Returns a list of IO::File and IO::Path objects for the files and directories found in the $path. If $path is not given assumes the current directory.

A second optional parameter can be given that will be matched against the strings to filter out certain entries. By default it filters out the '.' and '..' entries.


for dir() -> $file {
   say $file;

To include all the entries (including . and ..) write:

dir(test => all())

To include only entries with a .pl extension write:

dir(test => /.pl$/)

TODO: more IO Ops