Presumably, you have a lot of code that is common between the two cases (else it likely wouldn't make much sense to combine these case into a single module).

So, best would be to factor out the code that isn't the same (normally you 'factor out' what is the same). In the places where these chunks were factored out, you put a $obj->Method(...). Now write Method() for the two cases but both wrapper classes have the same interface.

After you've defined the two versions of each method, you'll have two classes. Each is a wrapper for one of the external classes.

A common next step would be to make @Blarg::Wibble::Foo_Bar::ISA= 'Blarg::Wibble' and then have Blarg::Wibble::new() decide which subclass to bless into based on the type of argument passed in.

But OO old-timers learn to be suspicious of inheritance. And this is a perfect example of a trap where inheritance makes your design a bit more fragile and harder to extend.

Better is just to have Blarg::Wibble::new() create and hold a Blarg::Wibble::Foo_Bar-wrapper that it uses.

For testing, you want to 'skip' tests in the case you described. How you skip tests depends on the testing module you used. For example, Test::More offers the following somewhat dubious construct:

SKIP: { skip $why, $how_many if ! eval { require Foo::Bar; 1 }; # tests go here };

For your case, I'd probably have a FooBar.t and BaxQuux.t that each use a common test.pm whose import() runs the tests and does skip_all if the passed-in module name can't be required.

- tye        


In reply to Re: Question about creating intelligent behaviors in modules WRT external objects/modules (factor) by tye
in thread Question about creating intelligent behaviors in modules WRT external objects/modules by atcroft

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.