Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Scope of a Module?

by frag (Hermit)
on Jul 07, 2001 at 05:54 UTC ( [id://94667]=note: print w/replies, xml ) Need Help??


in reply to Scope of a Module?

It seems like what you're trying to do violates modularity in a way that'll cause problems. That is, somebody who wants to extend this to a new format should ideally be able to just whip up a DBI::Pretty::Foo and start using it in scripts. But with this design, any such developer also has to muck with the DBI::Pretty.pm as well, adding an elsif to that bit of (pseudo-)code. This is really undesirable, potentially making it difficult to maintain and use your module.

As an alternative, how about this:

  • Instead of 'my $hash = shift || {}' (inside your constructor), create a parse_input() method. (Or grab_input(), or something similar.) In the case of your existing module, it would just consist of that one statement, returning a hashref.
  • Subclasses handling different forms of input could now be created by simply replacing the new() and parse_input() methods with ones of their own construction, returning a hashref that conforms to your requirements. (Actually, you could make it so that people extending your module would only have to rewrite parse_input(), if you get creative with the way you write your new() method -- blessing an object immediately, and making all additional calls in new() through the -> operator, i.e. "$self->parse_input".)
  • Anyone using the module must now decide in advance what form the input will take, and must explicitly choose the corresponding module ('use DBI::Pretty::XML').

If you do want the module to be flexible enough to handle a few alternative situations by default without requiring subclasses, just place that if/elsif you've got inside the default parse_input(), but rather than calling alternative use statements, each condition should just invoke different parse_foo_input() methods. Doing it like this -- in parse_input(), not in new() -- shouldn't interfere with the ability for other people to roll their own subclasses.

But the simplest/best thing is probably to just require that anyone using the module must themselves pick the subclass(es) that they want. You can then provide a few basic alternative subclasses along with the main module.

-- Frag.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2024-03-28 10:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found