Environment Variables
In addition to the various switches that explicitly modify Perl's behavior, you can set various environment variables to influence various underlying behaviors. How you set up these environment variables is system dependent, but one trick you should know if you use sh, ksh, or bash is that you can temporarily set an environment variable for a single command, as if it were a funny kind of switch. It has to be set in front of the command:
You can do something similar with a subshell in csh and tcsh:$PATH="/bin:/usr/bin" perl myproggie
Otherwise, you'd typically set environment variables in some file with a name resembling chsrc or profile in your home directory. Under csh and tcsh you'd say:%(setenv PATH "/bin:/usr/bin"; perl myproggie)
And under sh, ksh, and bash you'd say:%setenv PATH '/bin:/usr/bin'
Other systems will have other ways of setting these on a semi-permanent basis. Here are the environment variables Perl pays attention to:$PATH="/bin:/usr/bin"; export PATH
HOME- Used if
chdiris called without an argument. LC_ALL,LC_CTYPE,LC_COLLATE,LC_NUMERIC,PERL_BADLANG- Environment variables that control how Perl handles data specific to particular natural languages. See the online docs for perllocale.
LOGDIR- Used if
chdirhas no argument, butHOMEis not set. PATH- Used in executing subprocesses, and for finding the program if the
-Sswitch is used. PERL5LIB- A colon-separated list of directories in which to look for Perl library files before looking in the standard library and the current directory. Any architecture-specific directories under the specified locations are automatically included if they exist. If
PERL5LIBis not defined,PERLLIBis consulted for backward compatibility with older releases.When running taint checks (either because the program was running setuid or setgid, or the
-Tswitch was used), neither of these library variables is used. Such programs must employ theuse libpragma for that purpose. PERL5OPT- Default command-line switches. Switches in this variable are taken as if they were on every Perl command line. Only the
-[DIMUdmw]switches are allowed. When running taint checks (because the program was running setuid or setgid, or the-Tswitch was used), this variable is ignored. IfPERL5OPTbegins with-T, tainting will be enabled, causing any subsequent options to be ignored. PERL5DB- The command used to load the debugger code. The default is:
See "The Perl Debugger" for more uses of this variable.BEGIN { require 'perl5db.pl' } PERL5SHELL(Microsoft ports only)- May be set to an alternative shell that Perl must use internally for executing commands via backticks or
system. Default iscmd.exe /x/con WinNT andcommand.com /con Win95. The value is considered to be space separated. Precede any character that needs to be protected (like a space or backslash) with a backslash.Note that Perl doesn't use
COMSPECfor this purpose becauseCOMSPEChas a high degree of variability among users, leading to portability concerns. Besides, Perl can use a shell that may not be fit for interactive use, and settingCOMSPECto such a shell may interfere with the proper functioning of other programs (which usually look inCOMSPECto find a shell fit for interactive use). PERLLIB- A colon-separated list of directories in which to look for Perl library files before looking in the standard library and the current directory. If
PERL5LIBis defined,PERLLIBis not used. PERL_DEBUG_MSTATS- Relevant only if Perl is compiled with the
mallocfunction included with the Perl distribution (that is, ifperl -V:d_mymallocyields "define"). If set, this causes memory statistics to be displayed after execution. If set to an integer greater than one, also causes memory statistics to be displayed after compilation. PERL_DESTRUCT_LEVEL- Relevant only if your Perl executable was built with debugging enabled, this controls the behavior of global destruction of objects and other references.
Apart from these, Perl itself uses no other environment variables, except to make them available to the program being executed and to any child processes that program launches. Some modules, standard or otherwise, may care about other environment variables. For example, the use re pragma uses PERL_RE_TC and PERL_RE_COLORS, the Cwd module uses PWD, and the CGI module uses the many environment variables set by your HTTP daemon (that is, your web server) to pass information to the CGI script.
Programs running setuid would do well to execute the following lines before doing anything else, just to keep people honest:
$ENV{PATH} = '/bin:/usr/bin'; # or whatever you need $ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
See "Security" for details.