Hello World in Raku
Your first Raku program - the classic Hello World example with Docker setup
Every programming journey starts with Hello World. Let’s write our first Raku program.
The Code
Create a file named hello.raku:
| |
That’s it! One line to print to the console.
Understanding the Code
say- Raku’s print function that automatically adds a newline. Inspired by Perl 5’ssayfeature."Hello, World!"- A double-quoted string (allows interpolation).- Semicolon - Statement terminator (optional for the last statement in a block).
.rakuextension - The modern Raku file extension..p6and.pl6also work but are legacy.
Running with Docker
The easiest way to run this without installing Raku locally:
| |
Running Locally
If you have Rakudo installed:
| |
Or use the interactive REPL:
| |
Expected Output
Hello, World!
Key Concepts
sayvsprint-sayadds a newline automatically;printdoes not- Gradual typing - Types are optional; add them when you want safety
- Unicode everywhere - Full Unicode support in strings and identifiers
- Perl heritage - Many concepts familiar to Perl programmers
- MoarVM - Runs on a custom virtual machine designed for Raku
Alternative Syntaxes
Raku offers multiple ways to do the same thing (TIMTOWTDI - “There Is More Than One Way To Do It”):
| |
A More Raku-Style Example
| |
This demonstrates:
sub- Defines a subroutine (function)- Type signatures -
Str $namerequires a string argument - Return type -
-->Str specifies the return type - String interpolation - Variables in double quotes are expanded
- Implicit return - The last expression is returned automatically
Using a Class
Raku has built-in object-oriented programming:
| |
Key concepts:
class- Defines a classhas- Declares an attribute$.name- Public accessor (twigil.means public)$!name- Private attribute access:name<Raku>- Named argument using colon-pair syntax
The REPL
Raku’s REPL is great for exploration:
| |
The .WHAT method shows the type of any value—very useful for learning!
File Extensions
.raku- Recommended modern extension.rakumod- For module files.p6- Legacy Perl 6 extension (still works).pm6- Legacy Perl 6 module extension
Next Steps
Continue to learn about Raku’s powerful features like gradual typing, grammars for parsing, and built-in concurrency with promises and channels.
Running Today
All examples can be run using Docker:
docker pull rakudo-star:alpine