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

I have written this code to change a key in the registry to a specific path. When a user selects a database , the program is supposed to edit the registry and then launch the program - reading the registry key.

The problem is this.

The menu options will work intermittently. Some times option 1 will work perfectly , and other time option 1 will change the key value to an option 2 value.

What am I doing wrong?

Any help would be greatly appreciated!

Excerpt from the Perl code - Editing the registry
============================================================
print ("Please choose a database to generate a report from.[1-3]\n"); print ("[1] Database1\n"); print ("[2] Database2\n"); print ("[3] Database3\n"); print (">"); $a = <STDIN>; chomp $a; if ($a == "1") { $pathe = "K:\\path1"; } elsif ($a == "2") { $pathe = "K:\\path2"; } elsif ($a == "3") { $pathe = "K:\\path3; } chomp $pathe; use Win32::Registry; $p = "CLSID\\{42DE62C7-AD04-11D1-BE7E-00E0290B0890}\\LocalServer32"; $main::HKEY_CLASSES_ROOT->Open($p, $CurrVer) || die "Open: $!"; $CurrVer->GetValues(\%vals); # get sub keys and value -hash ref foreach $k (keys %vals) { $key = $vals{$k}; } $CurrVer->SetValue("",REG_SZ, "$pathe"); foreach $k (keys %vals) { $key = $vals{$k}; print "$$key[2]\n"; } $p = "CLSID\\{42DE62C7-AD04-11D1-BE7E-00E0290B0890}\\LocalServer32"; $main::HKEY_CLASSES_ROOT->Open($p, $CurrVer) || die "Open: $!"; $CurrVer->GetValues(\%vals); # get sub keys and value -hash ref foreach $k (keys %vals) { $key = $vals{$k}; } $CurrVer->SetValue("",REG_SZ, "$pathe"); foreach $k (keys %vals) { $key = $vals{$k}; print "$$key[2]\n"; } $p = "CLSID\\{E850B195-F118-4C04-9E57-9901CDB7430D}\\LocalServer32"; $main::HKEY_CLASSES_ROOT->Open($p, $CurrVer) || die "Open: $!"; $CurrVer->GetValues(\%vals); # get sub keys and value -hash ref foreach $k (keys %vals) { $key = $vals{$k}; } $CurrVer->SetValue("",REG_SZ, "$pathe"); foreach $k (keys %vals) { $key = $vals{$k}; print "$$key[2]\n"; } $p = "CLSID\\{7C4A7B0C-6055-4551-B870-F112BE54C094}\\LocalServer32"; $main::HKEY_CLASSES_ROOT->Open($p, $CurrVer) || die "Open: $!"; $CurrVer->GetValues(\%vals); # get sub keys and value -hash ref foreach $k (keys %vals) { $key = $vals{$k}; } $CurrVer->SetValue("",REG_SZ, "$pathe"); foreach $k (keys %vals) { $key = $vals{$k}; print "$$key[2]\n"; }
============================================================

Edit by tye, add CODE tags, remove BR tags

Replies are listed 'Best First'.
Re: Perl with Windows Registry
by Sandy (Curate) on May 25, 2004 at 22:32 UTC
    Warning: I know nothing about Win32::Registry, but nonetheless, I tried to see if I could learn something from your post.

    There are 4 bits of code that are repeated over. Maybe a loop over your various definitions of $p might be in order.

    But this bit of code definately confuses me...

    foreach $k (keys %vals) { $key = $vals{$k}; } $CurrVer->SetValue("",REG_SZ, "$pathe"); foreach $k (keys %vals) { $key = $vals{$k}; print "$$key[2]\n"; }
    Near as I can figure, the 1st foreach loop ends up setting the $key scalar to one of the values in the hash, with no knowledge of what the key ($k) / value ($key) combination is. Note that the manual clearly states that (keys %myhash) returns the keys in a pseudo-random order.

    Example

    #!/usr/bin/perl # arbitray hash table my %vals = ("1","one","2","two","3","three"); # your code foreach $k (keys %vals) { $key = $vals{$k}; } print "\$key=$key\n"; # add something else to hash table %vals = ("1","one","2","two","3","three","4","four","5","five"); # your code foreach $k (keys %vals) { $key = $vals{$k}; } print "\$key=$key\n";
    gives
    $key=two $key=five
    How this affects the statement
    $CurrVer->SetValue("",REG_SZ, "$pathe");
    I have no idea, because as I said before, I am not familiar with the windows registry module. But how is $key known by the SetValue function? Is it a global variable??

    Sorry not to be of more help, but maybe some of this might help you figure out what your program is really doing, and what it is you want it to do.

      Thankyou Sandy for your quick reply.

      I think you found out what my problem was. Although I have not yet fixed it.
      As for this line:

      $CurrVer->SetValue("",REG_SZ, "$pathe");

      It writes the value $pathe to a registry key.

      Thankyou again for your help. I will keep everyone posted on my progress.

      Joshua.