Saturday, July 2, 2011

ENV in perl to view the environmental variables set


The program to view the environmental variables set:
foreach $key (keys(%ENV)) {
    printf("%-10.10s: $ENV{$key}\n", $key);
}

ouput:
HOMEPATH  : \Documents and Settings\acer
PATH      : C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Pr
iles\QuickTime\QTSystem\;C:\strawberry\c\bin;C:\strawberry\perl\site\bin;
wberry\perl\bin
USERDOMAIN: ACER-5D58E53AB3
TERM      : dumb
PROCESSOR_: x86
QTJAVA    : C:\Program Files\Java\jre1.6.0_02\lib\ext\QTJava.zip
TEMP      : C:\DOCUME~1\acer\LOCALS~1\Temp
PROCESSOR_: 2502
SYSTEMDRIV: C:
SYSTEMROOT: C:\WINDOWS
COMSPEC   : C:\WINDOWS\system32\cmd.exe
LOGONSERVE: \\ACER-5D58E53AB3
SESSIONNAM: Console
FTP_PASSIV: 1
WINDIR    : C:\WINDOWS
PROCESSOR_: 6
USERNAME  : acer
PROCESSOR_: x86 Family 6 Model 37 Stepping 2, GenuineIntel
ALLUSERSPR: C:\Documents and Settings\All Users
COMPUTERNA: ACER-5D58E53AB3
CLASSPATH : .;C:\Program Files\Java\jre1.6.0_02\lib\ext\QTJava.zip
TMP       : C:\DOCUME~1\acer\LOCALS~1\Temp

ENV in perl to view the environmental variables set


The program to view the environmental variables set:
foreach $key (keys(%ENV)) {
    printf("%-10.10s: $ENV{$key}\n", $key);
}

ouput:

HOMEPATH  : \Documents and Settings\acer
PATH      : C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Pr
iles\QuickTime\QTSystem\;C:\strawberry\c\bin;C:\strawberry\perl\site\bin;
wberry\perl\bin
USERDOMAIN: ACER-5D58E53AB3
TERM      : dumb
PROCESSOR_: x86
QTJAVA    : C:\Program Files\Java\jre1.6.0_02\lib\ext\QTJava.zip
TEMP      : C:\DOCUME~1\acer\LOCALS~1\Temp
PROCESSOR_: 2502
SYSTEMDRIV: C:
SYSTEMROOT: C:\WINDOWS
COMSPEC   : C:\WINDOWS\system32\cmd.exe
LOGONSERVE: \\ACER-5D58E53AB3
SESSIONNAM: Console
FTP_PASSIV: 1
WINDIR    : C:\WINDOWS
PROCESSOR_: 6
USERNAME  : acer
PROCESSOR_: x86 Family 6 Model 37 Stepping 2, GenuineIntel
ALLUSERSPR: C:\Documents and Settings\All Users
COMPUTERNA: ACER-5D58E53AB3
CLASSPATH : .;C:\Program Files\Java\jre1.6.0_02\lib\ext\QTJava.zip
TMP       : C:\DOCUME~1\acer\LOCALS~1\Temp


ENV in perl to add path


ENV is a hash in perl.
You can access as it a simple hash
For instance to add a path in the environment variable PATH in the OS execute the following program:

env_path.pl:
$ENV{'PATH'} = "G:\\perl_programs;"."$ENV{'PATH'}";
print "$ENV{'PATH'}";


output:
G:\perl_programs>perl env_path.pl
G:\perl_programs;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Prog
ram Files\QuickTime\QTSystem\;C:\strawberry\c\bin;C:\strawberry\perl\site\bin;C:
\strawberry\perl\bin
G:\perl_programs>

This added path will go off once the perl program is executed.It will be available only to the child process