-5

I'm using AS perl on win7.

print `cd \\\\ `; # does nothing, says nothing

Same with qx()

print `dir \\\\ `;  # correctly prints the root directory

other commands also seem to work fine.

cd works fine from the command line of a batch file.

Has anyone else seen this? Is there a workaround?

TLP
  • 66,756
  • 10
  • 92
  • 149
user1067305
  • 3,233
  • 7
  • 24
  • 29
  • 2
    What do you expect "change directory" to return and hence to be printed? Are you asking the wrong question, are you expecting the working directory of the Perl program to be changed? Please be more specific about the problem and what you want to achieve. – AdrianHHH Mar 14 '15 at 17:55
  • I'm expecting that when the program exits, the prompt will show that it is in the new directory. It isn't! Subsequent commands using files in that directory fail. Kapish? – user1067305 Mar 14 '15 at 21:38
  • Also, the print returns any error messages. There were none in this case. – user1067305 Mar 14 '15 at 21:46
  • 2
    ***Kapish?*** Are you serious?! You are so blinded by a lack of understanding. Yes, we do ***capisce***! That's why we are trying to explain to you that the `` cd \\ `` is going to succeed in the subshell, but will not do anything visible to your Perl program. Subsequent `` somecmd `` invocations will start independent subshells in which the current directory is still unchanged. That's why you need to `chdir` in your Perl script. – Sinan Ünür Mar 14 '15 at 22:48
  • Though I already tried chdir, and it didn't work, your confidence inspired me to try it again. It still didn't work, with no error message printed. When I gave it an illegal address, an error message was printed. – user1067305 Mar 15 '15 at 04:49

2 Answers2

7

You may be looking for chdir. Using a shell command in backticks is not going to have a lasting effect. When you run a backtick command, you spawn a new shell, execute the command, and return the standard output to Perl. Then the shell exits and any and all changes to it is lost.

TLP
  • 66,756
  • 10
  • 92
  • 149
  • chdir does exactly the same thing, which is not surprising, since it is a synonym. – user1067305 Mar 14 '15 at 21:39
  • 5
    @user1067305 Don't confuse the [Windows command](https://technet.microsoft.com/en-us/library/bb490875.aspx) `chdir` with the [Perl function](http://perldoc.perl.org/functions/chdir.html) `chdir`. The former will only change the working directory of the shell it is invoked in; the latter will change the working directory of your Perl script. – ThisSuitIsBlackNot Mar 14 '15 at 21:57
  • AHA! That's the answer!! I WAS using chdir as a windows command in the backticks. Using it as the perl command, followed by a dir in backticks, gave me the dir of the targeted folder. Thank you all! – user1067305 Mar 15 '15 at 05:13
  • 1
    @user1067305 it's great you got your program working, but do you understand why cd in backticks did not do what you expected? It really is worth taking the time to understand what it was doing and why it wasn't what you wanted. For similar reasons, attempting to change environment variables like %PATH using backticks will also appear to fail. – Grant McLean Mar 16 '15 at 00:08
  • Yes, thank you. I believe I do. The confusion here arose because I was translating a simple script from a batch file into perl, and some of the commands worked and some didn't. You guys explained why. The other confusion was that people said "use chdir", but there are TWO chdirs, the windows one and the perl one, and that's where you set me straight. – user1067305 Mar 17 '15 at 02:55
1

perldoc -q changed

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.

Community
  • 1
  • 1
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • The command is part of a script which executes many other commands, all of which work. Commands in the same script which follow the cd fail because the directory has not changed. – user1067305 Mar 14 '15 at 21:40