Wednesday, November 4, 2009

Unsolicited Code Review

I am awed by Carlin's IRC bot in Perl 6 code. It is cool and practical and working, and makes me want to steal code from it and write bots of my own.

At the same time, every time I look at the source, my eyes land on a series of elsif's, and I think "That's so Perl 5!" Here it is:

Starting at the beginning, that first line is a wonderful bit of Perl 6 craziness. $message .= split(' '); is equivalent to $message = $message.split(' ');. What's wacky about that is $message starts off as a string, and ends up an array! The next two lines make the first element of the array $command and the rest of them $params. (If you're wondering about how $message and $params can be arrays, they are array objects in scalar variables. Or something like that, I'm fuzzy on the details, but it clearly works.)

I'd recast all these lines as one simple line harking back to Perl 5, but in a nice Perl 6 way: my ($command, @params) = $message.split(' '); It would perhaps be better to split on \s+, too. And now we can switch $message away from being a rw parameter. (I believe doing that will not mess up the rest of the bot code, but I admit I haven't actually tested it.)

Then the rest of the function is probably better expressed as given / when block. The great thing is all those $command ~~ 'karma' can become just 'karma' if we do given $command.


(Hmmm... actually now that I look at it, how the heck does the Str $message is rw handle being converted to an array of strings in the old code?)

No comments:

Post a Comment