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

Hi guys! I'd like to do the following: I have a very simple GUI form, which has menu File. In that menu user can choose Source directory and Out directory. Than, if he presses Process button from Action menu, this to dirs should be processed (doesn't matter how exactly in this case). Please see the code:
use strict; use Wx; package WxPerlComExample; use base qw(Wx::App); sub OnInit { my $self = shift; my $frame = WxPerlComExampleFrame->new(undef, -1, "WxPerl Example" +); $frame->Show(1); $self->SetTopWindow($frame); return 1; } package WxPerlComExampleFrame; use base qw(Wx::Frame); use Wx qw( wxDefaultPosition wxDefaultSize wxDefaultPosition wxDefaultSize wx +ID_EXIT ); use Wx::Event qw(EVT_MENU); our @id = (0 .. 100); # IDs array sub new { my $class = shift; my $self = $class->SUPER::new( @_ ); ### CODE GOES HERE ### # Create menus my $firstmenu = Wx::Menu->new(); $firstmenu->Append($id[0], "Choose Source dir"); $firstmenu->AppendSeparator(); $firstmenu->Append($id[7], "Choose Out dir"); my $secmenu = Wx::Menu->new(); $secmenu->Append($id[8], "Process files"); $secmenu->Append(wxID_EXIT, "Exit\tCtrl+X"); # Create menu bar my $menubar = Wx::MenuBar->new(); $menubar->Append($firstmenu, "File"); $menubar->Append($secmenu, "Actions"); # Attach menubar to the window $self->SetMenuBar($menubar); $self->SetAutoLayout(1); # Handle events EVT_MENU( $self, $id[0], \&ChooseIn ); EVT_MENU( $self, $id[7], \&ChooseOut ); EVT_MENU( $self, $id[8], \&ProcessFiles ); EVT_MENU( $self, wxID_EXIT, sub {$_[0]->Close(1)} ); return $self; } ### PUT SUBROUTINES HERE ### sub ChooseIn { my ($self, $event) = @_; my $dialog = Wx::DirDialog->new($self, '', '', &Wx::wxDD_CHANGE_DI +R ); $dialog->ShowModal; my $dir_in = $dialog->GetPath; # That's what I want to have access + to } sub ChooseOut { my ($self, $event) = @_; my $dialog = Wx::DirDialog->new($self, '', '', &Wx::wxDD_CHANGE_DI +R ); $dialog->ShowModal; my $dir_out = $dialog->GetPath; # That's what I want to have acces +s to } # sub ProcessFiles { # ***SOME PROCESSING CODE*** # } package main; my($app) = WxPerlComExample->new(); $app->MainLoop();
The question is - how do I get variables $dir_out and $dir_in from subs ChooseIn and ChooseOut? I have to pass this vars to ProcessFiles sub, while this subs are not called directly, only via reference. I actually can make them anonymous subs as the third argument of EVT_MENU, and define this vars before, but is it the best solution?
  • Comment on WxPerl and accessing subroutine return variables without direct call of subroutine
  • Download Code

Replies are listed 'Best First'.
Re: WxPerl and accessing subroutine return variables without direct call of subroutine
by Anonymous Monk on Jun 21, 2014 at 08:46 UTC
    When you register/set up the handlers, ask yourself what is $self? What is @_ in the event handlers?

    Now ask yourself where you should store values important to your application?

    :D its all about $self, $self is your stash, if there is a live child of $self , you let the $child keep its own value (which you can GetValue), for temporary children no longer alive (like $dialog ), you store the value important to you in $self, say  $self->{dir_in} = $dir_in;

    Make sense?

      Please give me a second to comprehend this. Is it correct: I can set $dir_in like this $self->{dir_in} = $dir_in; inside a subroutine, and then call $dir_in from main subroutine? But how exactly does this call look?

        and then call $dir_in from main subroutine? But how exactly does this call look?

        There is no "calls", its a variable, ita a hashref, perlintroPerl variable types, Hashes