* Michel Rodriguez <[EMAIL PROTECTED]> [2003-12-06 18:41]:
> I see 3 ways to do this:

You forgot one:

You say there are not typically hooks in the factory method to
override what class gets instantiated. But there is! That hook is
the constructor calls themselves. To override them, you need to
replace the constructors. You do something like this:

  my $saved_constructor = \&Foo::Bar::Node::new;
  local *Foo::Bar::Node::new = sub {
    shift;
    local *Foo::Bar::Node::new = $saved_constructor;
    return Foo::SubclassBar::Node->new(@_);
  }

If you call the factory method during the block this is in,
it will create Foo::SubclassBar::Node objects instead of
Foo::Bar::Node objects.

The inner local() is to make sure that in case the
Foo::SubclassBar::Node constructor calls its superclass
constructor, you don't go into endless recursion and it still
works right.

I don't see how you could reasonably make this snippet into an
opaque module since the local() call has to be in your own
function. At most, you might condense this to something like 

  local *Foo::Bar::Node::new = wrap(
    \&Foo::Bar::Node::new,
    qw(Foo::SubclassBar::Node new)
  );

But that doesn't really buy you anything.

-- 
Regards,
Aristotle
 
"If you can't laugh at yourself, you don't take life seriously enough."

Reply via email to