Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: Module Naming Dilemma by xdg
in thread Module Naming Dilemma by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 17:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found