It might be worth pointing out why using cd
in backticks didn't work.
Running a command in backticks starts up a completely new shell environment for the command. That new environment starts with a copy of all of the environment variables from the environment that your program is running in. The current directory is one of those environment variables (it's in $ENV{PWD}
).
You new environment starts up. The first (and only) thing that it does is to change directory. So the value of $ENV{PWD}
in the new environment is changed. But the value in your original environment stays the same as it was.
Your new environment then closes down as its job is done. All of the environment variables that it has are removed from memory. And control returns to the original environment. Which still has the original value for the current directory.
A child environment cannot change the environment variables in its parent environment. So any attempt to change directory using an external program is doomed to failure.
But changing directory using Perl's built-in function chdir
works just fine. Because that changes the value in the current environment.
Hope that's helpful.