Symbol::Glob?

Ovid on 2005-11-24T20:13:20

Once again I'm searching for a common utility module that I assume is out there. For example, I recently asked about deleting subroutines from globs. The problem here is that you can't really do it without deleting the entire glob. What needs to be done is to copy the glob slots except for the CODE slot, delete the glob, then add back the other slots. This seems like silly make-work. I'd expect there to be a module which properly abstracts this for me:

package Some::Package;
use Symbol::Glob;
# assumes current package unless specified
my $glob = Symbol::Glob->new({ name => 'foo' });

$glob->scalar(14);
$glob->sub( sub { return 'this is a sub' });
print $Some::Package::foo; # prints 14

$glob->delete('scalar');
print $Some::Package::foo; # undefined
print $glob->sub->();      # prints 'this is a sub'

$glob->delete;             # removes entire glob

Is there something that straightforward on the CPAN? I'm not seeing it.


Symbol::Values

rjbs on 2005-11-24T20:46:31

I have not investigated whether it does what you want, but it's in the ballpark.

Now, back to turkey.

Symbol::Glob 0.01

pemungkah on 2005-11-30T20:35:33

I decided to go ahead and write this, because I'm doing a fair amount of glob munging in WWW::Mechanize::Pluggable, and it'd be nice to have it better encapsulated. It does what you asked it to do in your example, and it's available on CPAN.

As a nice bonus, I got a chance to try the inside-out object model and Class::Std. Found it much easier to concentrate on getting the details of the glob manipulation right because I knew that the object access code was safe.

I did find one thing I needed to do that wasn't simple with Class::Std. As you noted, removing a symbol table entry safely consists of deleting the glob altogether, then inserting the fields you want to keep back into the glob again. A Class::Std class doesn't let you iterate over all of the fields in the object easily, because they aren't *in* the object. I ended up creating a mapping hash that mapped method names to the storage hashes to be able to find them all easily.