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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: vienv - Edit local environment variables on Win32 (fixed)
by tye (Sage) on Oct 10, 2010 at 23:32 UTC | |
Re: vienv - Edit local environment variables on Win32
by ambrus (Abbot) on Oct 11, 2010 at 12:05 UTC | |
by tye (Sage) on Oct 11, 2010 at 14:07 UTC |
Back to
Cool Uses for Perl