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

How would you get a return value from a Button event when the event calls a subroutine. Below is the code for my button:
my $mw = MainWindow->new; $mw->title("Something"); my $but_browse = $frm_name -> Button(-text=>"BROWSE", -command =>\&load_listbox) -> pack(-side => 'left'); sub load_listbox { my $filepath = $mw->chooseDirectory() or return(); opendir (DIR, $filepath) || die("Cannot open directory"); $lb->delete(0, 'end'); $lb->insert('end', grep { ! -d } readdir DIR); close DIR; $lb->bind('<Button-1>', sub { $ent4->configure(-text => $lb->get($lb->curselection( )) ); $ent5->focus; }); }
When this button is clicked, I am able to choose a directory. I store the path in $filepath, how to do I access that variable to get the path? I have another button that needs the filepath from this button? Do I need "return $filepath;" at the end of the load_listbox sub? Whats the code I would need to access it? Would it be something like "$thepath = $but_browse=>get();". Thanks in advance!

Replies are listed 'Best First'.
Re: Return value from Button event
by zentara (Cardinal) on Sep 07, 2010 at 18:29 UTC
    Are you looking to put the information in a listbox?
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new; $mw->title("Something"); my $but_browse = $mw -> Button(-text=>"BROWSE", -command =>\&load_listbox) -> pack(-side => 'left'); my $lb = $mw->Listbox()->pack( -side => 'left' ); MainLoop; sub load_listbox { my $filepath = $mw->chooseDirectory() or return(); opendir (DIR, $filepath) || die("Cannot open directory"); $lb->delete(0, 'end'); $lb->insert('end', grep { ! -d } readdir DIR); close DIR; print "$filepath\n"; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Return value from Button event
by Anonymous Monk on Sep 07, 2010 at 16:35 UTC
    Shortest solution is to make $filepath a global variable, or you could store it as your mainwindow attribute
    $mw->{_my_store}{filepath}
    Coping with Scoping
Re: Return value from Button event
by dasgar (Priest) on Sep 07, 2010 at 18:13 UTC

    Based on some things in your post, I'm assuming that your doing some GUI work. Also, since some of the syntax doesn't look quite like what I remember from Win32::GUI, I'm assuming that you're using Tk or some other GUI module.

    Going off of those assumptions, here's the concept of what I've done using Win32::GUI. If you're using something else, you should still be able to apply the concepts with the module(s) that you're using.

    • Create a textfield. Make it read only if you don't want the user to modify it directly.
    • Create a browse button. When it's clicked, do the following:
      • Pop up a folder browser window.
      • If the pop up is canceled, do nothing.
      • If a file/directory is selected, update the textfield to display the path selected.
    This allows for the selected path to be accessible by retrieving the text value of the textfield. If for some reason you can't use a textfield (or don't want to use one), then I'd agree with previous reply about using global variables.