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 #### # /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