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

I am using Perl/Tk to create a GUI. The problem I need help on deals with running a separate perl script. So, I have the main GUI that is quite large. I want to run a separate script to do a lot of text analysis/manipulation. So, when I call the separate perl script from my GUI, I want to display some status messages of what its currently working on. I have created a test example to demonstrate what I am currently doing. The first is the main program and the second is the separate Perl script

MAIN:

#!/usr/bin/perl -w # perlCall.pl use Tk; use TK::ROText; # create the Main Windoow my $mw = MainWindow->new; $resultsBox = $mw->Scrolled('Text', -relief => 'sunken', -width => 100, -height => 32, -background => 'gray90', -scrollbars => 'ose', -selectbackground => 'gray60',)->p +ack(-fill => 'both', -expand => 1); $mw->Button(-text => 'Run Script', -command => \&CallScript)->pack(-fi +ll => 'both', -expand => 1); sub CallScript { @myarray = `perl C:\\IBM\\test_code\\otherScript.pl`; $resultsBox->insert('end', "@myarray"); } MainLoop;

SEPARATE: (otherScript.pl)

#!/usr/bin/perl -w # otherScript.pl for ($i = 0; $i < 10; $i++) { print "This is a test at iteration number: $i\n"; } for ($i = 0; $i < 10; $i++) { sleep (1); print "Testing $i, \n"; }
As shown in the example, it waits until the full separate script is finished before it displays any of the text. Basically, I want the GUI to act a command prompt that can be updated as the script goes along or at least have text updates. Such as when the first for loop finishes, insert that into the text box. On a less-important question, how do I insert the text without the space in the beginning of each line?

Replies are listed 'Best First'.
Re: Calling another script from Perl/Tk
by zentara (Cardinal) on Oct 22, 2008 at 20:55 UTC
    while (<SCRIPT>) { $resultsBox->insert('end', $_); }
    You are going to have "blocking" problems in your Tk script, when reading the pipe filehandle that way. What will happen is your Tk gui will be unresponsive while it waits on the pipe. See how to do non-blocking win32 pipe reads for Tk at Please suggest a non-forking way to do this (OS: windows). See ikegami's example. On unix, we can use Tk::fileevent on pipes, but it dosn't work on win32, so you need special hacking.

    But.... if your script returns that array quickly, you may be able to do it your way.


    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Calling another script from Perl/Tk
by Illuminatus (Curate) on Oct 22, 2008 at 20:00 UTC
    You should switch from backticks to a pipe open:
    open SCRIPT, "<code-to-execute> |" or die "script failed: $!";
    As long as your script output is line-based, you can use
    <SCRIPT>
    to read lines as they are printed (as long as you set $| to 1 inside the script you are calling).
      Thanks for the help! However, could you guide me a little bit farther. I have tried to change my code to this:
      open SCRIPT, "otherScript.pl |" or die "script failed: $!"; $resultsBox->insert('end', $SCRIPT);
      I am sure that I am not implementing this correctly. I am assuming that I will need to do a loop of some sort to keep inserting the new lines until the script is finished. But, I am at a loss with the new code you gave me. Also, I don't know what you mean by I could use <SCRIPT>. My separate code is line-based and should only pass text. I did put a line in my called script of $| = 1;.
        You must be really new to perl. SCRIPT in this case is a filehandle, not a scalar or array. You should be able to do something like
        open SCRIPT, "otherScript.pl |" or die "script failed: $!"; while (<SCRIPT>) { $resultsBox->insert('end', $_); } close SCRIPT;
Re: Calling another script from Perl/Tk
by Anonymous Monk on Jan 28, 2009 at 11:52 UTC
    hello i am new to using perl tk,can you help me knowing how to call a function to execute in perltk when we click or select an widget ex: click an submit button and the other perl script executes and out dispalyed in an new window
      plzz reply fast?????????????????/