Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing to a directory and then getcwd()

Many of my colleagues use the following commands in their BEGIN block.

$scriptDir = dirname($0);
chdir($scriptDir);
$scriptDir =  getcwd();

I have looked around and can't help but think that the third line i.e. $scriptDir = getcwd(); is redundant. Because we already have the scriptdir from $scriptDir = dirname($0); .Am I missing something here ?

like image 361
tarunkt Avatar asked Dec 23 '16 06:12

tarunkt


2 Answers

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.

like image 120
zdim Avatar answered Nov 13 '22 08:11

zdim


  • 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
like image 25
Chankey Pathak Avatar answered Nov 13 '22 08:11

Chankey Pathak