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)
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 usefileparse()
.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With