Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Removing keys in the registry (code)

by tye (Cardinal)
on Jun 04, 2004 at 16:47 UTC ( #360992=note: print w/ replies, xml ) Need Help??


in reply to Removing keys in the registry

This isn't particularly hard to solve. I typed into the Chatterbox a subroutine that does it when you asked about it there. I guess you ran off before waiting for a reply. It can be as simple as:

sub deleteReg { my( $key, $name )= @_; for( eval { keys %{$key->{$name}} } ) { deleteReg( $key, "$name\\$_" ); } delete $key->{$name}; } deleteReg( $Registry, "HKEY_LO­CAL_MACHINE/SOFTWARE­/MyWay/", );

Here is a more robust and efficient version (though I'm sure some will consider the above use of eval to be more robust and I agree, but I wanted to show an alternative to that):

*isa= \&UNIVERSAL::isa; sub deleteReg { my $key= shift @_; # Registry key to delete item from my $name= shift @_; # Name of item to delete { my $item= $key->{$name}; if( ! $item ) { warn "Can't read $name in ", $key->Path(), ": $^E\n"; return; } if( ref($item) && isa($item,"HASH") ) { for my $subName ( keys %$item ) { deleteReg( $item, $subName ); } } } delete $key->{$name} or warn "Can't delete $name from ", $key->Path(), ": $^E\n"; } deleteReg( $Registry, "HKEY_LO­CAL_MACHINE/SOFTWARE­/MyWay/", );

Another way to do it doesn't bother with values:

sub regDelTree { my $rootKey= shift @_; # Reg key to del subtree from my $keyName= shift @_; # Name of key to delete { my $key= $rootKey->{$keyName}; if( ! $key ) { warn "Can't open $keyName in ", $rootKey->Path(), ": $^E\n"; return; } for( $key->SubKeyNames() ) { regDelTree( $key, "$_\\" ); } } delete $rootKey->{$keyName} or warn "Can't delete $keyName from ", $rootKey->Path(), ": $^E\n"; } regDelTree( $Registry, "HKEY_LO­CAL_MACHINE/SOFTWARE­/MyWay/", );

The main reason that one of these isn't just included in the module is that dealing with errors in a general way in nested code like this is a pain to do well. The above code just punts and uses warn which is reasonable but often isn't what a user of a module would want. I've wanted to add something like this to the module and I will in a future release.

Code untested at this point. Yes, you might have to do a little work yet. (:

- tye        


Comment on Re: Removing keys in the registry (code)
Select or Download Code
Re^2: Removing keys in the registry (code)
by ZlR (Chaplain) on Aug 25, 2005 at 09:04 UTC
    Hello tye,
    and thanks for this code.

    I do have a question : what does the eval do ?

    I tried to play with $key->{$name} and got a lot of hash references.
    The eval does bring the name of the key to delete. Why ?

    Also after executing this code i end up with a default value in the hive and can't find a way to delete it ?!

    Thanks if you can shed some light ,
    Z.

      what does the eval do ?

      If a registry key contains values, then keys will return the names of those values. But $key->{$aValueName} will not give you back a hash reference so the %{$key->{$name}} would fail with "Can't coerce array into hash" or something.

      sub deleteReg { my( $key, $name )= @_; for( eval { keys %{$key->{$name}} } ) { deleteReg( $key, "$name\\$_" ); } delete $key->{$name}; }

      The eval will return what keys returns if $name is a subkey name and will trap the die and return and empty list if $name is a value name. So for( eval ... ) will silently skip values.

      Also after executing this code i end up with a default value in the hive and can't find a way to delete it ?!

      You can't have a registry key without an empty default value. That is just the way the registry is.

      - tye        

        ok i get it :) that's a nice trick .

        You can't have a registry key without an empty default value. That is just the way the registry is.

        yes but what if i wanted to destroy that very "registry folder" after i emptied from all its values ?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://360992]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-12-26 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls