HamNRye has asked for the wisdom of the Perl Monks concerning the following question:

This is driving me nuts.

Here's the problem, I'm writing a TK program, and everytime I do a  $_ = `rsh system dir' I get a console window pop-up. The program works, but I hate the entire 6 pop-up console windows every minute.

I know that I have seen this problem mentioned in the Active State documentation, but I can't find it.

Replies are listed 'Best First'.
Re: Perl TK on Win32
by Chmrr (Vicar) on May 11, 2001 at 03:10 UTC

    Windows' command line leaves a lot to be desired. Anyways, that being said, your best bet is probably something like the following:

    • Fire up the process with Win32::Process, as noted elsewhere. Make the process redirect its otuput to a temporary file (one of the few things that Windows' command prompt can do)
    • Use $process->wait() (possibly with a timeout and interspersed with $main->update; if it's a long task)
    • Open the temp file and read away.
    Hope this helps.

     
    perl -e 'print "I love $^X$\"$]!$/"#$&V"+@( NO CARRIER'

      I know from my TCL/TK days that this type of solution can destroy a decent program. Just the resulting disk I/O does not make this worthwhile. (I tried this once, even with a ramdisk and my program took longer by a magnitude of 4.) At the very least I would look at working with a MySQL database and store the info in there for better retrieval. By the same token, I could run the GUI separate from the rest of the program and then run the daemon as a console application that outputs text files to be read by the GUI.

      Or even:  `start /m rsh $server dir > ${server}.txt` Also, these windows go away in a flash, but each iteration of the mainloop calls 6 rsh'es for 6 different servers, and you get this obnoxious flash of console windows. And as we add more servers to check, this could get really ugly. So if it comes down to needing to somehow run the program from the console, WTF am I fartin' about with Tk?

      I'm starting to get the impression that this "cannot be done" and I also feel that I am about to get the "It's Window's fault" post. But this is from TCL/TK code that worked (with a few signal trapping incidents), and had none of the console problems. Last night I ported it over to VB and ran it without too many problems. (I suck at VB)

      Before you warm my toes with flames, I don't mean this to say that one is better, and I know comparing the different langs is like comparing apples and oranges. The point I am making is that if there is not a mechanism to achieve this in Perl, is there a wish list? It seems that this is something that I have seen alot of people struggle with, and nobody has a solution.

      EDITED Also, I tried using the Win32::Process to write out to tempfiles, and this only suppreses the rsh Windows if the perl script is run this way. (In essence starting it from a console.) Again this begs the question, why am I using TK?

      I'll keep working on this, and if I can find a way, I'll make a module out of it. But this is something that I think should be a core feature. use Tk::Exec; Tkexec("rsh $server dir", $dir); or something like that.

      And to respond to a later post, I used the wperl.exe, same result.

        Or even: `start /m rsh $server dir > ${server}.txt` Also, these windows go away in a flash, but each iteration of the mainloop calls 6 rsh'es for 6 different servers, and you get this obnoxious flash of console windows.

        Did you try this?

        start /b yourprogram

        start /? says:

        B -- Start application without creating a new window. The application has ^C handling ignored. Unless the application enables ^C processing, ^Break is the only way to interrupt the application
        Run this, then remove the /b option and the windows start flashing again.
        for my $i (0..10) { print "\nStarting $i\n"; system( qq{start /b perl -e "print 'program $i'";} ); }

        /J

Re: Perl TK on Win32
by idnopheq (Chaplain) on May 11, 2001 at 02:52 UTC
    Win32::Process may be your best bet, but note issues w/ it actually doing what you describe as identified at the end of TK Chatterbox Client.

    HTH
    --
    idnopheq
    Apply yourself to new problems without preparation, develop confidence in your ability to to meet situations as they arrise.

      Looked at the Chatterbox Client, and I assunme that this is the sub you are talking about:

      ###################################################################### +########## # # closeDOSParent # # Closes the dos prompt that created this process # sub closeDOSParent { # This just simply doesn't work. I'm not sure why. return; return if ($opt_debug); # debugging info goes to STDOUT! if ($^O =~ /MSWin32/i) { my($process); my($program) = $^X; $orig_params =~ s/\-c\S*\b//g; my($pgm) = "perl $0 $orig_params"; eval ' use Win32::Process; Win32::Process::Create($process,"$program","$pgm",0,DETACHED_PRO +CESS,".") || die Win32::FormatMessage(Win32::GetLastError()) . "\n"; '; exit; } }

      I'm not looking to close the DOS window that started my process, but trying to avoid the console windows popping up when doing an "rsh" from the program. They close again immediately, but having 6-7 windows popping into the foreground evey minute is a pain.

      I'll look into the Win32::Process docs and see if I can figure out anything....

        Another thought I just had while waiting for the pizza delivery man was "Can you daemon-ize it?" ... maybe Win32::Daemon is an approach, pass the service instructions, then kill the service when the app closes?

        I have no real idea if this will even work, but my blood sugar is low ...

        HTH
        --
        idnopheq
        Apply yourself to new problems without preparation, develop confidence in your ability to to meet situations as they arrise.

        I'm not looking to close the DOS window that started my process

        All this command does is hide the DOS window that $pgm is spawning (so launch rsh with that type of command). Look around on the site, there have been many other questions WRT the same thing, including my own - I have used this solution and it works.
Re: Perl TK on Win32
by cwsaylor (Acolyte) on May 11, 2001 at 06:01 UTC
    Use wperl.exe to run your program. For ActivePerl, this gets installed into C:\perl\bin by default.

    Chris Saylor

Re: Perl TK on Win32
by JojoLinkyBob (Scribe) on May 11, 2001 at 01:20 UTC
    Did you try:
    $_ = `rsh system dir &' ; ?#to background it
    Desert coder

      I tried that, but Win2k doesn't seem to recognize the "&" notation for running minimized (aka. Background). (What happened to the DOS program options that were in Win 95??)

      I also gave the old start command a try, running "start /m rsh system dir". The problem is that I catch the return code for the start command and not the info from the rsh.