in reply to RE: Re: Factory Pattern for Class Heirarchy
in thread Factory Pattern for Class Heirarchy

My thought was that through the use of require or eval { use ... }, the loading of the module could also be dynamic.

Replies are listed 'Best First'.
RE: RE: RE: Re: Factory Pattern for Class Heirarchy
by johannz (Hermit) on Oct 10, 2000 at 02:19 UTC

    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."; }; };
      if ($objectName->can('new')) { return $objectName->new(@_); # Pass in any remaining a +rgs } else { die "'$objectName' does not contain a constructor."; };
      This is both overkill and inaccurate. A constructor does not have to be named new, so the die message is inaccurate. And it's going to die anyway if you can't find the method, so just use:
      return $objectName->new(@_); # I hope they know how!

      -- Randal L. Schwartz, Perl hacker

        I realize that the constructor does not have to be named 'new' but I was going with the generalization (which I wasn't explicit about) that they would be using the same constructor as the base class. On further thought, since this test should always succeed if they were inheriting from the base class, your correction is better. My code really really doesn't test what I thought it was going to. He might consider checking $objectName->isa('My::Baseclass') to at least verify that at least the new class is inherited from the base class that he hopes it is. This is just to make debugging easier by catching errors sooner rather than later.

        Remember: This was a quick and dirty hack to try and get an basic answer for the question.