From 'perldoc -f use', I learned
In other words, you will need to check what is passed in.use Module VERSION LISTImports 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
use Module VERSION
use Module LIST
use Module
use VERSIONBEGIN { require Module; import Module LIST; }except that Module *must* be a bareword.
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."; }; };
In reply to RE: RE: RE: Re: Factory Pattern for Class Heirarchy
by johannz
in thread Factory Pattern for Class Heirarchy
by dcorbin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |