Recently in Perl 6 Category

These links are collected from the Perlbuzz Twitter feed. If you have suggestions for news bits, please mail me at andy@perlbuzz.com.

Perl lets you call object methods in two different ways:

  • $obj->method()
  • method $obj
The latter form is usually called only for things like clone $obj, which is pretty ugly. For more about indirect object syntax and some of the pain it causes, see When Is Backwards-Compatibility Not Worth It?.

It's funny that Max posted that blog entry the other day, because I just was shown a problem that was caused indirectly by it. Mike O'Regan showed me some code that he was surprised even compiled, because it certainly wasn't working.

sub custom_sort {
    return
        $a->{foo} cmp $b->{foo}
        ||
        a$->{bar} cmp b$->{bar}
}

See the a$ instead of $a? Yuck. But it compiles just fine. Why? Well, let's see what B::Deparse decompiles it out as:

$ perl -MO=Deparse foo.pl
sub custom_sort {
    return $$a{'foo'} cmp $$b{'foo'} || $-->a > {'bar'} cmp $-->b > {'bar'};
}
foo.pl syntax OK

Turns out that it's calling method a on the object $- and then seeing if that is greater than {'bar'}. Double-ugh.

Perl 6 still has indirect object syntax, but you must follow it with a colon, as in method $obj: @args. Larry says in Perl 6 it's completely unambiguous.

Why I love say

| | Comments (2)

Dean Wilson reveals My Favourite Three Features in a recent blog post, but doesn't discuss my #1 feature of Perl 5.10: The say command.

Why love say so much? It's just the same as print with a "\n" at the end, right? Yup, but that "\n" causes heartache for me in life. See, I've been working on removing interpolation from my life wherever possible. For instance, we've probably all seen beginners do something like:

some_func( "$str" );

where the quotes around $str are unnecessary. (Yes, I know there could be overloaded stringification, but I'm ignoring that possibility here.) That function call should be done as:

some_func( $str );

By the same token, I don't use double quotes any more than necessary. Rather than creating a string as:

my $x = "Bloofga";

do it as

my $x = 'Bloofga';

It's not about speedups. It's about not making the code do anything more than it has to, so that the next programmer does not have to ask "why is this work getting done?" If the code doesn't need the double-quoting, then don't use the double-quoting.

I started down this road when I read this rule in Perl Best Practices, but ignored it. "Eh, no biggie," I thought. Then I started using Perl::Critic, and it complained about everywhere I was using double quotes. As I examined those complaints, I came around to realize that if you're having the computer do work, the next programmer has to wonder why.

So now we get the say command, and I get to eliminate at least 50% of my necessary string interpolation. Instead of:

print "Your results are:\n";

I can now use:

say 'Your results are:';

So much cleaner. In a color-coding editor like vim, the distinction is even clearer, and as MJD likes to point out, "It's easier to see than to think."

Start using say. Even if you're not on 5.10 yet, you can use Perl6::Say for most of the places that say works in 5.10. Even better, stop using unnecessary interpolation altogether.

About this Archive

This page is a archive of recent entries in the Perl 6 category.

Perl 5.10 is the previous category.

Programmers is the next category.

Find recent content on the main index or look in the archives to find all content.

Other Perl Sites

Other Swell Blogs

  • geek2geek: An ongoing analysis of how geeks communicate, how we fail and how to fix it.