Programming Perl

Programming PerlSearch this book
Previous: 3.2.10 callerChapter 3
Functions
Next: 3.2.12 chmod
 

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.


Previous: 3.2.10 callerProgramming PerlNext: 3.2.12 chmod
3.2.10 callerBook Index3.2.12 chmod