Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Interaction of Windows Batch files and Perl's system() function

by Anonymous Monk
on Sep 07, 2011 at 12:00 UTC ( [id://924589]=note: print w/replies, xml ) Need Help??


in reply to Interaction of Windows Batch files and Perl's system() function

Windows7 won't change anything :)

What you have here, is cmd being cmd, the cmd way , and perl being perl

cmd.exe is a special kind of program

Instead of striking out the incorrect parts of my investigation below, everything is in readmore

Read $ help call

Install procexp.exe from http://live.sysinternals.com/ and start it from your experiment shell, and then watch the tree of processes after each action (like Ctrl+C)

After launching the batch file, i see the cmd that launched the batch, its child, a perl process, with a child of cmd.exe

On 1st ctrl+C, the perl process ends, as does the cmd.exe process, they're both gone from procexp/taskmanager

But cmd being so special, you have to exit really end the subshell , its the only way

The perl process has ended, the cmd it launched is still around, but it is nobodys child

When you launch a cmd from a cmd from a cmd, each subsequent cmd is a child of the former, and Ctrl+C doesn't kill it, or make it an orphan

You'll notice, when you hit ctrl+c, you get double newlines, thats because, apparently, ctrl+c is being propagated to both cmds

You'll also notice at first it won't take your y/n, but if you try it a second time, it will

Terminating on signal SIGINT(2) $ Terminate batch job (Y/N)? y 'y' is not recognized as an internal or external command, operable program or batch file. $ y $ exit
See, the thing is, all 3 things share the same STDIN/STDERR/STDOUT among other things

See DLL_PROCESS_ATTACH

I don't have a complete grasp of everything, but yes, there is a bug in there somewhere, probably in the perl dll hook, not detaching cleanly, maybe

You might also be able to glean some leads from these frontends/wrappers for cmd.exe, eConsole and console2

On the other hand, perl on win32 has behaved this way for a long time (11+ years at least)

So I can't help but think, the porters, and ActiveState, left it this way for a reason -- either its unfixable, or its the best compromise available ...

What you want to do is use $SIG{INT}='IGNORE'; so that pressing Ctrl+C doesn't end the shell launched by perl, force the user to type exit, like
call perl -le " $SIG{INT}=q[IGNORE]; $ENV{X}='Y'; $ret = system('cmd') +; print qq[system returned $ret ]; "

But that still leaves the problem of being prompted by the original shell running the batch file

$ exit system returned 0 Terminate batch job (Y/N)? y
$ exit system returned 0 Terminate batch job (Y/N)? n finished

I'm not sure how to signal to it, that everything is ok , no need to prompt, the answer probably lies in that dllhook business :/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://924589]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-29 10:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found