in reply to RE: RE: Re: Factory Pattern for Class Heirarchy
in thread Factory Pattern for Class Heirarchy
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."; }; };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: RE: RE: RE: Re: Factory Pattern for Class Heirarchy
by merlyn (Sage) on Oct 10, 2000 at 02:27 UTC | |
by johannz (Hermit) on Oct 10, 2000 at 02:43 UTC |