<?xml version="1.0" encoding="windows-1252"?>
<node id="996924" title="How to import names from a dynamically required module" created="2012-10-02 17:39:24" updated="2012-10-02 17:39:24">
<type id="115">
perlquestion</type>
<author id="665947">
jds17</author>
<data>
<field name="doctext">
Hi,&lt;br&gt;

I have written a module where I would like to use Role::Basic. At runtime, I want to require a specific module consuming such a role and also to import some of its names.&lt;br&gt;

I could not get the import of the names right. What I tried is the following:&lt;br&gt;

&lt;code&gt;
my $package_name = get_package_name();

eval "require $package_name";
eval "$package_name-&gt;import(
        qw( method1 method2 method3 )";
&lt;/code&gt;
        
Here, $package_name is the fully qualified name of the role consuming package determined at runtime and method1,... are methods exported by this package.&lt;br&gt;

What I get as an error is:&lt;br&gt;

&lt;code&gt;
Undefined subroutine &amp;MyPackage::num_records called at ...
&lt;/code&gt;

where MyPackage is the name of the current package (i.e. the one the above code is in) and not the desired one, i.e. $package_name.</field>
</data>
</node>
