René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback
 

Win32::TieRegistry

Win32Api::Registry is a prerequisite for Win32::TieRegistry.
use warnings;
use strict;

my %reg;

my $companyName ="adp";
my $productName ="perlTest";
my $softwareKey ="HKEY_CURRENT_USER/Software";

use Win32::TieRegistry (
  TiedHash=>\%reg, 
  Delimiter=>'/',
);


my $productKey;
my $companyKey;


# does company exist in registry?
$companyKey = $reg{"$softwareKey/$companyName"};
if (defined $companyKey) {
  # yes, company exists, but does product also exist?
  $productKey=$reg{"$softwareKey/$companyName/$productName"};
  unless (defined $productKey) {
    # no, product does not exist
    $reg{"$softwareKey/$companyName/$productName"}={};
  }
}
else {
  # no, company does not exist in registry, create it:
  $reg{"$softwareKey/$companyName"}={};
  # also, create product:
  $reg{"$softwareKey/$companyName/$productName"}={};
}


$productKey = $reg{"$softwareKey/$companyName/$productName"};

# printing existing values
foreach my $k (keys %$productKey) {
  print "$k = $$productKey{$k}\n";
}

# setting the default value:
$$productKey{'/'} = "new default value";

# setting some other values:
$$productKey{'test_val_1'} = "7";
$$productKey{'test_val_2'} = 42;
$$productKey{'test_val_3'} = "0x470";