Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Selective deletion from symbol table?

Reply
Thread Tools

Selective deletion from symbol table?

 
 
J Krugman
Guest
Posts: n/a
 
      04-19-2004



Suppose I have this:

*foo = sub { print "hello from foo\n" };
*foo = \3;

Now &foo and $foo are defined in the current package (let's say
it's main). Now suppose that I want to undefine &foo. Something
like

delete $main::{foo}

would undefine not only &foo but $foo as well. Is there a way to
undefine *only* &foo?

Thanks!

jill

P.S. The motivation for this question is the sub mk_classdata, the
sole method in the CPAN module Class:ata::Inheritable:

sub mk_classdata {
my ($declaredclass, $attribute, $data) = @_;

my $accessor = sub {
my $wantclass = ref($_[0]) || $_[0];

return $wantclass->mk_classdata($attribute)->(@_)
if @_>1 && $wantclass ne $declaredclass;

$data = $_[1] if @_>1;
return $data;
};

my $alias = "_${attribute}_accessor";
*{$declaredclass.'::'.$attribute} = $accessor;
*{$declaredclass.'::'.$alias} = $accessor;
}

This is nice, but IMHO one *minor* flaw is that once one makes a
derived class "opaque" with respect to a given class attribute
(which happens if one sets the class attribute for the derived
class $wantclass) there is no way to undo this. It would be nice
if, for example, setting the class attribute for the derived class
to undef had the effect of making the derived class once again
"transparent" with respect to the class attribute. Or course, this
is not perfect either, since there may be cases in which undef is
a perfectly acceptable value for the class attribute to have, and
in these cases we wouldn't want this behavior, but, in any case,
it got me thinking about the general problem of selectively undoing
an assignment such as

*{$declaredclass.'::'.$attribute} = $accessor;


--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.

 
Reply With Quote
 
 
 
 
Steven Kuo
Guest
Posts: n/a
 
      04-19-2004
On Mon, 19 Apr 2004, J Krugman wrote:

>
> Suppose I have this:
>
> *foo = sub { print "hello from foo\n" };
> *foo = \3;
>
> Now &foo and $foo are defined in the current package (let's say
> it's main). Now suppose that I want to undefine &foo. Something
> like
>
> delete $main::{foo}
>
> would undefine not only &foo but $foo as well. Is there a way to
> undefine *only* &foo?
> ...





Use 'undef' as described in 'perldoc perlsub':

use strict;
use warnings;
use vars qw(*foo);

*foo = sub { print "hello from foo\n" };
*foo = \3;

foo();
print $foo, "\n";

undef(&{*foo{CODE}}); # see 'perldoc perlref' regarding this syntax

# or more simply:
# undef(&foo);

eval {
foo();
};

print "Uable to run foo subroutine : $@";

print $foo, "\n";

--
Hope this helps,
Steven

 
Reply With Quote
 
 
 
 
J Krugman
Guest
Posts: n/a
 
      04-19-2004
In <(E-Mail Removed)> Steven Kuo <(E-Mail Removed)> writes:

< snip >

>Hope this helps,


Yes, very much. Thanks!

jill

--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(Encryption Package) error: cannot find symbol symbol: class BaseNCode clusardi2k@aol.com Java 6 08-29-2012 08:33 PM
Why ":symbol" failed but 'symbol' successed with JRuby 1.0.3? Song Ma Ruby 2 07-20-2008 04:08 AM
Selective File Sharing permissions on small workgroup. =?Utf-8?B?SlAgQXBvbGxv?= Wireless Networking 2 10-11-2005 12:30 PM
what's differnece between #ifdef symbol and #if defined(symbol) baumann@pan C Programming 1 04-15-2005 08:25 AM
Selective delete of auto complete list in Firefox? Jim Firefox 2 03-06-2005 07:38 PM



Advertisments