Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

SysEnvAdd.pl

by RayRay459 (Pilgrim)
on Jul 19, 2001 at 19:52 UTC ( #98120=sourcecode: print w/replies, xml ) Need Help??
Category: Win32 Stuff
Author/Contact Info Ray Espinoza ray.espinoza@ebay.com
Description: This program will add a System Environment Variable. It will require a reboot to take effect.

#!D:\perl\bin\perl
use strict;

my ($Reg, $Host, $Key, $Value);
use Win32::TieRegistry ( 
    TiedRef => \$Reg, 
    ArrayValues => 1, 
    Delimiter => '/', 
    ':REG_' 
);

# Asking for machine name and info to be added to the sys env var
print "What host would you like to connect to? ";
chomp($Host = <STDIN>);
print "What name would you like for the Sys Environment Variable? ";
chomp($Key = <STDIN>);
print "What value would you like to assign to this variable? ";
chomp($Value = <STDIN>);

# this is the system environment variable area done on the registry
my $SysEnv= $Reg->Connect("$Host", "LMachine/System/CurrentControlSet/
+Control/Session Manager/Environment/") 
    or  die "Can't connect to $Host 's registry or can't open Registry
+ key, Session Manager/Environment: $^E
+\n";

# create a new value and set it's data
$SysEnv->{"/$Key"} = "$Value";

Replies are listed 'Best First'.
(tye)Re: SysEnvAdd.pl
by tye (Sage) on Jul 19, 2001 at 21:28 UTC

    Here are some minor suggestions:

    • Allow the data to be specified on the command line. Having to type the values on separate lines can be inconvenient in some situations.
    • Turn on warnings.
    • Handle EOF (or an error) when reading from STDIN.
    • Environment variables are usually given a value type of REG_EXPAND_SZ (instead of the default REG_SZ that you used) so that they can be, for example, "%SystemRoot%\\system32".
    • Check for failure on the insert.
    Which results in something like this:
    #/usr/bin/perl -w use strict; my $Reg; use Win32::TieRegistry ( TiedRef => \$Reg, ArrayValues => 1, Delimiter => '/', ':REG_' ); my( $Host, $Key, $Value )= @ARGV; # Asking for machine name and info to be added to the sys env var if( ! defined $Host ) { print "What host would you like to connect to? "; $Host= <STDIN> or exit; chomp $Host; } if( ! defined $Key ) { print "What name would you like for the Sys Environment Variable? +"; $Key= <STDIN> or exit; chomp $Key; } if( ! defined $Value ) { print "What value would you like to assign to this variable? "; $Value= <STDIN> or exit; chomp $Value; } # this is the system environment variable area done on the registry my $SysEnv= $Reg->Connect("$Host", "LMachine/System/CurrentControlSet/ +Control/Session Manager/Environment/") or die "Can't connect to $Host 's registry or can't open Registry + key, Session Manager/Environment: $^E +\n"; # create a new value and set its data $SysEnv->{"/$Key"} = [ $Value, REG_EXPAND_SZ ] or die "Can't create $Key ($Value): $^E\n";

            - tye (but my friends call me "Tye")
      thanks tye. you rock!! I am always looking for advice on how to better writing in Perl. ~Ray~
Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://98120]
help
Chatterbox?
[ambrus]: I think the minimal implementation here is just a timer and io function, plus pushing to the @REGISTRY.
[Corion]: ambrus: Yeah, at least that's what I think I'll start with. A timer and potentially the dummy IO function that never is ready
[ambrus]: That only lets you invoke the main loop from Prima->run;, not from AnyEvent->condvar- >recv; but that shouldn't matter

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2016-12-08 12:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (141 votes). Check out past polls.