in reply to vienv - Edit local environment variables on Win32
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
|
|---|