in reply to Re: Modification of @ISA at run time
in thread Modification of @ISA at run time

Apropos of the recent discussion "How to differentiate hash reference and object reference?", how do you recommend handling isa when using delegation like this? Do you also define an isa that passes the isa call through to the delegate?

sub isa { my ($self, $class) = @_; return $self->{delegate}->isa($class); }

If so, it's another reason why people should avoid this:

if ( UNIVERSAL::isa( $obj, 'Astro::Data::Mythological' ) ) { # whatever }

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Replies are listed 'Best First'.
Re^3: Modification of @ISA at run time
by nothingmuch (Priest) on Sep 01, 2005 at 14:20 UTC
    Well, the reimplementation of isa is probably the way to go, but normally I've found that the greater the all-knowingness of an object, the less I need to introspect it.

    In a sense the behavior is transparent, instead of using a delegate there could have been an if/else on the member data in the single classes get_sunrise_time

    My point is that when designing in this direction, usually you want to pretend that the object is the same, and the delegate is just an implementation detail. In situations when the role is truely different you don't want to instantiate from the same class anyway, you have have two top level classes, one for west-rising suns, and the other for east rising ones.

    In fact, that is exactly what the delegation model is - it's two independant classes, completely unbound to each other, but interchangable, and an object which can reuse them to give you an alternative with a single point of entry.

    Good examples of this kind of reuse can be dug up by querying Any... These are modules which usually wrap around several modules which do the same role, but have different features. The wrapper modules hide the details of the difference from the user, letting the user give the generic module more kinds of input, without caring which bunch of code really does the job at the end.

    To wit, after saying

    my $a = Archive::Any->new($archive_file);
    I don't care if what I got was reblessed into a different class, or wheter it has a delegate, or whether it uses MMD inside. In fact, it probably isn't Archive::Tar at all, in any sense, since it doesn't provide it's full interface.

    When ->isa checks are used with respect to the delegate for the prupose of type checking (what kind of archive format do you encapsulate, mr Archive::Any instance?), then it's perhaps a design problem, because not caring is the reason we wrapped it in the first place.

    -nuffin
    zz zZ Z Z #!perl
      When ->isa checks are used with respect to the delegate for the prupose of type checking (what kind of archive format do you encapsulate, mr Archive::Any instance?), then it's perhaps a design problem, because not caring is the reason we wrapped it in the first place.
      But one may find desirable to have some common functionality plus extra type-specific features, e.g.
      if ($this->isa 'cool_archive') { print $this->name, " is a cool archive.\n", <<EOT; You also have the following cool features: ... (more cool features) EOT # interactively ask user to pick one... }
      Or is such a design to be considered inherently risky?
        This example is not well suited for a delegator model, but for a factory one, where each class presents it's routines to the routine displayer code (polymorphically, too - the code that has these archive plugins shouldn't care, it should only handle the interactivity).

        The open archive command takes a CLI, and dispatches to a factory constructor, that chooses the right class, and then the instance of this class is presented to the user with the enumerated options, extracted metadata, and so forth.

        -nuffin
        zz zZ Z Z #!perl