NAME

Perl6::Overview::File - File and Filesystem operations Structure

DESCRIPTION

Files

FH = open FILENAME, MODE
# FH is the returned filehandle
# FILENAME is, well the name of the file
# MODE can be :r :w :a :rw
# if MODE left out it defaults to :r

my $fh = open "filename", :r orelse die "Could not open file $!";

my $row = =$fh;                           # reading a line

my @rows = =$fh;                          # reading all the lines

my $content = slurp "filename";
                # slurp the content of a file into a scalar variable

close $fh;

Directories

my $dh = opendir "dirname" orelse die "Could not open directory $!";

my @files = readdir($dh);
my @files = $dh.readdir;

for $dh.readdir -> $entry {  # read them one-by-one
    say $entry;
}

closedir($dh);
$dh.closedir;

rewinddir($dh);               # start from the beginning
$dh.rewinddir;

File and Directory tests

"thing" ~~ :e                     # true if thing exists in the filesystem
"thing" ~~ :d                     # true on directories
"thing" ~~ :f                     # true on files
"thing" ~~ :r                     # true on readable files/directories
"thing" ~~ :w                     # true on writable files/directories
"thing" ~~ :x                     # true on executable files/directories

"file" ~~ :z                      # true if size is 0
"file" ~~ :s                      # returns file size in bytes


mkdir "dirname"
rmdir "dirname"
chdir "dirname"

unlink
chmod
chown