in reply to Using a 'package' as a 'module', and choosing at runtime?

You could try using inheritance and polymorphism

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 ;

Nuts and Bolts

We start with a 'base' package. This will contain all of the methods that are common to all the platforms. Then for each supported platform we create a new package, and declare within it the @ISA variable(this package 'is a' ...) so that when perl wants to find a method when doing $obj->method_name, it will first check the objects package, if it doesn't find it there, it will go through each package in the @ISA list, and execute the first 'method_name' it finds. This is how perl does OOP polymorphism, the facility for different classes to have different implementations for the same 'method_name'.

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.