From 'perldoc -f use', I learned
use Module VERSION LIST
use Module VERSION
use Module LIST
use Module
use VERSION
Imports some semantics into the current package from the named module, generally by aliasing certain subroutine or variable names into your package. It is exactly equivalent to
BEGIN { require Module; import Module LIST; }
except that Module *must* be a bareword.
In other words, you will need to check what is passed in.
If you want them to be able to use a path, you will need to use 'require'. This however requires extra security checking to ensure that they don't spoof you by using relative path names.
The following code has only minimally been tested.
package Factory;
use strict;
use My::Baseclass;
my %includedObjects = ();
sub createNew {
my $self = shift;
my $objectName = shift;
unless (exists($includedObjects{$objectName})) {
eval "use My::Baseclass::$objectName";
# eval "require My::Baseclass::$objectName";
if ($@) {
die $@;
};
# else
$includedObjects{$objectName} = 1;
};
if ($objectName->can('new')) {
return $objectName->new(@_); # Pass in any remaining args
} else {
die "'$objectName' does not contain a constructor.";
};
};
|