PrePAN

Sign in to PrePAN

MooX::NonOO Allow non-OO interface for Moo modules

Author
robrwo@github
Date
URL
https://github.com/robrwo/MooX-NonOO
Status
In Review
Good

Synopsis

package MyClass;

use Moo;
use MooX::NonOO;

...

as_function
   methods => [ ... ], # names of methods to allow as functions
   args => [ ... ];        # constructor arguments for the default instance

Description

I have some legacy modules that allow non-OO usage, e.g. you can just call methods as functions. If the first argument isn't a blessed reference to the class, then it uses a default singleton instance.

I'd like to convert the modules to use Moo, and have realized the code can be generalized to a module.

(Yes, I know a lot of people frown on this. I do now, but I'd like to maintain backward compatibility. And arguably OO, while making this module simpler to write, is overkill for some use cases.)

I have a working prototype.

My big question is: 1. Is there already something that does this for Moo (so I don't waste timing writing another module)? 2. Is there a better name for this?

Comments

I don't know if anything like this exists, I certainly haven't seen one, though I've built this pattern a couple times.

Some possible substitute names:

MooX::SingletonFunctions
MooX::SingletonDefault
MooX::DefaultSingleton
These names don't capture the purpose of the module, which is to allow non-OO use of a class.
anonymouse
Anonymous
MooX::Functional
MooX::Functional implies functional programming rather than functional calling style.

Thanks for the feedback, though.
But it's not non-OO, it's an implicit singleton. There's still an object being used somewhere. I think having Singleton in the name will make it easier to search for on CPAN, and more obvious what is happening.
But MooX::NonOO is short and catchy.
But it's not easily searchable and doesn't accurately and completely explain what it's doing. If I see "MooX::NonOO" in a module I'm trying to debug, I'm going to have to go to the docs, because that could be a couple different things. "ImplicitSingleton" says exactly what is happening.
Have you had a look at Class::Exporter? It's trying to solve the same problem, and it works with Moo.

Please sign up to post a review.