Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Module Naming Dilemma

by xdg (Monsignor)
on Jan 04, 2005 at 23:41 UTC ( [id://419457]=note: print w/replies, xml ) Need Help??


in reply to Module Naming Dilemma

I like the concept. I took a similar, though slightly different, approach in Math::Random::OO, where I have several subclasses and didn't want to type out the full constructor each time. In short, I have my main module export a "factory function" (better names welcomed) that encapsulates a call to the constructor. So you can do this:

use Math::Random::OO qw( Uniform UniformInt Normal Bootstrap ); $uniform = Uniform(-1,1); $uni_int = UniformInt(1,6); $normal = Normal(1,1); $boot = Bootstrap( 2, 3, 3, 4, 4, 4, 5, 5, 5 );

I did it by writing a quick custom import function like so (though as I post it, I see that I'm not checking for certain errors -- grrr -- bad style, slap on the wrist for me.):

sub import { my ($class, @symbols) = @_; my $caller = caller; for (@symbols) { no strict 'refs'; my $subclass = "Math::Random::OO::$_"; eval "require $subclass"; *{"${caller}::$_"} = eval "sub { return ${subclass}->new(\@_) }"; } }

Your module sounds like a generalization of this approach, albeit still requiring the call to new. As for names, I'd prefer if your module were named "Alias" or something similar, as what you're doing is aliasing, not defining or altering a class. How about some syntax like this:

use Alias 'My::Long::Class::Name' => 'ShortForm'; use Alias [ 'My::Long::Class::Name', @options ] => 'ShortForm';

(update:Alias is already used, so I guess it has to be something else.)

The first example is simpler than than using "as" as a keyword. The second keeps the real name and options together and still is suggestive that, in the end, you're aliasing it to ShortForm.

update:I wasn't familiar with Package::Alias and namespace, both look like they do pretty much the same as what I've got above.

update 2:I guess the advantage of what Ovid's trying to do compared to, say, Package::Alias is that he gets it done in a single function call, rather than a "use" and an "alias" afterwards. May I humbly suggest "Nickname" as the module name, with a syntax like I suggest above for brevity in the standard case with no extra import options?

use Nickname 'My::Long::Class::Name' => 'ShortForm'; use Nickname [ 'My::Long::Class::Name', @options ] => 'ShortForm';

-xdg

Code posted by xdg on PerlMonks is public domain. It has no warranties, express or implied. Posted code may not have been tested. Use at your own risk.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://419457]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 03:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found