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

Hello, I am fairly new to Perl and Perl/Tk. I am running a linux system with Blackbox as my windows manager. Bbrun will not compile on this system since that was made for a RedHat 6.2. So I am attempting to make a gui run command prompt. I have run into a problem of how to send the command to the system to be executed. Perhaps someone could help me. Below is the code.
#!/usr/bin/perl -w use Tk; use warnings; use diagnostics; use strict; my $text; my $mw = MainWindow->new; $mw->title("PerlRun"); $text = $mw->Text(-height => 1, -width => 15)->pack(-anchor => 'nw', -side => 'top'); $mw->Button(-text => " Run ", -command => [ \&runprog ]) ->pack(-side => 'left', -anchor => 'sw'); $mw->Button(-text => "Done", -command => sub { exit }) ->pack(-side => 'left', -anchor => 'sw'); sub runprog { system "\$text"; exit; } MainLoop;
Thank You

Replies are listed 'Best First'.
Re: Perl/Tk run program
by JamesNC (Chaplain) on Jul 29, 2003 at 03:20 UTC
    Try using an entry widget like so:
    use Tk; use warnings; use diagnostics; use strict; my $text; my $mw = MainWindow->new; $mw->title("PerlRun"); $mw->Entry(-width => 30, -textvariable, \$text)->pack(-anchor => 'nw', -side => 'top'); $mw->Button(-text => " Run ", -command => \&runprog ) ->pack(-side => 'left', -anchor => 'sw'); $mw->Button(-text => "Done", -command => sub { exit }) ->pack(-side => 'left', -anchor => 'sw'); sub runprog { system($text); } MainLoop;
    If you wanted to use the Text widget, than your only problem was that you were trying to run the command on the ref to the $text widget instead of the variable in the runprog sub, so.. what you really wanted to do is this:
    my $cmd = $text->get('0.1','end'); chomp $cmd; system($cmd);
    Take a look at the Mastering Perl/Tk by Steve Liddie... great book :)
    Cheers,
    JamesNC
      That works great. Next, how do I get the process to run in the background and the perlrun to quit, without it stopping the program I just started. I am really new to perl and I do not know the commands. I have Learning Perl/Tk by Schwartz & Phoenix, O'reilly. Thank you for your help.
        Try using backticks and you can bg it like you would any unix process from the command line, I am on WinXP and would probably use Win32::Process... but you can just do something like:
        my $rv = `someprog &`; # try this instead of your system() call...
        Since I am not on a Unix box that is an untested line
        Tk's main loop keeps running until you exit... There is a better way to do non-blocking system calls however... you should do some reading of the perl/tk faq's or read about it in Mastering Perl/Tk which is an O'Reilly book ( ISBN 1-56592-716-8) I believe the Schwartz book you are referring to is "Learning Perl". The Perl/Tk book is all about the Perl/Tk implementation and assumes you already are familiar with Perl.
        JamesNC