Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Bundling commonly-used modules into a toolset

by creamygoodness (Curate)
on Oct 25, 2005 at 19:04 UTC ( #502842=note: print w/replies, xml ) Need Help??


in reply to Bundling commonly-used modules into a toolset

Delicious! This is what I had hoped Toolkit would be!

I'd like to use this on a distro destined for CPAN which numbers 47 modules and counting. One of those modules is a base class for almost all the others, supplying new(), etc. A typical module begins:

package Restaurant::People; use strict; use warnings; use base qw( Restaurant::Util::Class ); use Carp; use Scalar::Util qw( blessed dualvar );

Now, say I have other classes which inherit from Restaurant::People: Customer, Chef, Server, Busser, Manager, etc. The ultimate in laziness would be this, if I turned Restaurant::Util::Class into a ToolSet-enabled superclass:

package Restaurant::People::Cook; use base qw( Restaurant::People );

However, it doesn't look like I can get away with that -- the code needs to look like this instead, correct?

package Restaurant::People::Cook; use base qw( Restaurant::People ); use Restaurant::Util::ToolSet;

That's still going to save a bunch of space. I always need strict and warnings, nearly always need Carp, and often need Scalar::Util, so I'll just throw 'em all in the ToolSet. Sweet!

Regarding the Alpha status: I didn't see a warning anywhere in the docs. Is the interface stable? Without any explicit notice, I infer that distributions are in Beta if the version is less than 1.0.

--
Marvin Humphrey
Rectangular Research ― http://www.rectangular.com

Replies are listed 'Best First'.
Re^2: Bundling commonly-used modules into a toolset
by xdg (Monsignor) on Oct 26, 2005 at 00:52 UTC

    Cool. That's one of the applications for which I thought it might be useful. I don't think that use base will work because it doesn't call import. You could do it in two steps, though:

    package Restaurant::People::Cook; use Restaurant::People; # A ToolSet module our @ISA = qw( Restaurant::People );

    I suppose I could add a method to ToolSet that adds the ToolSet module to the caller's @ISA. Then you'd get the imports and set up a subclass.

    ToolSet->set_base(1);

    The other approach would be to explicitly set base modules, so it doesn't necessarily have to be the ToolSet module:

    ToolSet->set_base('Restaurant::People');

    Any preference?

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      After some experimentation, I've concluded that it's a good thing that use base and use My::ToolSet require separate lines -- because of what other people will expect when they subclass.

      If someone writes this:

      package BunnyBurgers::PRFlack; use base qw( Restaurant::People );

      ... they won't be expecting to have have a whole bunch of extra functions pulled into their namespace, or changes in pragma behavior. Now it's true that all of a sudden all of the imports I brought in via Restaurant::Util::ToolSet to Restaurant::People are available as methods, so $pr_flack->can('dualvar'). But that happens anyway -- $nearly_any_object->can('carp') -- and at least subclassers don't have to worry about surprise namespace polution in their actual module code.

      For the time being, I've settled on starting all the modules like this:

      package Restaurant::People::Cook; use Restaurant::Util::Toolset; use base qw( Restaurant::People );

      There will only be one ToolSet in the entire distro, and it will bring in strict, warnings, Carp, Scalar::Util, and a global RESTAURANT_DEBUG constant. Restaurant::Util::Toolset class won't be public, so it will be possible to modify it, but I'm expecting to keep it stable. That should cause minimum confusion. The only people who need to know what's in Restaurant::Util::Toolset are people who are hacking/debugging modules which use it.

      So, to answer your question, I wouldn't really use either one of those. Multiple inheritance of ToolSet-enabled modules would just get too messy.

      I'm quite happy with what we've got now. By compressing several lines at the top of the file into one, a bunch of meaningful, frequently edited code now appears in the the first screenful, so when I type "gg" in vim, I'm right in the game.

      --
      Marvin Humphrey
      Rectangular Research ― http://www.rectangular.com

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2020-06-06 09:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (41 votes). Check out past polls.

    Notices?