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

Hi Monks,

I've written a Perl program. Now I want to create a GUI for it using Perl/Tk.

I want the user to press a button, which will set the existing Perl program in motion. Is there a better way to do this other than turning my original program (which is very very big) into a subroutine within my Tk code?

Thanks!
  • Comment on Perl Tk User Interface & the rest of the Perl program

Replies are listed 'Best First'.
Re: Perl Tk User Interface & the rest of the Perl program
by rbc (Curate) on Aug 14, 2002 at 19:30 UTC
    how about something like this ...
    ... sub clicked { system ( 'myscript.pl' ); } my $top = MainWindow->new(); my $button = $top->Button ( -text => "click me", -command => \&clicked )->pack(); ... MainLoop();
      Dear rbc, derby & Courage,

      Thank you so much! Your suggestions totally helped me out.
Re: Perl Tk User Interface & the rest of the Perl program
by Courage (Parson) on Aug 14, 2002 at 19:24 UTC
    I suggest you to write independent GUI wrapper that will ask user for parameters, may be performs saving/restoring of values of previous sessions, an then pressing "process" will call your "command-line interface" program which will understand those options and will do the work.

    Also, instead of just running another perl instance, it is possible to do 'script-name.pl';

    I implemented several utilities at work using this way, and consider that as quite convenient way to do.

    Courage, the Cowardly Dog
    things, I do for love to perl...

      Dear Courage,

      Thanks for your reply!

      However, I'm extremely new to Perl & Tk, so I don't know how to write a wrapper. Are there properties in certain widgets that will allow me to return to the original program after the user presses the button?

      Thanks again!
        when user presses a button, you can manage it to invoke a sub which, in turn, will do what I just said and return.

        Here is oneliner for Win32 that will demonstrate what I just said:

        perl -MTk -lwe "for(tkinit){$_->Button(-text=>'test',-command=>sub{pri +nt'do anything you want here, i.e. call your favourite program'})->pa +ck;$_->Text->pack};MainLoop"
        OTOH there are quite a lot of examples of Tk programs here and there, so you can start using them right away.

        Courage, the Cowardly Dog

Re: Perl Tk User Interface & the rest of the Perl program
by derby (Abbot) on Aug 14, 2002 at 19:28 UTC
    Not a lot to go on but you could

    1. fork/exec or system the existing program.

    2. with a little work possibly do, require or use the existing program

    It really all depends on how you've structured the original script and how married you are to not changing it.

    -derby