Raku Programming Language Cheatsheet

If you’re diving into the world of Raku programming, you’ve chosen a versatile and expressive language that combines elements of Perl, Python, and other languages. To help you navigate the syntax and features of Raku more efficiently, we’ve put together a cheatsheet that covers essential aspects of the language. Whether you’re a beginner or an experienced developer, this quick reference guide will assist you in writing clean and concise Raku code.

Raku Basics

Hello World

say "Hello, World!";

Variables

my $variable = 42;

Strings

my $string = "Raku is awesome!";

Arrays

my @array = 1, 2, 3;

Hashes

my %hash = "key" => "value";

Control Structures

If statement

if $condition {
    # code to execute if condition is true
}

For loop

for @array {
    # code to execute for each element in the array
}

While loop

while $condition {
    # code to execute while condition is true
}

Subroutines

Declaring a Subroutine

sub greet($name) {
    say "Hello, $name!";
}

Calling a Subroutine

greet("Alice");

Object-Oriented Programming

Defining a Class

class Dog {
    has $.name;

    method bark() {
        say "Woof, Woof!";
    }
}

Creating an Object

my $dog = Dog.new(name => "Buddy");

Calling a Method

$dog.bark();

Regex and Pattern Matching

Matching

if "Raku is fun" ~~ /fun/ {
    say "Match found!";
}

Capture Groups

"Hello, World" ~~ /(\w+), (\w+)/;
say $0; # Hello
say $1; # World

File I/O

Reading from a File

my $file-content = slurp "filename.txt";

Writing to a File

spurt "output.txt", "Hello, World!";

Error Handling

Try-Catch

try {
    # code that may throw an exception
}
catch {
    say "Error: $_";
}

Concurrency

Promises

my $promise = start {
    # code to run asynchronously
}

await $promise;

Conclusion

This cheatsheet provides a glimpse into the core features of the Raku programming language. Keep in mind that Raku is known for its flexibility, so don’t hesitate to explore its extensive documentation for more advanced topics and functionalities. With this quick reference guide, you’re well-equipped to start writing efficient and elegant code in Raku. Happy coding!

You can refer to the official Raku documentation for more in-depth information on each topic.

FAQ

What is Raku, and how does it differ from other programming languages?

Raku is a versatile language that amalgamates features from Perl, Python, and more. Its expressive syntax and built-in functionalities set it apart for diverse programming needs.

How can I handle errors in Raku programs?

Raku offers a robust error-handling mechanism through the try-catch construct. Wrap potentially problematic code in a try block and handle exceptions gracefully in the catch block.

What’s the significance of regex in Raku, and how can I use it efficiently?

Raku excels in pattern matching with its powerful regex capabilities. Use the ~~ operator for matching and explore capture groups to extract specific information from strings.

Is Raku suitable for object-oriented programming (OOP)?

Absolutely! Raku embraces OOP principles with ease. Define classes, create objects, and call methods seamlessly to implement robust and modular code structures.

How does Raku handle concurrency, and what are Promises?

Raku simplifies concurrent programming with Promises. Use the start keyword to initiate asynchronous tasks and await to synchronize their completion, enhancing program efficiency.