DEV Community

Cover image for Not So Obvious Semantic Changes (Part 2 of 3)
Elizabeth Mattijsen
Elizabeth Mattijsen

Posted on

Not So Obvious Semantic Changes (Part 2 of 3)

About $_

In Perl, $_ is a global variable which may be localised for certain scopes, like in a for loop. This means that it is trivial in Perl to make a subroutine that accesses $_ of the caller's context:

# Perl
sub topic_squared {
    say $_ * $_;
}
$_ = 42;
topic_squared;  # 1764
Enter fullscreen mode Exit fullscreen mode

However, in Raku, $_ (aka “ the topic variable “) is always lexical. This means that it is (almost) impossible to write a subroutine that uses the value of $_ from the scope of the caller.

This also means that idiom such as a bare say cannot work in Raku, as basically almost all commands in Raku are just subroutines. So it was decided to not support that idiom in Raku:

# Raku
for 1..10 {
    say;  # Unsupported use of bare “say"
}
Enter fullscreen mode Exit fullscreen mode

Fortunately, there is a syntax that is almost as small in Raku:

# Raku
for 1..10 {
    .say;  # calls method "say" on $_
}
Enter fullscreen mode Exit fullscreen mode

This calls the say method on $_. As we’ve seen, the period is used in Raku to indicate method calling. If you call a method on "nothing", then it will assume the current topic, so will use $_ for that.

$a and $b are not special

In Perl, the variables $a and $b are special in that you do not have to define them before you can use them:

# Perl
use strict;  # MUST define variables before using them
$a = 42;     # except $a
$b = 666;    # and $b

say $a;  # 42
say $b;  # 666
Enter fullscreen mode Exit fullscreen mode

So why does Perl have that? Well, Perl will fill those variables in certain contexts for you, specifically when doing a sort:

# Perl
my @sorted = sort {
    $a cmp $b    # $a and $b set to each value in turn to compare
} @files;
Enter fullscreen mode Exit fullscreen mode

In Raku there are several ways to do the same, but the easiest to remember is the auto-signature syntax:

# Raku
my @sorted = sort {
    $^a cmp $^b    # generates signature -> $a, $b
}, @files;
#↑ note: comma is needed
Enter fullscreen mode Exit fullscreen mode

Note that the ^ in this example is called a "twigil" (as in "secondary sigil").

More on signatures in future blog posts.

Summary

In this blog post the subtle different semantics of $_ between Perl and Raku are handled, and it is shown that Raku doesn't have the direct equivalent of Perl's $a and $b, but that there is a more flexible alternative using secondary sigils.

Top comments (0)