Minor Annoyances to the CPAN

Ovid on 2008-07-22T12:42:58

Oops That namespace is taken. I'll think again. Suggestions welcome.

Quite often I find myself needing a simple check to see if a sub has already been called. This happens all the time and I hate coding this.

use Sub::Called 'already_called', 'not_called';

sub user {
    unless (already_called) {   # only gets called once
        My::Fixtures::Users->load;
    }
    ...
}

sub schema {
    if ( not_called ) {
        # setup schema
    }
    else {
        return $schema;
    }
}

Yeah, it was only five minutes to write and it's a minor, minor nit, but I hopefully won't have to worry about it again. It should be on the CPAN soon.

It checks the package and subroutine to determine if it's been called yet.

It's possibly my most trivial module. Flame away!


Memoize?

djberg96 on 2008-07-22T14:00:02

Couldn't you add callback support to MJD's Memoize module? Add two options, CALLBACK_ON_LOAD and CALLBACK_IF_CACHED, or something:

memoize('function', CALLBACK_ON_LOAD => sub{ My::Fixtures::Users->load });

This gives me an idea for the Ruby memoize library, actually...

Re:Memoize?

Ovid on 2008-07-22T15:29:32

I don't want to necessarily memoize these functions, so hooking into that doesn't seem right (though to be fair, it didn't occur to me, either).

I can add this functionality to my module

reneeb on 2008-07-22T20:31:08

Hi Ovid,
I could add the functionality you mentioned to my module...

Cheers, Renee

Re:I can add this functionality to my module

Ovid on 2008-07-23T08:09:52

If you want to do that, it would be great and I can just delete mine. Thanks :)