<?xml version="1.0" encoding="windows-1252"?>
<node id="1016618" title="Variable as module name?" created="2013-02-01 17:25:09" updated="2013-02-01 17:25:09">
<type id="115">
perlquestion</type>
<author id="785151">
Cody Fendant</author>
<data>
<field name="doctext">
&lt;p&gt;I want to do this:&lt;/p&gt;

&lt;code&gt;
use Foo;
my $object = Foo-&gt;new();
&lt;/code&gt;

&lt;p&gt;Only I want to replace 'Foo' with a variable determined by some other factor.&lt;/p&gt;

&lt;code&gt;
my $module_name = 'Foo';
use $module_name;
my $object = $module_name-&gt;new();
&lt;/code&gt;

&lt;p&gt;I got as far as understanding that &lt;code&gt;use&lt;/code&gt; will never be happy with this, is that right? And I ought to use &lt;code&gt;require&lt;/code&gt; instead?&lt;/p&gt;

&lt;p&gt;My code doesn't give an error if I do this:&lt;/p&gt;

&lt;code&gt;
$module = 'Foo';
my $module_path = $module . '.pm';
require $module_path;
&lt;/code&gt;
&lt;p&gt;But it complains that I'm calling "new" on an unblessed reference when I do &lt;code&gt;my $object = $module-&gt;new();&lt;/code&gt; on the next line.&lt;/p&gt;


</field>
</data>
</node>
