Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
One method I've used for handling plugins (thanks to the guidance of various monks in the CB) is a delegation class - something like this:

package Toolkit::DB; ############################################# # new - constructor # # # returns blessed db object ############################################# sub new { my $self = shift; my %params = @_; if (!$params{dbtype}) { $params{dbtype} = "Toolkit::FileDB"; } $params{delegate} = "$params{dbtype}"->new(%params); return bless \%params,$self; } ############################################# # insert - add a record to the db # # takes hash of fieldname/fieldcontent # # returns success/fail ############################################# sub insert { my $self = shift; my %data = @_; my $result = $self->{delegate}->insert(%data); return $result; }

(With similar methods for all the other function calls needed). Then at run time you can choose which implementation class to use, or take the default ('Toolkit::FileDB' in this instance).

Update: It's just occurred to me - that way you could also create stubs for the functionality that return 'this function is not available without X::Y::Z installed' instead of a fatal error.

Update: There's some possibly related discussion about using use and require in Use and Require - making an informed choice.

--------------------------------------------------------------

"If there is such a phenomenon as absolute evil, it consists in treating another human being as a thing."

John Brunner, "The Shockwave Rider".


In reply to Re: Creating a "Plug-In" Framework for a Module by g0n
in thread Creating a "Plug-In" Framework for a Module by ajt

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 avoiding work at the Monastery: (4)
As of 2024-03-29 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found