http://www.perlmonks.org?node_id=339201


in reply to Re: •Re: It's a dog, but what kind? (polymorphism , in Perl OO)
in thread It's a dog, but what kind? (polymorphism , in Perl OO)

Polymorphism is a much more general concept, and we skip over that generality by starting with the factory piece first.

See my reply below for why this isn't a good example of polymorphisim anyway. Update: blue_cowdawg's update fix the OP's polymorphisim issue.

I would be interested to see if the factory could be made without using eval, as well...I think it can, especially if the dogs were loaded previously in a more-safe matter.

It can using require $path_to_breed; instead of eval "use $breed;";. But it ammounts to almost the same thing, except that you have to specifiy the path to the breed file instead of the traditional 'module::name' format. As you say, it's also possible to load all the subclasses before anything is called, but that could get very inefficient fast.

My favored solution would be to map a breed via a hash:

my %breeds = ( cocker => 'dog::cocker', setter => 'dog::setter', ); sub new { my $class = shift; my $breed = shift; my $self = { }; if($breed) { my $breed_class = $breeds{$breed}; eval " use $breed_class "; die $@ if $@; $self = $breed_class->new(); } else { bless $self, $class; } return $self; }

----
: () { :|:& };:

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re: Re: Re: •Re: It's a dog, but what kind? (polymorphism , in Perl OO)
by flyingmoose (Priest) on Mar 24, 2004 at 00:43 UTC
    As you say, it's also possible to load all the subclasses before anything is called, but that could get very inefficient fast.
    Understood. It could be a killer. As a non-perl example, Sun did something like this with java import in 1.4.x, and we saw numerous performance issues -- and we ended up reimplementing lazy loading of some plugins with reflection (i.e. equivalent of symbol tables) -- ugly!
    My favored solution would be to map a breed via a hash:
    Great idea, chief! I'd probably use both the hash and require for purity's sake -- and call the import explicitly. You might (I'm theorizing) save a little performance from not using eval at all in that case. But yeah, then you would lose the exception catching (eval block)???

      The only problem with require in this case is that if you use a variable for the module name, you have to specify the path to the module, not the bare name. Using eval STRING is the accepted idiom around this problem. Yes, it is slow, but it's probably minimal unless you're doing it in a tight loop. In this example, the error handling aspects of eval are a non-factor, since we're just dieing with the error message anyway. Of course, your own code may or may not do that.

      ----
      : () { :|:& };:

      Note: All code is untested, unless otherwise stated