8.3.3 The Environment Hash: %ENV
8.3.3 The Environment Hash: %ENV
On any system, Perl has available to it a hash by the name %ENV. This hash has details of the environment in which Perl is working. Any operating system, whether Unix, Macintosh or Windows-based, has a number of variables called environment variables. Usually, the operating system needs them to be set so that so that various programs run on the system correctly. These refer to details such as the operating system used, the name of the machine, the user who is logged in, the nature of the display terminal used, etc. There are a host of other system-specific and session-specific details as well. The number, the names and the values
of the environment variables are different on different machines.
The names of the environment variables and their corresponding values are acquired by Perl from the operating system as well as the Apache Web server. The Perl software may create some environment variables as well. Since the environment variables are inherited by Perl from the operating system and stored as key-value pairs, the keys and their corresponding values vary from machine to machine. Even on the same machine, the keys and values are different depending on whether a Perl script is run from the command line or from the Web as a CGI program.
The following program is run on the command line and prints the keys and corresponding values in the environment variable %ENV. It is not run as a CGI program.
Program 8.4
#!/usr/bin/perl
#environCommand.pl
use strict;
my $key;
foreach $key (sort (keys %ENV)){
print "The value of the ", $key, " field is ", $ENV{$key}, "\n";
}
The program iterates over every key-value pair in %ENV hash and prints the pair. The output this program prints on the screen when run from the command-line is shown below. Some lines have been broken up because they are too long.
The value of the AUTOBOOT field is YES
The value of the BOOT_FILE field is /boot/vmlinuz-2.2.19-7.0.1
The value of the BOOT_IMAGE field is linux
The value of the COLORTERM field is gnome-terminal
The value of the CONSOLE field is /dev/console
The value of the DISPLAY field is :0
The value of the EDITOR field is /usr/ucb/vi
The value of the GDMSESSION field is Default
The value of the GDM_LANG field is en_US
The value of the GROUP field is kalita
The value of the HOME field is /home/kalita
The value of the HOST field is pikespeak.uccs.edu
The value of the HOSTNAME field is pikespeak.uccs.edu
The value of the HOSTTYPE field is i386-linux
The value of the INIT_VERSION field is sysvinit-2.78
The value of the KDEDIR field is /usr
The value of the LANG field is en_US
The value of the LESSOPEN field is |/usr/bin/lesspipe.sh %s
The value of the LOGNAME field is kalita
The value of the LS_COLORS field is no=00:fi=00:di=01;34:ln=01;36:pi=40;
33:so=01;35:bd=40;33;01:cd=40;33;
01:or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;
32:*.btm=01;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;
31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;
31:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;
35:*.bmp=01;35:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35:
The value of the MACHTYPE field is i386
The value of the MANPATH field is /usr/man:/usr/local/man:/usr/users/kalita/man:/usr/share/man
The value of the OSTYPE field is linux
The value of the PATH field is
.:/home/kalita/bin:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/usr/bin/X11:/usr/bin/mh
The value of the PREVLEVEL field is N
The value of the PWD field is /home/kalita/perl/cgi-bin
The value of the QTDIR field is /usr/lib/qt-2.2.0
The value of the RUNLEVEL field is 5
The value of the SESSION_MANAGER field is local/pikespeak.uccs.edu:/tmp/.ICE-unix/1179
The value of the SHELL field is /bin/csh
The value of the SHLVL field is 2
The value of the SSH_ASKPASS field is /usr/libexec/openssh/gnome-ssh-askpass
The value of the TERM field is xterm
The value of the USER field is kalita
The value of the USERNAME field is kalita
The value of the VENDOR field is intel
The value of the VISUAL field is /usr/bin/emacs
The value of the WINDOWID field is 92274812
The value of the WWW_HOME field is http://www.cs.uccs.edu/~kalita
The value of the XAUTHORITY field is /home/kalita/.Xauthority
The value of the _ field is /usr/bin/gnome-terminal
We do not have to understand every key and its value. The purpose of showing the output is to make the reader aware of the variety of environment variables and their values that an operating system maintains. We clearly see that many of the environment variables represent information that is static or permanent. An example is the VENDOR field whose value is intel, indicating it is an Intel machine on which the Perl script is running. However, the field WINDOWID is very specific and temporary giving an integer with the ID
number of a window where the script just ran.
