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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • 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 goofing around in the Monastery: (5)
As of 2024-04-19 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found