The dot is the cutest sigil

masak on 2009-10-04T10:47:21

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):

  • Less to write.
  • The translation between the old style and the new is automatic. Just remove the pointy elbows.
  • Spelling errors are now caught at runtime, as opposed to never.
  • The reason assignments (like the assignment to .bar above) work is that attribute accessors can be made rw. The flip side of that is that one can omit the rw and avoid assignment accidents. That's also an improvement over the hash.
  • Now the dot really looks like a cute little sigil. It isn't, of course, it's still the call-public-method twigil we know and love. But it's even easier to pretend that the attributes are special variables namespaced under the chosen object.

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 $_ &8212; 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
};


Very inneresting ...

thickas on 2009-10-05T09:27:40

masak++

I have been enjoying your P6 articles immensely.

Probably you, Jonatahan, & Nicholas are the authors I most eagerly look forward to reading.

Thank you very much.

Re:Very inneresting ...

masak on 2009-10-05T09:51:59

Thanks for the kind words. Writing blog posts is satisfying in itself, but hearing that people enjoy (let alone read) them makes it extra worthwhile.