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

I do not think any of these have been answered in the FAQ's so here we go. How would one spawn a new instance of a command shell in Windows from a simple perl script? I know about fork() command but all this does is spawn a nested command prompt in the same window as the already running command prompt used by the parent process.

I want a parent process to launch a child process in it's own command shell window.

Second, has anyone ever used bgstipple successfully? What exactly does it do? Does anyone have an example of a use of bgstipple?

Third, How would I insert graphics into a tk widget? Like a button or textbox background or even a simple window frame? I thought bgstipple was the way but from reading about it it seems that it is for a sort of shading effect (bgstipple that is).

Replies are listed 'Best First'.
Re: A few random questions.
by BrowserUk (Patriarch) on Dec 09, 2003 at 22:09 UTC

    For your first question the simplest way is

    perl -e"system 'start cmd';"

    Depending upon what you want to do with the new command shell, then you may want to follow the "cmd" part with /c or /k and then the command you want to run.

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail

      That works great but I cannot seem to feed the command I want to the newly spawned command prompt. What would the syntax be for that?

      system("perl -e\"system 'start cmd';\" /c perl \"$filename\"");
      I have tried the preceeding code and a few other variations but have been unsuccesfull.

        Based on your example, you want to run another perl script from within your perl program, and have it present its output in a separate cli window, then this maybe what you are looking for.

        my $filename = 'some perl script (potentially with spaces).pl'; system qq[ start cmd /k perl "$filename" ];

        Using qq// for the outer set of quotes removes the need to escape the inner ones needed around the filename whilst still allowing $filename to be interpolated. See Quote-like operators for more info.

        Using /k will ensure that the window remains and the script output is visible after the command has ended.

        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail

        All you need to do to run a command with options is to join them in one long string. eg.
        my $process = join (" ",$execution_path, $options); system ($process);
        However, this won't work with the command prompt as you can't pass further instructions to a command line without a special option switch (/K). I don't really understand why you need to open another DOS Box to run something, as the system() command will wait for the process to terminate before continuing. Nevertheless, with the /K option, your script should open a new DOS box to run the script.
        my $execution_path = "cmd.exe"; my $options = "/K C:\\Perl\\Perl.exe"; my $process = join (" ",$execution_path, $options); system ($process);

        However, if you can't get what you want out of system(), then check out Win32::Process, this has numerous facilities for creating and manipulating processes, you can even suspend or kill processes at a PID level. As for the TK question, I'm afriad that I haven't even started looking at TK yet.

        Seems BrowserUK got there before me on the /K thing though....

Re: A few random questions.
by waswas-fng (Curate) on Dec 09, 2003 at 21:50 UTC
    Take a look at Win32::Job for the cmd subproccess. It is nice and has a controlling interface. I am not into TK so I can't answer the other questions.

by flyingmoose (Priest) on Dec 10, 2003 at 02:44 UTC

    First off, on the subject of Tk, I highly recommend "Mastering Perl Tk" (the O'Reilly Emu Book) for a reference on all things Tk. It is very easy to understand (more so than most computer books) and it includes some features of Tk that you probably aren't going to pick up easily from reading CPAN.

    I haven't done too much with images in Tk, yet, but I have added images to a Canvas using Tk::Photo.

    Apparently, you can also use Tk::Bitmap to add icons to Labels (and I presume buttons as well). Out of respect for the author's work, I won't copy the examples I have from the Emu book.

    bgstipple is apparently for drawing patterns, not graphics, so I would avoid trying to use that if there are files you would like to display.

Re: A few random questions. (1!)
by tye (Sage) on Dec 11, 2003 at 07:25 UTC

    Don't forget the very useful and undocumented:

    system( 1, "command line goes here" );
    [which is just like sytem("command line") except that it doesn't wait for the subprocess that gets spawned to run the command.] Note that I don't bother talking about system( 1, "cmd", @args ) as on Win32 such (last I checked) just silently turns into system( 1, "cmd @args" ), unfortunately.

    It is just too bad that the way it was implemented (the magical numeric "1" which also prevents use of an indirect object, I think) is just crufty enough that it isn't supported in Unixish builds of Perl. It would be handy even in Unix as the alternatives aren't quite as nice (and the big win would be that we'd then have a portable way of doing this in Perl).

    I'd like to have it in Unix (all) Perl even with the current cruftiness.

                    - tye