I had to get rid of absolute paths, especially in production environment. I have to be sure, that my perl scripts have current working directory set to the their own directory.
package cwd;
use Carp;
use File::Basename;
use Cwd qw(realpath getcwd chdir);
my $current;
BEGIN {
$current = getcwd;
my $basename = basename $0;
my $realpath = realpath $0;
$realpath =~ s/$basename$//;
chdir $realpath
or croak "cannot chdir: $!\n";
}
END {
chdir $current
or croak "cannot chdir: $!\n";
}
1
Now I can invoke callee program, which will execute in its own directory:
# /tmp/caller/CALLER
# /tmp/callee/CALLEE
# CALLEE content:
use strict;
use cwd;
use Cwd;
map{ print $_,$/ }glob'*';
print "\ncallee dir:", getcwd, "\n";
# CALLER content:
use strict;
use Cwd;
print qx|/tmp/callee/CALLEE|;
print "\ncaller dir:", getcwd, "\n";
# output (from `cd /tmp/caller; ./CALLER`):
CALLEE
callee dir:/tmp/callee
caller dir:/tmp/caller