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 ?
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)
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
what perhaps makes for an even easier decision to use it for both.
Even more, given the
dirname
description from File::Basename (original emphasis)I would rather go even with
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.