Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

masak (6289)

  (email not shown publicly)

Been programming Perl since 2001. Found Perl 6 somewhere around 2004, and fell in love. Now developing November (a Perl 6 wiki), Druid (a Perl 6 board game), pls (a Perl 6 project installer), GGE (a regex engine), and Yapsi (a Perl 6 implementation). Heavy user of and irregular committer to Rakudo.

Journal of masak (6289)

Sunday October 04, 2009
05:47 AM

The dot is the cutest sigil

[ #39709 ]

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

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}.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • 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.

    • 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.