Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl directory change from Windows cmd.exe

Tags:

cmd

perl

chdir

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?

like image 612
dls Avatar asked Dec 10 '22 17:12

dls


2 Answers

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.

like image 160
Sinan Ünür Avatar answered Jan 05 '23 21:01

Sinan Ünür


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.

like image 20
Conspicuous Compiler Avatar answered Jan 05 '23 22:01

Conspicuous Compiler