http://www.perlmonks.org?node_id=360992


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