A Silly Moose Typo and Perl's Runtime Laxity

| 2 Comments

I made a really silly typo. It went a little something like this:

#!/usr/bin/env perl

use Modern::Perl;

package Oops {
    use Moose;

    has 'typo ', is => 'ro', default => 'oops';
}

Yes, there's an extra space in 'typo '. (If I were using the fat comma's autoquoting behavior, I wouldn't have made this typo, but I have my Moose attribute style for good or for ill, and this is one of the drawbacks.)

Moose doesn't care. Should it? (No.) Perl does. Should it? (As far as it affects how the parser works, yes.)

The result is plain:

my $oops = Oops->new;
my $meth = 'typo ';
say $oops->$meth;
say $oops->typo;

The rules that you've drummed into your head about what Perl allows as an identifier or this or that are mostly rules about what you can get past Perl's parser. After that, you can do almost anything you want. Because Moose attribute declarations don't have to pass the normal parser rules about identifiers (if you specify them as strings, as I did), you have a great deal of freedom, if you're willing to use that indirection throughout the rest of the code.

The moral of the story for novices is this: the computer doesn't care what you name things, if you can somehow get the name past the parts which care.

The moral of the story for experienced developers is this: abstractions leak, and sometimes that goes to your advantage. (It's not that I've defined a private attribute, but it's a naming convention a little bit stricter than that with the leading underscore.)

The moral of the story for gurus is this: choose your coding style with caution, if you want to make bugs like this impossible.

2 Comments

I'm wondering what benefit you see to:


has 'typo ', is => 'ro', default => 'oops';

over some of the alternatives, such as

has typo => ( is => 'ro', default => 'oops' );

I don't have a lot of experience with Moose (unfortunately), but to me that small alteration looks like a win in both readability and automatic error detection (due to the parser, such as you mention).

It's personal preference. To me, the parentheses in latter example look like superfluous punctuation for its own sake. I understand that other people think it groups meta-attributes visually, but I don't see that when I look at it. I just see visual noise.

Modern Perl: The Book

cover image for Modern Perl: the book

The best Perl Programmers read Modern Perl: The Book.

affiliated with ModernPerl.net

Categories

Pages

About this Entry

This page contains a single entry by chromatic published on December 1, 2012 1:00 PM.

Tests Have APIs Too was the previous entry in this blog.

Not the Final Word on Mock Objects is the next entry in this blog.

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


Sponsored by Blender Recipe Reviews and the Trendshare how to invest guide

Powered by the Perl programming language

what is programming?