in reply to Question about creating intelligent behaviors in modules WRT external objects/modules
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
|
|---|