You could load the modules the same way that File::Spec does.
( Assume that use warnings;
, use strict;
, and a version declaration immediately follow all of the package
declarations throughout this post )
package Common::Copy::Win32;
sub do_this {}
package Common::Copy::Linux;
sub do_this {}
package Common::Copy;
my %module = (
MSWin32 => 'Win32',
linux => 'Linux',
);
my $module = $module{$^O} || 'Linux';
require "Common/Copy/$module.pm";
our @ISA = ("Common::Copy::$module");
#!/bin/perl
use Common::Copy;
Common::Copy->do_this();
This would require that the subroutines are written as methods which is probably not what you want.
It looks like you actually want it to export in a similar way to how File::Spec::Functions does.
This implementation assumes that the subroutines aren't written as methods.
package Common::Copy;
require Exporter;
our @ISA = qw'Exporter'; # use Exporters `import` method
our @EXPORT = qw'do_this';
our @EXPORT_OK = qw'';
our %EXPORT_TAGS = ( ALL => [ @EXPORT_OK, @EXPORT ] );
my %module = (
MSWin32 => 'Win32',
linux => 'Linux',
);
our $module = $module{$^O} || 'Linux';
require "Common/Copy/$module.pm";
$module = "Common::Copy::$module"; # full name of actual module
foreach my $subname (@EXPORT, @EXPORT_OK) {
my $subref = $module->can($meth); # misuses method lookup
no strict 'refs';
# import the subroutines into this namespace
# assumes they aren't written as methods
*{$subname} = \&$subref;
}
#!/bin/perl
use Common::Copy;
# use Common::Copy qw':all';
# use Common::Copy qw'do_this';
do_this();