#!/usr/bin/perl
use warnings;
use strict;
use feature 'say';
use Win32::TieRegistry;
#
# Win32::TieRegistry creates a variable $Registry which is a VIRTUAL root above
# of all registry trees (Such as HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE etc)
#
# Make sure it does indeed exist!
#
die unless $Registry;
# Each regstry key is identified by a name that differs, however, from
# the known HKEY_* names:
say for keys (%$Registry);
# Classes\
# LMachine\
# CConfig\
# CUser\
# Users\
# PerfData\
# DynData\
In order to unambiguously distinguish between keys and values, there are two rules:
Append the delimiter to each key name
Prepend each value name with the delimiter
This can lead to paths with two adjacent delimiters, eg HKEY_CURRENT_USER/Environment//PATH.
Showing a particular registry value
#!/usr/bin/perl
use warnings;
use strict;
use feature 'say';
use Win32::TieRegistry(Delimiter=>'/');
my $path_value = $Registry->{'CUser/Environment//PATH'};
# Does rule «prepend value with delimiter» always have to
# be followed? It doesn't seem so:
die unless $path_value eq $Registry->{'CUser/Environment/PATH'};
# Alternative way to get value:
die unless $path_value eq $Registry->{CUser}->{Environment}->{PATH};
say for (split ';', $path_value);
#!/usr/bin/perl
use warnings;
use strict;
use feature 'say';
use Win32::TieRegistry(Delimiter=>'/');
my $path_value = $Registry->{'CUser/Environment//PATH'};
# Does rule «prepend value with delimiter» always have to
# be followed? It doesn't seem so:
die unless $path_value eq $Registry->{'CUser/Environment/PATH'};
# Alternative way to get value:
die unless $path_value eq $Registry->{CUser}->{Environment}->{PATH};
say for (split ';', $path_value);
Determining if a path is a key, a value or neither
#!/usr/bin/perl
use warnings;
use strict;
use Win32::TieRegistry(Delimiter => '/');
determine('HKEY_CURRENT_USER/Environment');
determine('HKEY_CURRENT_USER/Environment/PATH');
determine('HKEY_CURRENT_USER/Environment/does-not-exist');
sub determine {
my $path = shift;
if (my $key_ = $Registry->Open($path)) {
print "$path is a key\n";
return;
}
if (exists $Registry->{$path}) {
printf "$path is a value: %s\n", substr($Registry->{$path}, 0, 50);
return
}
print "$path is neither\n";
}
use warnings;
use strict;
use Win32::TieRegistry (Delimiter=>'/');
my $companyName ="TQ84";
my $productName ="perlTest";
my $softwareKey ="HKEY_CURRENT_USER/Software";
my $companyKey;
my $productKey;
# does company exist in registry?
$companyKey = $Registry->{"$softwareKey/$companyName"};
if (defined $companyKey) {
print "I found Company $companyName in registry\n";
$productKey=$Registry->{"$softwareKey/$companyName/$productName"};
if (defined $productKey) {
print "Found product $productName, too\n";
}
else {
print "But, I did not find $productName\n";
$productKey = $Registry->{"$softwareKey/$companyName/$productName"}={};
}
}
else {
print "I did not find $companyName in registry\n";
$Registry->{"$softwareKey/$companyName"}={};
# also, create product:
$productKey = $Registry->{"$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";