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        


In reply to Re: Removing keys in the registry (code) by tye
in thread Removing keys in the registry by sulfericacid

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.