file-io beginner

How to change the current directory in Perl?

Question

How to change the current directory in Perl?

Changing the Current Directory in Perl

In Perl, you change the current working directory using the built-in chdir function. It works similarly to the shell cd command, changing your process’s directory to the one specified. The function returns true on success and false on failure, so it’s important to check the return value and handle errors.

The argument to chdir should be a directory path (relative or absolute). A common mistake is to pass an undefined or incorrect path, which causes an error. Additionally, because chdir directly affects the process’s current working directory, many scripts use the cwd function from Perl’s core Cwd module to display the current directory before and after changing it.

Perl Concepts

  • chdir changes the working directory of the running Perl script.
  • $! holds the error message from the last system call, helpful for troubleshooting failures.
  • Scalar variables use the $ sigil in Perl.
  • Always check chdir's return value to avoid silent failures.

Runnable Example: Change to a Valid Directory

This example attempts to change directory to /tmp (a common existing directory on Unix systems). It prints the directory before and after chdir. You can modify $dir if you are on Windows or another system.

use strict;
use warnings;
use Cwd;

# Use a directory that typically exists - adjust if needed:
my $dir = "/tmp";

print "Current directory before chdir: ", cwd(), "\n";

if (chdir $dir) {
    print "Changed directory to: ", cwd(), "\n";
} else {
    die "Could not change directory to '$dir': $!";
}

How to Run

Save the above script or run it directly with perl -E '...' . Make sure the directory you specify in $dir actually exists on your system, or the script will die with an error.

Common Pitfalls

  • Invalid directory: If the path given to chdir doesn’t exist, you get an error — always verify or handle it.
  • Relative vs absolute paths: Relative paths are resolved based on the current directory.
  • Permission issues: Even if the directory exists, permissions might prevent changing into it.
  • Platform differences: Use forward slashes / on all platforms if possible, or Windows style backslashes carefully.

Additional Tips

  • The Cwd core module is very handy to get and verify the current working directory.
  • Perl’s error variable $! always contains the last OS error message, useful inside die or warn.
  • Testing with standard directories like /tmp or C:\ (Windows) helps avoid unexpected failures.

In summary, chdir is the straightforward way to change directories in Perl scripts — just ensure the directory exists, check the return value, and handle errors gracefully.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 13ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Current directory before chdir: /Volumes/SSD/skills/server-ops/vps/107.174.42.198/Standalone-Apps/perlcode
Changed directory to: /private/tmp
STDERR
(empty)

Was this helpful?

Related Questions