mrguy123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,
I have a program in which I want to create dynamic objects according to input from another program. This is easy enough, except for the "use" feature. I have added an example below to explain my case clearly:
Program 1: demo
#!/usr/bin/perl use strict; use demo2; use demo1; { &poly("demo1"); } sub poly { my ($temp) = @_; my $object = new $temp; ##A function that is used in both objects $object->test; }
Program 2 (demo1.pm)
package demo1; sub new { my $class = shift; my $self = {class => $class}; bless ($self,$class); return $self; } sub test { print "demo1\n"; } 1;
Program 3 (demo2.pm)
package demo2; sub new { my $class = shift; my $self = {class => $class}; bless ($self,$class); return $self; } sub test { print "demo2\n"; } 1;
In this example, I can create a dynamic object according to the input I get for sub poly, sent from the main function (or another program in real life). If I send demo1, the program prints demo1, and vice versa for demo2.
However, if I want to dynamically create one of the objects, I need to declare it as "use demo1" or "use demo2", which is hardcoded. This I don't want.
Does anybody have any ideas how I can work around this?(in my development I need to declare many objects and I don't want to hard code anything).
Update1: The problem is basically in compiling a program that dynamically declares use (e.g. use $temp if it was possible)
Update2: It seem this problem can be fixed by using "require "instead of "use". For example
my $temp = "demo1.pm"; require $templ
Thanks for all you help
Guy

---Oh what do you do poor, poor Angus, when hunger makes you cry?
I fix myself an omelet, sir, of fluffy clouds and sky".

Replies are listed 'Best First'.
Re: Creating a dynamic object in Perl
by Corion (Patriarch) on Jan 07, 2008 at 15:29 UTC

    Instead of use, take one of its building blocks, require, which allows you loading of modules at runtime.

      Worked like a charm!!
      Thanks for your help
Re: Creating a dynamic object in Perl
by spx2 (Deacon) on Jan 07, 2008 at 16:39 UTC
    if you want to load a module whos name is calculated by you
    and load it in a particular place in the program at the particular
    time that code is executed yes you can use require
    having in mind that the difference between require and use is that
    use loads modules at compile-time and
    require at run-time.



    altough not very ethic,your code could have been written without either "use" or "require" of them
    like this :
    #!/usr/bin/perl sub new_pkg { our $i; $i++; eval <<__SOMECODE__ package demo$i; sub new { my \$class = shift; my \$self = {class => \$class}; bless (\$self,\$class); return \$self; } sub test { print "demo$i\\n"; } __SOMECODE__ ; }; new_pkg for 1..10; for (1..10) { my $type_of_obj = "demo$_"; my $new_obj=$type_of_obj->new; $new_obj->test; };
    P.S. : beware,as this is not a best practice(just showing that you can do things this way too)