in reply to Re: searching for an object reference
in thread searching for an object reference

Perhaps you might want to attack this from the other end. Instead of deep-searching to find references to the class, just dynamically update the log class to point where you want, overriding only the critical methods:
package Log; sub new { my ($class) = shift; bless {}, $class; } sub write { print "write old\n"; } sub open { print "open old\n"; } sub close { print "close old\n"; } 1; package NewLog; sub import { for my $sub (qw(new write open)) { no strict 'refs'; *{'Log::'.$sub} = *{'NewLog::'.$sub}; } } sub new { my ($class) = shift; bless {}, $class; } sub write { print "write new\n"; } sub open { print "open new\n"; } 1; use Log; use NewLog; my $log = new Log; $log->open; $log->write; $log->close;
If you run this, you'll see the following output:
open new write new close old
Note that open and write are overridden, and close is not. This incurs less overhead if you've got a lot of Log objects, or if your objects using them are very deeply nested.

Replies are listed 'Best First'.
Re^3: searching for an object reference
by Mostly Harmless (Initiate) on Jul 06, 2005 at 07:38 UTC
    Thanks, that's a good idea. However I can't use it in my case, as there are too many methods in Log.pm to override - they all use couple of instance variables, which should have new values. Ideally this would work for me:
    package Redirector; my $new_obj; # Redirect all methods calls of object A to object B. sub redirect { my $new_obj = shift; my @methods = @_ || 'all'; <instrument the methods so that Redirector::foo is called instead of old_obj::foo> } sub print { my $real_obj = shift; # Conveniently switch the object. $new_obj->print(@_); }
    The issue with the above is keeping track of $new_obj. This needs to be a package variable, and the class itself has to be a singleton - unless a hash with package names as keys. Any ideas ? For now, deepcopying looks the simplest solution to me, as the data to be traversed is small in size. But I think your idea can be generalized to a Redirector CPAN module !