Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

•Re: Change the parent of an instance

by merlyn (Sage)
on May 11, 2004 at 18:27 UTC ( [id://352530]=note: print w/replies, xml ) Need Help??

in reply to Change the parent of an instance

Well, you could do this by re-blessing, but that'll suck. Instead, you should have a member variable that handles certain behaviors as a pluggable value, and then replace that member variable as the role of the object changes.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

  • Comment on •Re: Change the parent of an instance

Replies are listed 'Best First'.
Re: ?Re: Change the parent of an instance
by muba (Priest) on May 11, 2004 at 18:34 UTC
    What's wrong with reblessing then? The variable keeping track of the actual type might be a good idea, but many of the methods will then look like
    if ($somehow_is_a_foe) { } else { }
    because the two share a lot, although they hardly share anything.

    Update: I have not yet thought about re-blessing. I think it's a good idea, worthwhile trying but if you say it's bad (or at least, will suck) I'll be glad to know why, just to find out if re-blessing is what I want.
      No, I mean you plug it like this:
      package Elf; sub named { my $class = shift; bless { Name => shift; Actions => Elf::Live::, Has => {}, }, $class; } sub die { shift->{Actions} = Elf::Dead::; } sub can_attack { # delegate to actions shift->{Actions}->can_attack(@_); } sub enemy_of { shift->{Actions}->enemy_of(@_); } package Elf::Live; sub can_attack { ... } sub enemy_of { ... } package Elf::Dead; sub can_attack { ... } sub enemy_of { ... }
      A simple change of the Actions variable by plugging in a different "behavior container" causes a bank-switch of those behaviors to a new motif. I forget the "gang of four" pattern for this, but it's one of those. {grin}

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        Ah! Now I see!

        I think it's the best idea I've seen so far, assuming re-blessing really sucks. I'll have a look at it (a big re-style of the code is now required but it's worth it).

        Just for my information, could you please tell me the disadvanteges of re-blessing?

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://352530]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-06-21 09:53 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.