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,


In reply to Re: RFC: Alias::Exporter by Tanktalus
in thread RFC: Alias::Exporter by runrig

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.