Raku
2015fragletMCP + fragletc
Raku is a language that evolved out of Larry Wall and co's goal of developing a next generation language to succeed Perl 5. Originally dubbed Perl 6, the changes from Perl 5 became so significant that the decision was made to rename the language to avoid confusing potential users. As noted in Wikipedia:
An implication of these goals was that Perl 6 would not have backward compatibility with the existing Perl codebase. This meant that some code which was correctly interpreted by a Perl 5 compiler would not be accepted by a Perl 6 compiler. Since backward compatibility is a common goal when enhancing software, the breaking changes in Perl 6 had to be stated explicitly. The distinction between Perl 5 and Perl 6 became so large that eventually Perl 6 was renamed Raku.
Raku's lineage as a Perl successor is made obvious by its use of distinctive language features like sigils, the use of "my" and "sub" for defining local variables and functions, and so forth. However, the language diverges significantly, including introducing a type system, formal parameter passing lists for functions (in Perl parameters can be passed implicitly through the @_ variable), and an advanced class system supporting multiple dispatch and other features.
Raku also builds on Perl 5's inclusion of regular expressions in the core language by introducing a built-in, full-blown lexing and parsing system referred to as rules, making Raku, like Perl 5 before it, uniquely well suited to text processing.
The included example program demonstrates the definition of a basic class with instance variables, as well as class instantiation, method invocation, parameter passing, and string interpolation.
Hello World
#!/usr/bin/env raku
# BEGIN_FRAGLET
class Greeting {
has Str $.who;
method greet() {
say "Hello $!who!";
}
}
my $greeting = Greeting.new(who => "World");
$greeting.greet();
# END_FRAGLETCoding Guide
Language Version
Raku (Rakudo)
Execution Model
- Interpreted, runs directly from source
- Code executes at the top level
- Script runs once and exits
- Shebang support:
#!/usr/bin/env raku
Key Characteristics
- Dynamic typing with optional type annotations
- Case-sensitive
- Variables use sigils:
$(scalar),@(array),%(hash),&(code) - Object-oriented with classes, roles, and grammars
- Multiple dispatch (multi methods)
- Built-in regex engine (rules/grammars)
- Gradual typing (can add type constraints)
- Automatic memory management
Fragment Authoring
Write valid Raku statements. Your fragment becomes the script body. Code runs at the top level of the script, so statements execute immediately. You can define classes, subroutines, use variables, and call functions.
Available Packages
Standard Raku library is available. The container includes zef (Rakudo package manager) for installing additional modules if needed. Core modules include:
- Standard library functions and types
- Regex and grammar support
- Concurrency primitives (promises, channels, supplies)
- File I/O operations
- JSON support (if installed)
Common Patterns
- Output:
say "message";orput "message"; - Variables:
my $name = "Alice"; - Arrays:
my @list = 1, 2, 3; - Hashes:
my %hash = key => "value"; - Subroutines:
sub greet($name) { return "Hello, $name!"; } - Classes:
class Person { has Str $.name; } - Conditionals:
if $condition { ... } else { ... } - Loops:
for @list -> $item { ... } - String interpolation:
"Hello, $name!" - Method calls:
$object.method();
Examples
# Simple output
say "Hello, World!";
# Variables and string interpolation
my $name = "Alice";
say "Hello, $name!";
# Arrays and list processing
my @numbers = 1, 2, 3, 4, 5;
my $sum = [+] @numbers.map(* ** 2);
say "Sum of squares: $sum";
# Subroutines
sub greet(Str $name --> Str) {
return "Hello, $name!";
}
say greet("World");
# Hashes
my %colors = (
red => "#FF0000",
green => "#00FF00",
blue => "#0000FF"
);
say "Red: %colors<red>";
# Classes
class Person {
has Str $.name;
has Int $.age;
method introduce() {
say "I'm $.name, $.age years old";
}
}
my $person = Person.new(name => "Alice", age => 30);
$person.introduce();
# Regular expressions
my $text = "Hello World";
$text ~~ s/World/Raku/;
say $text;
# List processing with map and reduce
my @squared = (1..5).map(* ** 2);
say "Squares: @squared.join(', ')";Caveats
- Always use
myto declare variables (lexical scope) - String interpolation only works in double quotes, not single quotes
- Array and hash access use different syntax:
@array[0]vs%hash<key>or%hash{'key'} - Method calls use
.(dot) operator - Type constraints are optional but can be added:
sub add(Int $a, Int $b --> Int) { ... } sayadds a newline,printdoes not- Raku uses
~~for smart matching, not=~like Perl - Junction operators (
|,&,^) provide powerful logic operations
Fraglet Scripts
Echo Args
#!/usr/bin/env -S fragletc --vein=raku
say "Args: @*ARGS.join(' ')";Raku Fortune
#!/usr/bin/env -S fragletc --vein=raku
# One from the vault: Raku fortunes
say "Raku: the language that gives you a sub for that. And a method. And an operator. And a metaoperator.";Stdin Upper
#!/usr/bin/env -S fragletc --vein=raku
for $*IN.lines() -> $line {
say $line.uc;
}Test
#!/usr/bin/env -S fragletc --vein=raku
class Greeting {
has Str $.who;
method greet() {
say "Hello $!who!";
}
}
my $greeting = Greeting.new(who => "World");
$greeting.greet();