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".
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.