Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 04:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found