in reply to Re: RE: RE: Scoping and Objects
in thread Scoping and Objects

If I understand you correctly, you have a cache object. You want to be able to add methods to it at runtime, and some of those methods might need to manipulate the internals of the cache object.

btrott's technique is good -- that's probably the cleanest way to continue what you're doing. Add some methods to access the cache, and any subroutines added through references don't have to know the particulars about how the cache is implemented.

Another option (which I do not recommend if there's any other way around it) is to switch to your object's package and define a new subroutine or install one in the package's symbol table:

package main; # some stuff here package RadCache; sub mangle_cache { # do this and that }
or
package main; # stuff here my $sub_ref = sub { print "Messing with the cache.\n" }; *RadCache::mangle_cache = $sub_ref; # syntax may be a little off

I think you'd be better off just subclassing your cache object whenever you need to add a callback that works on the internals of the object. That may make your design easier to understand, anyway.