[Chapter 3] 3.2.11 chdir
Author: Larry Wall, Tom Christiansen & Randal L. Schwartz
Date: 1999-07-02T01:47:52Z

#

| Chapter 3 Functions |

### 3.2.11 chdir

    chdir EXPR

This function changes the working directory to `EXPR`, if possible. If `EXPR` is omitted, it changes to the home directory. The function returns 1 upon success, 0 otherwise (and puts the error code into $!).

    chdir "$prefix/lib" or die "Can't cd to $prefix/lib: $!\n";

The following code can be used to move to the user's home directory, one way or another:

    $ok = chdir($ENV{"HOME"} || $ENV{"LOGDIR"} || (getpwuid($<))[7]);

Alternately, taking advantage of the default, you could say this:

    $ok = chdir() || chdir((getpwuid($<))[7]);

See also the Cwd module, described in Chapter 7, The Standard Perl Library, which lets you keep track of your current directory.

3.2.10 caller |  | 3.2.12 chmod