Changing to a directory and then getcwd()

The dirname($0) does not return the full path, as Chankey Pathak and Matthias demonstrate.

I'd like to add that there are other ways. For example, you can use FindBin (also core)

use FindBin qw($RealBin);

BEGIN: {
    my ($scriptDir) = $RealBin;
    chdir $scriptDir             or die "Can't change to $scriptDir: $!";
};

The $RealBin gives the same as what you show, except that it is the full path with links resolved.

The chdir may fail, thanks to ikegami for the comment. In that case false is returned and the code above dies, adjust as suitable. Note that the questioned third line has nothing to do with this.

This module is also commonly used for relative path to libraries with lib pragma, for example

use lib "$RealBin/../lib";

what perhaps makes for an even easier decision to use it for both.


Even more, given the dirname description from File::Basename (original emphasis)

This function is provided for compatibility with the Unix shell command dirname(1) and has inherited some of its quirks. In spite of its name it does NOT always return the directory name as you might expect. To be safe, if you want the directory name of a path use fileparse().

I would rather go even with

use Cwd qw(abs_path);

BEGIN: {
    my ($scriptDir) = abs_path(__FILE__) =~ m|(.*)/|;
    chdir $scriptDir             
        or die "Can't change to $scriptDir: $!";
};

where abs_path is used since __FILE__ on its own may not provide the full path. The regex greedily scoops everything up to the very last /, that is, the full path to the directory of the script.


  • getcwd - Returns the current working directory
  • chdir - Changes the working directory to EXPR

Below example explains it well.

#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
use Cwd;

my $scriptDir = dirname($0);
chdir($scriptDir);
print "First: $scriptDir\n";
$scriptDir =  getcwd();
print "Second: $scriptDir\n";

Output:

chankeypathak@stackoverflow:~/Desktop$ perl test.pl 
First: .
Second: /home/chankeypathak/Desktop