Ahh, but what if the module you're importing from has a custom import, where the thing being exported does not actually exist as a symbol in the exporting module, but gets imported to a symbol in the calling module (e.g. exporting a dynamically created anonymous subroutine)? That's why I import to the Alias module first, though I may still be able to get by without the eval. I know, it's a strange arbitrary edge-case, but what the hay.
Update: Fixed some minor things in your code, and went back to importing to this module first. One thing to fix after that was the glob assignment. If you assign glob to glob, you end up with all functions initially imported with the same name being exported as the same (last exported) function (when you import to this module first). Another thing I might consider is using Tie::IxHash or something to process the lists of modules and symbols in their original order.
Another update: Another consideration...we're totally polluting the namespace of this module by doing it this way, so maybe we should initially import to Alias::Exporter::Junk or something...
sub import {
my $class = shift;
my %modules = @_;
my $package = caller();
for my $module ( keys %modules ) {
my $imports = $modules{$module};
# we need to load the module if it doesn't already exist.
(my $modname = $module . '.pm') =~ s,::,/,g;
require $modname;
my @import_list = keys(%$imports);
$module->import(@import_list);
for my $var (@import_list)
{
no strict 'refs';
my $from = join '::', __PACKAGE__, $var;
my $to = join '::', $package, $imports->{$var};
*{$to} = \&{$from};
}
}
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.