I have a library of objects in perl all having the same function_calls.
I am looking for how to create an approriate object from the library from a string.
my $object_name='myObject';#would actually be a hash lookup from user input with appropriate error checks
my $string = "return ${object_name}->new(\@params);";
my $object = eval $string;
$object->some_function();
Now I am having a problem, it works for some objects and doesn’t for others? Is there a more reliable way of doing this. I have tried printing the string out before eval and it appears to be correct with the correct class name, also every object takes the same parameter, any Ideas, thanks.
The
evalis not necessary since a string can be used as a package name, so the lines:Will do what you want. If you want to make sure that
myObjectis actually a valid package name you could do: