Beginner

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:

1
say "Hello, World!";

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’s say feature.
  • "Hello, World!" - A double-quoted string (allows interpolation).
  • Semicolon - Statement terminator (optional for the last statement in a block).
  • .raku extension - The modern Raku file extension. .p6 and .pl6 also work but are legacy.

Running with Docker

The easiest way to run this without installing Raku locally:

1
2
3
4
5
# Pull the Rakudo Star image
docker pull rakudo-star:alpine

# Run your script
docker run --rm -v $(pwd):/app -w /app rakudo-star:alpine raku hello.raku

Running Locally

If you have Rakudo installed:

1
2
# Run the script directly
raku hello.raku

Or use the interactive REPL:

1
2
3
4
5
6
# Start interactive Raku
raku

# Then type:
> say "Hello, World!";
Hello, World!

Expected Output

Hello, World!

Key Concepts

  1. say vs print - say adds a newline automatically; print does not
  2. Gradual typing - Types are optional; add them when you want safety
  3. Unicode everywhere - Full Unicode support in strings and identifiers
  4. Perl heritage - Many concepts familiar to Perl programmers
  5. 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”):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# The classic way
say "Hello, World!";

# Method call syntax
"Hello, World!".say;

# Without parentheses (common Raku style)
say "Hello, World!"

# Using print (no automatic newline)
print "Hello, World!\n";

# Unicode output works too
say "Hello, World! 🦋";

A More Raku-Style Example

1
2
3
4
5
sub greet(Str $name --> Str) {
    "Hello, $name!"
}

say greet("World");

This demonstrates:

  • sub - Defines a subroutine (function)
  • Type signatures - Str $name requires 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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Greeter {
    has Str $.name = "World";

    method greet(--> Str) {
        "Hello, $!name!"
    }
}

my $greeter = Greeter.new;
say $greeter.greet;  # Hello, World!

my $custom = Greeter.new(:name<Raku>);
say $custom.greet;   # Hello, Raku!

Key concepts:

  • class - Defines a class
  • has - 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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$ raku
Welcome to Rakudo™ v2025.12.
To exit type 'exit' or '^D'
> my $x = 42
42
> $x.WHAT
(Int)
> $x.is-prime
True
> "Hello".chars
5
> (1, 2, 3).sum
6

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
Last updated: