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

I am making a Tk program. I was wondering if I could run a different perl program that I wrote, by using a button? Also can I run a "EXE" program?

PLEASE REPLY!!!!!!!!!!!!!!

Replies are listed 'Best First'.
Re: Running a new Perl program
by Zaxo (Archbishop) on May 16, 2004 at 22:54 UTC

    You can run external programs with your choice of system, fork, open, backticks, or exec. For what I think you want, a combination of fork and exec sounds right.

    { my @prog = ('/path/to/program', @opts, @args); my $pid = fork(); defined $pid or warn "Could not launch @prog" and last; last if $pid; # Parent # adjust child environment here, if desired exec @prog; }
    fork sets up a child process which then exec's the external program. Similar code can be wrapped up in a sub which acts as your Tk button handler.

    Update: You should also make arrangements to prevent the child from going zombie. That can be done by setting up a $SIG{CHLD} handler, or with wait/waitpid. In an event-driven environment, and with the run of the child indeterminate, the SIGCHLD handler 'IGNORE' is probably best if that is usable on your platform.

    After Compline,
    Zaxo

      Don't forget qx, or the various platform-specific "Launch" methods available also.

Re: Running a new Perl program
by davidj (Priest) on May 27, 2004 at 14:47 UTC
    You need to be careful how you do this as Tk will block and you will not be able to access any other functionality of your Tk program until the called program exits. This may not be an issue unless the called program takes a long time to execute.

    Also can I run a "EXE" program?

    This leads me to believe you are doing this in a Windows environment. Check out Win32::Process for how to call an "EXE" as a subprocess (which will prevent blocking). The code below is a very simple example of what you are probably looking for.

    use Tk; use Win32::Process; use Win32; use strict; my $mw = MainWindow->new(); my $npBtn = $mw->Button(-text => "Notepad", -command => [\&run_prog, "C:\\winnt\\system32\ +\notepad.exe"] )->pack(); my $cBtn = $mw->Button(-text => "Calc", -command => [\&run_prog, "C:\\winnt\\system32\\ +calc.exe"] )->pack(); MainLoop; sub run_prog() { my $prog = shift; Win32::Process::Create($po, $prog, "", 0, NORMAL_PRIORITY_CLASS, "." ) || die &error(); } sub error() { print Win32::FormatMessage( Win32::GetLastError() ); }
    hope this helps,
    davidj