Is this code too strange?

Ovid on 2004-08-05T19:13:04

I thought about writing up something for Perlmonks demonstrating how test driven development (TDD) works with Test::More. The intent would be to write a short, but useful OO module that gives them a good sense of TDD. Then I realized I have an unusual enough coding style that I could possibly confuse more of the monks than help them. In glancing at this snippet, does it make sense (using private methods and named anonymous subs) or would it be too distracting from what I was trying to do?

my $_croak = sub {
    local *__ANON__ = '__ANON__croak';
    my ($proto, $message) = @_;
    require Carp;
    Carp::croak($message);
};
                                                                                                                                                            
my $_validate_code_slot = sub {
    local *__ANON__ = '__ANON__validate_code_slot';
    my ($self, $code_slot) = @_;
    no strict 'refs';
    unless (defined *{$code_slot}{CODE}) {
        $self->$_croak("Cannot replace non-existent sub ($code_slot)");
    }
    return $self;
};


Too distracting

dws on 2004-08-05T19:37:32

If you lead off with those fragements, I suspect you'd lose a lot of folks who you could otherwise help. I had a hard time answering the "how, exactlyl, does this help me?" question.

Too strange!

mir on 2004-08-05T20:11:17

The code is definitely too strange.

Once you've read the perlmonks post it makes sense, but overall I think that the __ANON__ trick will obscure your main point. It might be better to keep the tutorial focused on showing how you write just enough code to pass the tests (which you are not quite doing when you use tricks like this BTW).

Another vote for too distracting

Adrian on 2004-08-05T21:16:21

I'd just use the normal _leading_underscore convention that more people will be familiar with for a tutorial on TDD. You'll just have to spend time explaining the method, which will distract from your main point.

(we must be on the same ley-line since I'm writing a little tutorial myself on TDD - not aimed at perlmonks tho :-)