Here's something I wrote for a friend a while back.  If you want to access remote computers, you just have to put //$computername before the HKEY.
 
#####################################################
##                                                 ##
##     Win32-TieRegistry_Tutorial v1.0.052302      ##
##            written by Tim Johnson               ##
##                  05-23-2002                     ##
##                                                 ##
#####################################################
 
#Welcome to the tutorial.  This should give you a good
#enough idea of how to manipulate the Windows registry
#to perform most tasks.  Let's get started by declaring
#our modules.
 
use strict; #always a good idea in general
use Win32::TieRegistry(Delimiter => "/");#So that we can use forward slashes
 
#A new Win32::TieRegistry object is automatically created
#called $Registry.  $Registry is a hash reference[1].  Each
#key of the hash it points to is either another reference to
#another hash or a scalar which corresponds to a"value"(to
#use the registry term).  This will make a little more
#sense after you've used it a while.
 
#Now that we've created the registry hashes, let's try
#working with what we've created.  We can create a new
#object[1] that points to a specific part of the registry
#or we can just shoot for the part we want to change.
#For the purposes of making this easier to read, I'll make
#a new object.
 
my $IEKey = $Registry->{'HKEY_CURRENT_USER/Software/Microsoft/Internet Explorer/Main/'};
 
#The arrow operator, the way it is used in this example, denotes that we are
#working with a hash reference, and not a key of %Registry.  We now have
#an object that represents the exact key we want to work with.  Now let's
#change the 'Start Page' value. 
 
$IEKey->{'Start Page'} = "http://www.perl.org";
 
#Viola!  Your start page has changed.  Now let's try creating a new key.
 
$Registry->{'HKEY_CURRENT_USER/Software/Perl Beginners/'} = {};
 
#We've created a new, empty key.  Now let's set the default value.
 
$Registry->{'HKEY_CURRENT_USER/Software/Perl Beginners//'} = "Camel";
 
#Notice that since the default value does not have a name, there
#are TWO slashes at the end of the key.  With Win32::TieRegistry,
#If you want to resolve ambiguities about whether the item at the end
#is a registry key or registry value, you can (a)add a slash to the end
#to denote a key, or (b)add two slashes before the value, so our
#double-slash denotes a value with a zero-length name[2].
 
#For our next trick, lets create a new object for simplicity, and add
#a value to our new key.
 
my $BeginnerKey = $Registry->{'HKEY_CURRENT_USER/Software/Perl Beginners/'};
$BeginnerKey->{'TIMTOWTDI'} = "There Is More Than One Way To Do It";
 
#And for a variation on a theme, lets try a REG_DWORD value.  This changes
#things a little because it is a binary value.  We will have to specify
#both the value and the type like so, to avoid accidentally creating
#a string value.
 
$BeginnerKey->{'MyDWORD'} = ['0x0001',REG_DWORD];
 
#Finally, let's look at reading values.  We are still treating each
#key as a hash reference, so we will have to dereference our hash to
#get the value.
 
my $KeyValue = $BeginnerKey->{'TIMTOWTDI'};
print "My new variable's value is:  $KeyValue\n\n";
 
#That looks pretty clean, but what about getting all of the keys?
#If we dereference the hash, we can use the keys() function.
 
my @KeyArray = keys %{$BeginnerKey};
print "Here are the values in my registry key:\n";
foreach(@KeyArray){
 if($_ eq '/'){
  print "   /(default)\n";
 }else{
  print "   $_\n";
 }
}
 
#There are more things you can do with this module, but I think that
#covers the basics.  If you have any questions or comments, please post
#them back to the list and I'll try to answer them if I can.
 
#[1]Technically $Registry is a Win32::TieRegistry object.  It is a tied
#   hash that allows you to manipulate the hash and automagically
#   cause changes to be made to the registry at the same time as changes
#   are made to the hash.  Just think of it as a hash reference if that
#   seems confusing.
#[2]Win32::TieRegistry will cut you a little slack when it comes to
#   calling keys and values, but it is always a good idea to add a slash
#   to the end whenever you are working with a key.  This reduces the
#   possibility of accidentally creating a value when you meant to create
#   a key, or vice versa.
 
 
-----Original Message-----
From: Eric Hennessey [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, November 20, 2002 7:38 AM
To: [EMAIL PROTECTED]
Subject: FW: TieRegistry

Re-posting due to previously posting to wrong list.
 
-----Original Message-----
From: Eric Hennessey
Sent: Wednesday, November 20, 2002 10:34 AM
To: [EMAIL PROTECTED]
Subject: TieRegistry

Hi all...
 
Does anyone out there have any sample code snippets for using TieRegistry to create keys and update key values, for both local and remote machines?  Any samples would be most appreciated.
 
Thanks!
Eric

Reply via email to