Raku By Example
View me onGitHub
## lines

.say for "share.txt".IO.lines.unique;

## slurp

my %words;
for slurp.comb(/\w+/).map(*.lc) -> $word {
    %words{$word}++;
}
say %words.kv;

## dir

for dir('.', test => *) -> $file {
   rename $file, $file.lc if $file.f;
}

## unlink files

# create a file
my $f = open "foo",:w;
$f.print(time);
$f.close;

unlink "foo";

## copy and move

# create a file
my $f = open "foo",:w;
$f.print(time);
$f.close;

# copy
my $io = IO::Path.new("foo");
$io.copy("foo2");

# clean up
unlink ("foo2");

# move
$io.rename("foo2");

# clean up
unlink ("foo2");

## slurp

my $something;
repeat {
  $something = prompt 'enter something: ';
  say $something;
} while $something;

## lines

# There are better ways of doing this
my $filename = shift @*ARGS;

# I think the file is automatically opened and closed
for $filename.IO.lines -> $line {
  say $line;
}

## lines()

# for $*ARGFILES.lines { ... }
for lines() {
  .say;
}

## open 

my $filename = shift(@*ARGS);

my $fh = open $filename, :a;

my $something;
repeat {
  $something = prompt 'enter something: ';
  $fh.say($something);
} while $something;


## CATCH

my $total = 0;
for @files -> $filename {
    $total += lines($filename.IO).grep(
        { $_ !~~ /<&insigline>/ }
).elems;
 CATCH {
        when X::IO {
            note "Couldn't read $filename";
} }
}
say $total;
# As CATCH goes inside of the scope,we can see the scope's lexicals!