According to the manual, chdir, Changes the working directory to EXPR
, if possible.
This script, when executed from cmd.exe:
my $path = 'C:\\some\\path\\';
print "$path\n";
chdir("$path") or die "fail $!";
results in this output:
C:\some\path\
but when I am returned to the command prompt - I'm still at the original directory. Am I misunderstanding the purpose of chdir?
See the FAQ I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible?
In the strictest sense, it can't be done—the script executes as a different process from the shell it was started from. Changes to a process are not reflected in its parent—only in any children created after the change.
The same answer applies to Windows as well.
You can modify the starting directory of subsequent cmd.exe
invocations, or of child processes by messing with shortcuts and/or the registry.
When a shell runs a program, it essentially forks then execs the program -- in this case, your perl script. The directory within that forked process has been changed, and then that process dies. You're then returned to the original shell process.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With