in reply to Changing the target of refs of unknown type
The "easy" way to inflict much hard-to-debug trouble upon yourself is with a proxy object that acts like the real thing, but forwards everything to the "real" object. Your "master" object(s) only hand out proxies, and you can switch the master easily then:
# Untested code, not for production use # see "hard-to-debug trouble" package Proxy; use strict; use vars qw($AUTOLOAD); sub AUTOLOAD { my $self = shift; $AUTOLOAD =~ m!::([^:]+)$! or die __PACKAGE__ . ": Weird method name $AUTOLOAD"; my $method = $1; $self->()->$method(@_); }; sub new { my ($package,$master) = @_; my $self = sub { my $s = shift; if (@_) { $master = shift; }; $master; }; bless $self, $package; $self; };
This package fails to fake caller(), so debugging will get harder, and it doesn't respect/propagate list/scalar explicitly, which might have unfortunate results for your code too. Use it as follows:
package Master::Object; sub new { my $package = shift; my $master = $package->create_a_new_object(@_); return Proxy->new($master); }; ... my $main = Master::Object->new(); # set a new master object: my $new_master = Master::Object->create_a_new_object(); $main->($new_master);
This is enough rope to hang yourself with. You should really rethink why you would want to do this ! :)
|
|---|