Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

vienv - Edit local environment variables on Win32

by tye (Cardinal)
on Jun 12, 2003 at 15:31 UTC ( #265386=CUFP: print w/ replies, xml ) Need Help??

This code lets you edit your environment variables for your current command prompt. Simply run it through pl2bat (in case you don't have WinXP), put it somewhere in your PATH (like in perl/bin), set the VISUAL environment variable to your favorite editor (if you like, notepad is the default) and type vienv at the command prompt.

Then edit, copy/paste, or whatever to your environment variables. When you save and exit, the environment of the command prompt you launched it from will be changed. It just runs the code you save, so to unset an environment variable, delete everything after the "=".

                - tye
#!/usr/bin/perl -w use strict; main( @ARGV ); exit( 0 ); sub main { my( $file )= @_; my $start= tell(DATA) or die "Can't tell(DATA): $!"; open DATA, "+< $0" or die "Can't read self ($0): $!\n"; seek( DATA, $start, 0 ) or die "Can't fseek(DATA,$start,0): $!"; die "Expected :endofperl after __END__ of $0.\n" unless <DATA> =~ /^\s*:endofperl\s*$/i; open TEMP, "< $file" or die "Can't read $file: $!\n"; chomp( my @curr= <TEMP> ); close TEMP; my %curr= map { ( split /=/, $_, 2 )[0,1] } @curr; system( ( $ENV{VISUAL} || "notepad" ), qq("$file") ); open TEMP, "< $file" or die "Can't reread $file: $!\n"; chomp( my @user= <TEMP> ); close TEMP; my %user= map { ( split /=/, $_, 2 )[0,1] } @user; for my $key ( keys %user ) { if( ! exists $curr{$key} ) { next; } elsif( $curr{$key} eq $user{$key} ) { delete $user{$key}; } delete $curr{$key}; } seek( DATA, 0, 1 ) or die "Can't fseek(DATA,0,1): $!"; for my $key ( keys %curr ) { print DATA "set $key=\n"; } for my $key ( keys %user ) { print DATA qq{set $key=$user{$key}\n}; } truncate DATA, tell(DATA); } __END__ :endofperl

Comment on vienv - Edit local environment variables on Win32
Download Code
Re: vienv - Edit local environment variables on Win32 (fixed)
by tye (Cardinal) on Oct 10, 2010 at 23:32 UTC

    Silly me. When I posted this, I must have removed all of the pl2bat boilerplate considering it a distraction. But it wasn't pl2bat boilerplate; it was modified pl2bat stuff where the modifications were vital to the functioning of the script. The above code just doesn't work as-is. I guess nobody who bothered to try (if any) bothered to complain. Sorry about the mix-up. Below is a working version:

    @rem = '--*-Perl-*-- @echo off set > %tmp%/vienv.tmp perl -x -S "%0" %TMP%/vienv.tmp if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl if %errorlevel% == 9009 echo You do not have Perl in your PATH. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul goto endofperl @rem '; #!/usr/bin/perl -w #line 15 use strict; Main( @ARGV ); exit( 0 ); sub Main { my( $file )= @_; my $start= tell(DATA) or die "Can't tell(DATA): $!"; open DATA, "+< $0" or die "Can't read self ($0): $!\n"; seek( DATA, $start, 0 ) or die "Can't fseek(DATA,$start,0): $!"; die "Expected :endofperl after __END__ of $0.\n" unless <DATA> =~ /^\s*:endofperl\s*$/i; open TEMP, "< $file" or die "Can't read $file: $!\n"; chomp( my @curr= <TEMP> ); close TEMP; my %curr= map { ( split /=/, $_, 2 )[0,1] } @curr; system( ( $ENV{VISUAL} || "notepad" ), qq("$file") ); open TEMP, "< $file" or die "Can't reread $file: $!\n"; chomp( my @user= <TEMP> ); close TEMP; my %user= map { ( split /=/, $_, 2 )[0,1] } @user; for my $key ( keys %user ) { if( ! exists $curr{$key} ) { next; } elsif( $curr{$key} eq $user{$key} ) { delete $user{$key}; } delete $curr{$key}; } seek( DATA, 0, 1 ) or die "Can't fseek(DATA,0,1): $!"; print DATA "echo on\n"; for my $key ( keys %curr ) { print DATA "set $key=\n"; } for my $key ( keys %user ) { print DATA qq{set $key=$user{$key}\n}; } truncate DATA, tell(DATA); } __END__ :endofperl

    - tye        

Re: vienv - Edit local environment variables on Win32
by ambrus (Abbot) on Oct 11, 2010 at 12:05 UTC

    I wonder, wouldn't a short batch file with something like this work?

    @echo off set > %temp%\env.bat perl -i.bak -wpe "$_ = q/set / . $_" %temp%\env.bat notepad %temp%\env.bat call %temp%\env.bat

      Why not set | perl -pe "s/^/set /" > %temp%\env.bat ?

      Of course, you lose the confirmation feedback on what actually changed and the intuitive "delete the line to delete the environment variable" behavior and have higher risk of accidentally running other commands.

      - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://265386]
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2014-09-19 06:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (131 votes), past polls