in reply to Using a 'package' as a 'module', and choosing at runtime?
our $Type ; BEGIN { if ( $^O =~ /dar/ ) { $Type = 'Mac' ; } elsif ( $^O =~ /sol/ ) { $Type = 'Sun' } elsif ( $^O =~ /aix/ ) { $Type = 'Aix' } else { die "unknown os." } }; package base ; sub common_routine {} sub specific_routine { die "must be one of mac, aix, sun" ; } sub package Mac ; our(@ISA) = qw/base/ ; sub specific_routine { DoMacStuff()} package Aix; our(@ISA) = qw/base/ ; sub specific_routine { DoAIXStuff()} package Sun ; our(@ISA) = qw/base/ ; sub specific_routine { DoSunStuff()} package main ; my($obj) = new $Type ; $obj->specific_routine ;
For each method, that will need a different implementation for a given platform, we'll first add that method to the base package, which may just include a 'die' to remind us to implement this method for this platform we're testing on, and then add the method in each specific platform.
It possible that some methods may service several platforms, and one just needs it a little differently, in which case we can put the common method in the 'base' package, and the one that needs to be different in its own pacakge, thus overriding it.
|
|---|