perlsyn - Perl syntax

DESCRIPTION §

A (hopefully) comprehensive description of the differences between Perl and Raku with regards to the syntax elements described in the perlsyn document.

NOTE §

I will not be explaining Raku syntax in detail. This document is an attempt to guide you from how things work in Perl to the equivalents in Raku. For full documentation on the Raku syntax, please see the Raku documentation.

Free form §

Raku is still largely free form. However, there are a few instances where the presence or lack of whitespace is now significant. For instance, in Perl, you can omit a space following a keyword (e. g. while($x < 5) or my($x, $y)). In Raku, that space is required, thus while ($x < 5) or my ($x, $y). In Raku, however, you can omit the parentheses altogether: while $x < 5 . This holds for if, for, etc.

Oddly, in Perl, you can leave spaces between an array or hash and its subscript, and before a postfix operator. So $seen {$_} ++ is valid. No more. That would now have to be %seen{$_}++.

If it makes you feel better, you can use backslashes to "unspace" whitespace, so you can use whitespace where it would otherwise be forbidden.

See Whitespace for details.

Declarations §

As noted in the Functions guide, there is no undef in Raku. A declared, but uninitialized scalar variable will evaluate to its type. In other words, my $x;say $x; will give you "(Any)". my Int $y;say $y; will give you "(Int)".

Comments §

# starts a comment that runs to the end of the line as in Perl.

Embedded comments start with a hash character and a backtick (#`), followed by an opening bracketing character, and continue to the matching closing bracketing character. Like so:

if #`( why would I ever write an inline comment here? ) True {
    say "something stupid";
}

As in Perl, you can use pod directives to create multiline comments, with =begin comment before and =end comment after the comment.

Truth and falsehood §

The one difference between Perl truth and Raku truth is that, unlike Perl, Raku treats the string "0" as true. Numeric 0 is still false, and you can use prefix + to coerce string "0" to numeric to get it to be false. Raku, additionally has an actual Boolean type, so, in many cases, True and False may be available to you without having to worry about what values count as true and false.

Statement modifiers §

Mostly, statement modifiers still work, with a few exceptions.

First, for loops are exclusively what were known in Perl as foreach loops and for is not used for C-style for loops in Raku. To get that behavior, you want loop. loop cannot be used as a statement modifier.

In Raku, you cannot use the form do {...} while $x. You will want to replace do in that form with repeat. Similarly for do {...} until $x.

Compound statements §

The big change from Perl is that given is not experimental or disabled by default in Raku. For the details on given see this page.

Loop control §

next, last, and redo have not changed from Perl to Raku.

continue, however, does not exist in Raku. You would use a NEXT block in the body of the loop.

# Perl
my $str = '';
for (1..5) {
    next if $_ % 2 == 1;
    $str .= $_;
}
continue {
    $str .= ':'
}

# Raku 
my $str = '';
for 1..5 {
    next if $_ % 2 == 1;
    $str ~= $_;
    NEXT {
        $str ~= ':'
    }
}

For loops §

As noted above, C-style for loops are not called for loops in Raku. They are just loop loops. To write an infinite loop, you do not need to use the C idiom of loop (;;) {...}, but may just omit the spec completely: loop {...}

Foreach loops §

In Perl, for, in addition to being used for C-style for loops, is a synonym for foreach. In Raku, for is just used for foreach style loops.

Switch statements §

Raku has actual switch statements, provided by given with the individual cases handled by when and default. The basic syntax is:

given EXPR {
    when EXPR { ... }
    when EXPR { ... }
    default { ... }
}

The full details can be found here.

Goto §

goto is currently not implemented (yet). Labels are implemented, and can be used as a target for next, last and redo:

FOO:                         # Labels end with colons, like in Perl 
for ^10 {
    say "outer for before";
    for ^10 {
        say "inner for";
        last FOO;
    }
    say "outer for after";   # Will not show because of the "last" 
}
# outer for before 
# inner for 

For what is planned for goto, see https://design.raku.org/S04.html#The_goto_statement.

Ellipsis statement §

... (along with !!! and ???) are used to create stub declarations. This is a bit more complicated than the use of ... in Perl, so you'll probably want to look at https://design.raku.org/S06.html#Stub_declarations for the gory details. That said, there doesn't seem to be an obvious reason why it shouldn't still fulfill the role it did in Perl, despite its role being expanded in Raku.

PODs: embedded documentation §

Pod has changed between Perl and Raku. Probably the biggest difference is that you need to enclose your pod between =begin pod and =end pod directives. There are a few tweaks here and there as well. For instance, as I have discovered while writing these documents, the vertical bar ("|") is significant in X<> codes, and it's not clear how to get a literal "|" into them. Your best bet may be to use the Raku interpreter to check your pod. You can do this by using the --doc switch. E. g. raku --doc Whatever.pod. This will output any problems to standard error. (Depending on how/where you've installed raku, you may need to specify the location of Pod::To::Text.) Details on Raku-style pod is at https://design.raku.org/S26.html.

Generated from