Changing the Perl directory from Windows cmd.exe

According to the chdir manual, if possible, change the working directory to EXPR .

This script, when executed from cmd.exe:

 my $path = 'C:\\some\\path\\'; print "$path\n"; chdir("$path") or die "fail $!"; 

leads to this result:

 C:\some\path\ 

but when I return to the command line, I am still in the source directory. I do not understand the purpose of chdir?

+4
source share
3 answers

When the shell launches the program, it essentially plugs in, then runs the program - in this case, your perl script. The directory in this forked process has been changed, and then this process dies. Then you return to the original shell process.

+6
source

See the FAQ I {directory changed, changed my environment} in a perl script. Why did the change disappear when I exited the script? How will my changes be visible?

In the strict sense, this cannot be done. The script is executed as another process from the shell with which it was launched. Changes in the process are not reflected in its parental view, only in any children created after the change.

The same answer applies to Windows.

You can change the start directory of subsequent calls to cmd.exe or child processes to include shortcuts and / or registry.

+7
source

I have changes to directories and command environments using the perl -x switch to execute Perl code embedded in a file.

 @rem = '--*-Perl-*--' @echo off set TMPBAT=%TMP%\%0_temp.bat perl -x -S %0 %* if %errorlevel% == 2000 goto cleanup goto endofperl #!perl #line 9 use strict; use warnings; use BatchTool; __END__ :endofperl if exist %TMPBAT% call %TMPBAT% :cleanup set TMPBAT= 

BatchTool is a module that writes DOS commands to $ENV{TMPBAT} if 1) it does not exist or 2) is older than the script source.

+1
source

Source: https://habr.com/ru/post/1303509/


All Articles