role IO::FileTestable

Common role for paths that can be tested for filesystem metadata

role IO::FileTestable does IO { }

Methods

File Test operators

-e -f

The -e and -f file test operators do not exist in Perl 6. Use instead :e and :f.

-M -A -C

The -M, -A and -C file test operators do not exist in Perl 6, use instead the modified, accessed and changed methods instead.

:e :d :f :l :r :w :x :s :z

:e Exists
:d Directory
:f File
:l Symbolic link
:r Readable
:w Writable
:x Executable
:s Size
:z Zero size

Usage:

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';
}

Instead of the colonpair syntax, you can use method calls too:

if 'path/to/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';
}

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

Return an Instant object representing the timestamp when the file was last modified.

say "path/to/file".IO.modified;   #  e.g. Instant:1424089165

To obtain a human-readable form of the timestamp, use a DateTime object:

say DateTime.new("path/to/file".IO.modified);  # e.g. 2015-02-16T12:18:50Z

or more readably:

my $modification_instant = "path/to/file".IO.modified;
my $modification_time = DateTime.new($modification_instant);
say $modification_time;         # e.g. 2015-02-16T12:18:50Z

method accessed

Return an Instant object representing the timestamp when the file was last accessed.

say "path/to/file".IO.accessed;   #  e.g. Instant:1424353577

To obtain a human-readable form of the timestamp, use a DateTime object:

say DateTime.new("path/to/file".IO.accessed);  # e.g. 2015-02-19T13:45:42Z

or more readably:

my $access_instant = "path/to/file".IO.accessed;
my $access_time = DateTime.new($access_instant);
say $access_time;         # e.g. 2015-02-19T13:45:42Z

method changed

Return an Instant object representing the timestamp when the inode was last changed.

"path/to/file".IO.changed;        #  e.g. Instant:1424089165

To obtain a human-readable form of the timestamp, use a DateTime object:

say DateTime.new("path/to/file".IO.changed);  # e.g. 2015-02-16T12:18:50Z

or more readably:

my $change_instant = "path/to/file".IO.changed;
my $change_time = DateTime.new($chnge_instant);
say $change_time;         # e.g. 2015-02-16T12:18:50Z

Related roles and classes

See also the related role IO and the related classes IO::Handle and IO::Path.

Type graph

Below you should see a clickable image showing the type relations for IO::FileTestable that links to the documentation pages for the related types. If not, try the PNG version instead.

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 close

To close an open file handle, simply call its close method:

my $fh = open("path/to/file");
# ... do stuff with the file
$fh.close;

It is also possible to call this as a sub, thus the example above can be written equivalently like so:

my $fh = open("path/to/file");
# ... do stuff with the file
close $fh;

When a file was opened for writing, closing it is important to ensure that all contents are actually written to the file.

This documentation was generated from IO/FileTestable.pod.