Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: require & import $module_name

by temporal (Pilgrim)
on Jul 19, 2012 at 20:13 UTC ( #982708=note: print w/ replies, xml ) Need Help??


in reply to require & import $module_name

You are correct chromatic, I needed to use a named lexical there. Not really sure why that is. This works:

use Symbol 'delete_package'; foreach my $module (@modules) { require "sub/$module.pm"; import $module qw(foo bar); foo(); bar(); #cleanup delete_package($module); }

As well as the function call format that you wrote it in.

strict; system('rm -fr /home'), lol! tobyink, the modules are all created by myself so I'm (reasonably) sure that nothing like that will every happen. Thanks for looking out for me, though =D

use Symbol 'delete_package'; foreach my $module (@modules) { eval "use sub::$module"; import $module qw(foo bar); foo(); bar(); #cleanup delete_package($module); }

Works fine as well.

CountZero, cool little module. That does the trick too:

use Symbol 'delete_package'; require UNIVERSAL::require; foreach my $module (@modules) { "sub::$module"->require or die $@; import $module qw(foo bar); foo(); bar(); #cleanup delete_package($module); }

Thanks guys!

Strange things are afoot at the Circle-K.


Comment on Re: require & import $module_name
Select or Download Code
Re^2: require & import $module_name
by chromatic (Archbishop) on Jul 19, 2012 at 20:53 UTC
    I needed to use a named lexical there. Not really sure why that is.

    Without seeing the other code, it's hard to say for sure, but it's possible that something else modifies $_ without localizing it appropriately.

    As well as the function call format that you wrote it in.

    Like I said, be very careful with your approach to calling import. I know what the documentation says about use, but I patched it before 5.16 so it was closer to reality. You're asking for trouble if you do it that way.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://982708]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2014-04-18 06:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (462 votes), past polls