start page | rating of books | rating of authors | reviews | copyrights

Book Home Programming PerlSearch this book

19.2. 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:

$ PATH='/bin:/usr/bin' perl myproggie
You can do something similar with a subshell in csh and tcsh:
% (setenv 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'
And under sh, ksh, and bash you'd say:
$ PATH='/bin:/usr/bin'; export PATH
Other systems will have other ways of setting these on a semi-permanent basis. Here are the environment variables Perl pays attention to:

HOME

Used if chdir is 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 chdir has no argument, but HOME is not set.

PATH

Used in executing subprocesses, and for finding the program if the -S switch 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 PERL5LIB is not defined, PERLLIB is consulted for backward compatibility with older releases.

When running taint checks (either because the program was running setuid or setgid, or the -T switch was used), neither of these library variables is used. Such programs must employ the use lib pragma 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 -T switch was used), this variable is ignored. If PERL5OPT begins 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:

BEGIN { require 'perl5db.pl' }
See Chapter 20, "The Perl Debugger" for more uses of this variable.

PERL5SHELL (Microsoft ports only)

May be set to an alternative shell that Perl must use internally for executing commands via backticks or system. Default is cmd.exe /x/c on WinNT and command.com /c on 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 COMSPEC for this purpose because COMSPEC has 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 setting COMSPEC to such a shell may interfere with the proper functioning of other programs (which usually look in COMSPEC to 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 PERL5LIB is defined, PERLLIB is not used.

PERL_DEBUG_MSTATS

Relevant only if Perl is compiled with the malloc function included with the Perl distribution (that is, if perl -V:d_mymalloc yields "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 Chapter 23, "Security" for details.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.