This is a not-yet-(and perhaps never)-ready-for-primetime idea after looking at
this idea for providing a way to export functions (and perhaps other symbols) to an aliased name, except that I wanted to try to create a way to do that with a plain, already existing module which exports in a "normal" way (update: but not necessarily using Exporter). This is not meant to be production code, it is just starting to play with the idea, and so far only exports functions. Toward the end of making this, I did see
Exporter::VA, which is fine, but that module requires that the exporting modules use the
Exporter module, which not all modules do (such as
TheDamian's), or what if a module exports a code reference which does not already exist as a symbol in the module, etc., so there still may be some purpose to this code :-)
Example:
#!/usr/bin/perl
use strict;
use warnings;
use Alias::Exporter (
"Test::Foo" => { bar => 'foo' },
"Test::Bar" => { bar => 'baz' },
);
foo();
baz();
# file Test/Foo.pm
package Test::Foo;
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(bar);
sub bar {
print "Test::Foo::bar!\n";
}
1;
# file Test/Bar.pm
package Test::Bar;
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(bar);
sub bar {
print "Test::Bar::bar!\n";
}
1;
# the guts: warning - ugly code ahead
package Alias::Exporter;
use strict;
use warnings;
sub import {
my $class = shift;
my %modules = @_;
my $package = caller();
for my $module ( keys %modules ) {
my %imports = %{$modules{$module}};
my @import_list = keys %imports;
eval qq|
use $module ( \@import_list );
for my \$var ( \@import_list ) {
no strict 'refs';
*{ \$package . "::\$imports{\$var}" } = \\&{\$var};
}
|;
die $@ if $@;
}
}
1;