Hi everyone!
I have a sizeable perl script that I'd like to add a GUI to. Perl/Tk seems like a good choice (but let me know if you think otherwise). The program itself is about 3000 lines of code; it does various operations on text files, reports on the results in the console and stops to ask for user input five or six times along the way. It's pretty linear, i.e. it executes the first 100 lines, asks the user if they want it to do X and then goes if ($do_x eq "y") {#does X} and keeps working its way down. Essentially, I need be able to set a couple of variables with Tk button presses and such instead of setting them through <STDIN>, hide the console window and print some text in the GUI window instead of the console. The script is cross-platform. On Windows, I need to pack it into an exe with pp, but my testing so far shows that pp works fine with Tk so there shouldn't be any problems there. I'd like to make the GUI optional because some linux/mac users might not be able/willing to install Tk.
Now, I've played a bit with Tk, and I know how to make buttons, radiobuttons, listboxes etc. and set variables based on them. I reckon I can build every element I need, but I don't know how to put things together. The GUI elements only show up once the script has reached the MainLoop; statement, so I can't just put MainLoop; at line 3000 where the program ends and sprinkle the GUI elements all over the place where the <STDIN> points currently are.
Do I need to put all the "functional" parts of the script in subroutines in order to be able to trigger them with Tk GUI button presses and such? Then I'd end up with a couple of hundred lines of Tk code at the start of the script, ending with MainLoop; and then 3000 lines of the actual perl code below it in various subs. I'd much prefer to keep the current linear structure, but it doesn't seem to be possible...? Also, the explanations and sample code I've found online seem to only deal with static applications where all the GUI elements show up at once when the program is started. I'm not sure how to make a Tk GUI that guides the user through several steps and redraws the window on user input (remove the buttons/text and show the buttons for the next step etc.)

In reply to Perl/Tk code structure by elef

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.