anadem has asked for the wisdom of the Perl Monks concerning the following question:

I'd like to have delete operations on the registry return true/false. The docs for TieRegistry (at tinyurl.com/5ofltk) say
".. by calling the FastDelete member function: # # $prevSetting= $regKey->FastDelete(1); # # which will cause all subsequent delete operations via $regKey to sim +ply return a true value if they succeed.
Unfortunately I'm too dumb to get it to work. In my code, when deleting a reg value (not key) the delete operation either deletes the value and returns its data, or, when the value is not in the registry the delete fails and returns a string "The system cannot find the file specified."

Can some kind monk please point out the error of my ways?
Here's a simple sample:

use Win32::TieRegistry( Delimiter=>"/", ArrayValues=>1 ); $key = "LMachine/Software/Microsoft/Windows/CurrentVersion/SharedDlls/ +"; $value = "c:\\aaa.aaa"; # $Registry->{ $key } = { "/$value" => [ "0x0003", "REG_DWORD" ] }; $prevSetting = $Registry->FastDelete(1); $res = delete $Registry->{ $key . $value }; if( $res ) { print( "removed value $value and res is $res\n" ); } else { print( " delete failed and res is '$res'\n" ); }
Those docs go on to say "This optimization is automatically done if you use delete in a void context."
It may be asking too much, but if someone can tell me in how to use delete in a void context, that would also be helpful.
Many thanks

Replies are listed 'Best First'.
Re: TieRegistry FastDelete problem
by ikegami (Patriarch) on Jul 10, 2008 at 21:14 UTC
    Testing reveals it only applies when deleting keys (as opposed to values).
    use strict; use warnings; use Win32::TieRegistry ( Delimiter => '/' ); my $key_path = 'LMachine/Software/DELETE ME/'; $Registry->{$key_path} = {}; $Registry->FastDelete($ARGV[0]); my $res = delete $Registry->{$key_path}; if( $res ) { print( "removed value and res is $res\n" ); } else { print( " delete failed and res is '$res'\n" ); }
    >perl test.pl 0 removed value and res is HASH(0x225374) >perl test.pl 1 removed value and res is 1
Re: TieRegistry FastDelete problem
by pc88mxer (Vicar) on Jul 10, 2008 at 20:44 UTC
    if someone can tell me in how to use delete in a void context, that would also be helpful.
    Void context means you are not asking for any return values:
    delete $Registry->{$key}; # void context $res = delete $Registry->{$key}; # scalar context if (delete $Registry->{$key}) { ... } # also scalar context @results = delete $Registry->{$key}; # list context
    Update: 'array' context should be 'list' context (thanks ikegami.)
Re: TieRegistry FastDelete problem
by anadem (Scribe) on Jul 10, 2008 at 22:12 UTC
    Thank you, ikegami and pc88mxer. Much appreciated.