in reply to object swizzling?

Since what you call objects are really references, if you just re-bless $x into SomeOtherClass and assign it to $z, then both variables point to the same object, and methods that modify it will affect both variables.

Of course if you want a proxy that just "intercepts" some method calls and forwards most of the rest, you could just inherit from the class you're trying to proxy.

Replies are listed 'Best First'.
Re^2: object swizzling?
by perl5ever (Pilgrim) on May 05, 2009 at 21:25 UTC
    I guess that will work. I just have to make sure that my proxy is of the same underlying type (HASH, ARRAY, SCALAR) as the real object.
    Of course if you want a proxy that just "intercepts" some method calls and forwards most of the rest, you could just inherit from the class you're trying to proxy.

    In the lazy-load case I want to ensure that the real object is loaded before forwarding, so it's not that simple.

      In the lazy-load case I want to ensure that the real object is loaded before forwarding, so it's not that simple.
      You can call the super class's constructor first:
      package 'Bar'; use base 'Foo'; sub new { my $class = shift; my $parent_class_object = SUPER->new(); ... }
      And then store it, or use it, or rebless it, or whatever.