http://www.perlmonks.org?node_id=326717


in reply to unable to change remote directory

On UNIX systems, all processes are nicely separated from each other. Changes made in one process do not affect any other (without help of the kernel)

Only when a new process starts, the child get some info, for instance the current directory, the user id and environment variables. But from then on, the child is a different process.

Well, when you call system() or qx() --backticks, you start shell which handles your cd. That shell is done by a child process, which itself may change the directory nicely, but, by reason of full process separation, will not affect the current location of its parent.

For the same reason, 'cd' is a shell built-in: the shell cannot let some other process do it's job. The same for ftp: you cannot do !cd, but need lcd to change the local working directory. There is no /bin/cd !

So: `cd $a` or qx(cd $a) is not working because a sub-shell moves. system("cd", $d) doesn't use a shell, but cannot work because there is no /bin/cd.

Finally, chdir is the Perl built-in, which should work given that the directory exists. But of course, you may not be permitted to access the directory you want to go to. Therefore, try

use Cwd; print "From: ",getcwd(), "\n"; chdir $d or die "To $d: $!\n";