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

Hi monks,

I have a large perl program (let's call it "myscript.pl"), and now I'm writing a Tk interface for it. Earlier, when I sought your advice about how to call the perl program with a button click, some of you suggested this:

#!/usr/bin/perl use strict; use Tk; sub clicked { system ( 'myscript.pl' ); } my $top = MainWindow->new(); my $button = $top->Button ( -text => "click me", -command => \&clicked )->pack(); ... MainLoop();


My new problem:

In my Tk interface, I ask the user to browse and select the name of a file (let's call the variable $filename). I have to return this name back to myscript.pl so my original program can use it.

How do I do that? Would I change the -command property in my button to read

my $button = $top->Button ( -text => "click me", -command => [\&clicked, \$filename] )->pack();
???

Thanks for your help! I really appreciate this.

Replies are listed 'Best First'.
Re: Tk interface and the rest of the program -- II
by kvale (Monsignor) on Aug 15, 2002 at 20:22 UTC
    You have the right idea. For small callbacks, I usually create an anonymous sub:
    my $button = $top->Button ( -text => "click me", -command => sub {system "myscript.pl $filen +ame"} )->pack();
    and then have myscript.pl read in the filename from the command line.

    -Mark

      Hi kvale,

      So in myscript.pl, I should have something like

      my $filename = @_;


      in order to access $filename from the Tk interface?

      Thank you so much!
        Your statement assigns an array to a scalar, not what you want. To extract the subroutine parameter, use
        my $filename = shift;
        This shifts the first element off of @_ and assigns it to $filename.

        -Mark

Re: Tk interface and the rest of the program -- II
by rbc (Curate) on Aug 15, 2002 at 21:07 UTC
    You might also want to look at two Tk functions:
    getSaveFile and getOpenFile

    Just do ...
    $ perldoc Tk::getOpenfile
    ... to read all about it.
    It is easy to use.

    Good luck!