in reply to Seeking good ways to structure cross-platform code
package XPlat; use strict; my $OS_TYPE = $^O eq 'MSWin32' ? 'win' : ($^O eq 'nonstop_kernel' ? 'nsk' : 'unix'); push @ISA, "XPlat::$OS_TYPE"; sub XPlat::win::fn { print "windows version of function\n" } sub XPlat::nsk::fn { print "nsk version of function\n" } sub XPlat::unix::fn { print "unix version of function\n" } package main; XPlat->fn(); # calls function in whichever package is appropriate
As a bonus, you can use inheritance to share common methods by moving them up to a superclass, or produce other effects by creating additional subclasses.
|
|---|