Here is a stripped-down example BATCH file:

@echo off echo starting setlocal cd c:\tmp set A=B perl -e "$ENV{X}='Y'; system('cmd')" echo finished
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.

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:

c:\tmp>^C finished c:\>
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:\> c:\tmp> c:\> c:\tmp>
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.

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.

-- 
Ronald Fischer <ynnor@mm.st>

In reply to Interaction of Windows Batch files and Perl's system() function by rovf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.