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 ! :)


In reply to Re: Changing the target of refs of unknown type by Corion
in thread Changing the target of refs of unknown type by Irrelevant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.