I have an issue with using abs_path(). I give this function a valid path, but I get undef as a return value. The path is read from the command line using the following code.
use warnings;
use strict;
use Data::Dumper;
use Cwd qw();
print scalar @ARGV;
print "\n";
print "$ARGV[0]\n";
print "$ARGV[1]\n";
print "$ARGV[2]\n";
my $val = $ARGV[2];
print "$val\n";
my $result = "";
$result = Cwd::abs_path($val);
print "$result\n";
The perl script is executed as follows:
perl pl_test.pl -results_dir = /home/user123/
I get the following result:
3
-results_dir
=
/home/user123/
/home/user123/
Use of uninitialized value in concatenation (.) or string at pl_test.pl line 16.
I have verified using the debugger that the issue is that $result gets undef as return value from Cwd::abs_path($val);. I don't understand why this happens
One of the comments prompted me to do some further testing. What I found is that there is a dependency on a trailing slash, in the following manner:
if the directory does not exist: works without a trailing slash, returns
undefwith a trailing slash.if the directory is a sub directory of a directory that does not exist, always returns
undefif the directory exists, will work in both cases, with or without a trailing slash