Strangely Consistent

Theory, practice, and languages, braided together

The dot is the cutest sigil

I've recently gotten into this style of programming:

given %some-hash {
    if .<foo> {
        .<bar> = .<baz> + .<austria>;
    }
}

I keep thinking of it as a wonky kind of sigil, the .<> sigil, which has its very own variable namespace inside of the hash I've chosen as topic.

Now let's say that the above piece of code was part of a prototyped program which later evolved to use objects instead of sloppy hashes. Then the same code becomes even nicer:

given $some-object {
    if .foo {
        .bar = .baz + .austria;
    }
}

The advantages of objects to hashes are immediate (and well-known):

All this is fairly trivial; I just think it's a nice syntax. But it is with this example that the truth finally sinks in: keeping $_ and self separate from each other in Perl 6 was a really good idea.

With the syntax Perl 6 settled on, it's like there are two topics in a method: the common one ($_), and the OO one (self). And each has wonderful shortcuts: with $_ you just use prefix:<.> as above, and with self, you can use $. or @. or %. ad lib. The OO form is slightly longer than the common form, since OO is more intricate (and less ubiquitous).

Long form     Short forms
=========     ===========
$_.foo        .foo

self.foo      $.foo
              @.foo
              %.foo

And since they're disjunct from each other, you never have to context-switch or worry about what it is you're looking at when you see .foo. Unless you explicitly mix the two together with given self or invocant $_ which you're entirely free to do, but remember that it only buys you one character.

As an added bonus, we can now re-write the code example in the rejected rfc 342 so that it works in today's Perl 6, without introducing extra syntax:

my $record = loadrecord($studentID);
given $record {
    my $spam = open .minimum;
    $spam.say: qq:to'SPAM';
    Dear {.name}:
            Your tuition is now due.  Please send in a payment
    of at least {.minimum}.
    SPAM
};