Here is a stripped-down example BATCH file:
I run this script (on Windows 7) with working directory set in c:\. The script opens, as expected, a new command shell with working directory set to c:\tmp and the environment set up as defined in my script.@echo off echo starting setlocal cd c:\tmp set A=B perl -e "$ENV{X}='Y'; system('cmd')" echo finished
As a next step in this experiment, I type Control-C within this shell (instead of ending it "properly" by EXIT). When I do this, I observe the following strange behaviour:
First, I'm asked Terminate batch job (Y/N)?. Answering this with Y, I get the error message 'Y' is not recognized as an internal or external command, operable program or batch file. I get, however, a shell prompt, the current directory shown as c:\tmp. Let's say I type Control-C again. Now I see:
My prompt shows that my working directory is back to C:\ However, this shell shows a funny behaviour: If I repeatedly hit ENTER in this shell, I get the following output:c:\tmp>^C finished c:\>
Further experiments on this prompts indeed show that I have two shells running, alternately getting my input: One with working directory set to C:\, the other one to C:\tmp.c:\> c:\tmp> c:\> c:\tmp>
At this point, I didn't think of this riddle as a Perl question yet, and thought it might be due to some weird behaviour of Windows CMD.EXE and its way to handle Batch scripts; but when I replace in my batch script the line perl -e "$ENV{X}='Y'; system('cmd')" by its equivalent in Ruby, i.e. ruby -e "ENV['X']='Y'; system('cmd')", I get a completely different behaviour: Hitting Control-C withing the sub-commandshell doesn't have any effect (I just stay in the shell, and am required to type EXIT in order to leave). However, even the Ruby version is not without weirdness: After leaving the shell, I'm back on C:\ without seeing the message "finished" being output...
Perl and Ruby seem to differ in their behaviour when running an external program using system, so I don't think anymore that this is a "pure" Windows question; it has at least some Perl-related part in it; that's why I thought it is OK to post it here.
Any idea what had happened here? Could someone having Windows 7 try out my example batch script with his/her Perl version? BTW, I was running it using Perl 5.8.8, since this is the version which is used in this project.
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |