Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I have worked a lot with changes of the symbol table for Safe::World, and looking in the replies that your node got, I think that none of them are the best way.

Well, say that my way is better is easy, soo I will show what happens in the 3 different types:

  • DELETING FROM THE TABLE:
  • my $d = *{ 'main::destination::' }; delete $d->{foo};
    This will just remove all the entry from the table, but wont ensure that the sub reference was really removed from the memory. Making test of full deletion of packages for Safe::World, is easy to see that if you don't undef explicity a reference from the memory it will stay there even if you clean the HASH of the symbol table.

    But one of the problems here is that you remove all the references, soo, if you have a $foo and &foo, you will erase both.

  • undef
  • Just make a undef to the &sub actually works, but you will still have a *foo{CODE} value in the GLOB, what will use memory.

    Note that undef is the standar way to do that, and is the recomended way, but you won't have the GLOB exactly as you have it before set the sub.

  • Symbol::gensym
  • In Perl is impossible to create a GLOB that exists only in the scope. Soo, gensym() creates a new GLOB at Symbol::GENx, where x is a number. Soo, it won't be cleanned when you go out of the scope. Again you will be just duplicating everything.

  • local()
  • Since a GLOB really need to be in the symbol table, local(*FOO) will create a GLOB that replaces the already existing GLOB in the symbol table for the given scope. Soo, after go out of the scope, the previous will be back. The biggest problem is when you make references to the local(*GLOB) after go out of the scope. Soo, if you make local(*FOO), and want to make something like a closure of it to $holder = \*FOO, it won't work.

    Also for recursion or loop codes, the last local(*FOO) will change the previous sets.

  • NOW MAKEING A UNDO TO A GLOB:
  • Is easy, we just create a holder for the different types inside the GLOB, make a undef, than reset the values, unless the removed type:

    *FOO = sub { print "SUB FOO OK!\n" } ; $FOO = 123 ; print ">>>>>>>>>> BEFORE RM:\n" ; eval{ FOO() } ; warn($@) if $@ ; print "CODE: " . *FOO{CODE} . "\n" ; print "SCALAR: $FOO\n" ; glob_rm( \*FOO , 'CODE' ) ; print ">>>>>>>>>> AFTER RM:\n" ; eval{ FOO() } ; warn($@) if $@ ; print "CODE: " . *FOO{CODE} . "\n" ; print "SCALAR: $FOO\n" ; sub glob_rm { my $glob = shift ; my $type = uc( shift(@_) ) ; my %holder ; foreach my $tp ( qw(SCALAR ARRAY HASH CODE IO FORMAT) ) { $holder{$tp} = *{$glob}{$tp} if $tp ne $type ; } if ($type eq 'SCALAR') { undef ${*{$glob}} ;} elsif ($type eq 'ARRAY') { undef @{*{$glob}} ;} elsif ($type eq 'HASH') { undef %{*{$glob}} ;} elsif ($type eq 'CODE') { undef &{*{$glob}} ;} elsif ($type eq 'IO') { close *{$glob} ;} undef *{$glob} ; foreach my $Key ( keys %holder ) { *{$glob} = $holder{$Key} ;} }
    This will print:
    >>>>>>>>>> BEFORE RM: SUB FOO OK! CODE: CODE(0x1a72bac) SCALAR: 123 >>>>>>>>>> AFTER RM: Undefined subroutine &main::FOO called at tmp.pl line 17. CODE: SCALAR: 123
    Note that this approach is to remove a reference type of a GLOB. If you want to overwrite one of the types, the best is to just set the GLOB with a reference.

    Actually all of this depends of what you need. If you wan't speed, use just undef &FOO, but if you want to save memory I recomend the glob_rm() function.

    Enjoy! ;-P

    Graciliano M. P.
    "Creativity is the expression of the liberty".


    In reply to Re: undefining one slot of a typeglob by gmpassos
    in thread undefining one slot of a typeglob by AidanLee

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post; it's "PerlMonks-approved HTML":



    • Are you posting in the right place? Check out Where do I post X? to know for sure.
    • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
      <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
    • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
    • Want more info? How to link or How to display code and escape characters are good places to start.
    Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others exploiting the Monastery: (6)
    As of 2024-03-29 14:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found