There just has to be a way to remove that eval. ;-)
Why do you 'use $module ( \@import_list )'? You don't need to import anything into Alias::Exporter's namespace. So you only need to require it instead. That can pull it out of the eval if you do the required modifications:
(my $modname = $module . '.pm') =~ s,::,/,g;
require $modname;
Next, we want to pull that loop out of the eval. You have the right idea - it pretty much could have been done without the eval already, except that I just removed the importing from the other module. So now we need to point at the code in the desired module explicitly.
for my $var (@import_list)
{
no strict 'refs';
*{$package . '::' . $imports{$var}} = *{$module . '::' . $var};
}
Of course, having gotten rid of the eval, the die isn't needed anymore, either.
A few more variables could make things a bit easier to read. Also, I would use references a bit more. This gives us:
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;
for my $var (@import_list)
{
no strict 'refs';
my $from = join '::', $module, $var;
my $to = join '::', $package, $imports->{var};
*{$to} = *{$from};
}
}
}
Unfortunately, that's completely untested. And still needs more commenting ;-) Good luck,
-
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.