Selective deletion from symbol table?

Discussion in 'Perl Misc' started by J Krugman, Apr 19, 2004.

  1. J Krugman

    J Krugman Guest

    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::Data::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.
     
    J Krugman, Apr 19, 2004
    #1
    1. Advertising

  2. J Krugman

    Steven Kuo Guest

    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
     
    Steven Kuo, Apr 19, 2004
    #2
    1. Advertising

  3. J Krugman

    J Krugman Guest

    In <> Steven Kuo <> 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.
     
    J Krugman, Apr 19, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam Black

    selective table borders ?

    Sam Black, Nov 30, 2006, in forum: HTML
    Replies:
    2
    Views:
    1,523
    Jukka K. Korpela
    Dec 3, 2006
  2. baumann@pan
    Replies:
    1
    Views:
    769
    Richard Bos
    Apr 15, 2005
  3. Song Ma
    Replies:
    2
    Views:
    260
    Charles Oliver Nutter
    Jul 20, 2008
  4. animesh
    Replies:
    0
    Views:
    88
    animesh
    Dec 10, 2007
  5. Replies:
    6
    Views:
    2,025
Loading...

Share This Page