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

Wise Monks,
I am using wxPerl to write a GUI. The wxButton class is derived from the wxWindow class (one of the four classes it is derived from ). So to disable a button I want to use the Disable method of the wxWindow class. Here is the code how it is declared and set up.
sub new { my( $self, $parent, $id, $title, $pos, $size, $style, $name ) = @_ +; $parent = undef unless defined $parent; $id = -1 unless defined $id; $title = "" unless defined $title; $pos = wxDefaultPosition unless defined $pos; $size = wxDefaultSize unless defined $size; $name = "" unless defined $name; $style = wxDEFAULT_FRAME_STYLE unless defined $style; $self = $self->SUPER::new( $parent, $id, $title, $pos, $size, $sty +le, $name ); $self->{notebook_1} = Wx::Notebook->new($self, -1, wxDefaultPositi +on, wxDefaultSize, 0); $self->{inc_pane} = Wx::Panel->new($self->{notebook_1}, -1, wxDefa +ultPosition, wxDefaultSize, ); $self->{avail_but} = Wx::Button->new($self->{inc_pane}, $avail_but +, "Availability"); .... EVT_BUTTON ($self, $avail_but, sub { <b>$self->{avail_but}->SUPER::Disable(); </b> } } package main; unless(caller){ local *Wx::App::OnInit = sub{1}; my $app = Wx::App->new(); Wx::InitAllImageHandlers(); my $frame_1 = MyFrame->new(); $app->SetTopWindow($frame_1); $frame_1->Show(1); $app->MainLoop(); }


When I call parent's method using the SUPER::method() call , it still doesnt work and the parent is seen as wxFrame not wxWindow. Also wxButton does not have a Disable method of its own, so no problems of overriding here. I thought I should be able to call $object->parentMethod() directly since first parentMethod found will be the only parent method existing.

Thank you for your response to my previous node given below:
wxPerl Event Handling/GUI process control

Follow up question, when I fork, I have a problem with setting a variable in the main parent loop and making the child process check that variable even though I am passing a reference to the variable in the child process,. If I update variable in parent process when child is already set off, the next time child checks the variable it still sees the old value of the variable i.e the value when the child process was kicked off.

Sorry for the long tortuous sentences, I really appreciate all your help.
Thanks
  • Comment on (wx)?Perl - Calling methods of parent class, checking variables in child process after fork
  • Download Code

Replies are listed 'Best First'.
Re: (wx)?Perl - Calling methods of parent class, checking variables in child process after fork
by Joost (Canon) on Nov 22, 2007 at 23:46 UTC
    In general, if a method "foo" is defined by any superclass of a particular $object you can just call $object->foo(). The only reason you would need SUPER::foo is when you've overriding foo in the current package but you still want to call foo in whatever superclass defines it first (update: or last, depending on how you look at it: see perlobj).

    As for the fork() question: you can't modify any state in any parent process from a child process directly (update: or vice versa). You'll have to use the same mechanisms that you'd use to communicate between "unrelated" processes, though there are a few methods to simplify setting up the communication between parent and child processes. See perlipc and perlopentut for starters.

Re: (wx)?Perl - Calling methods of parent class, checking variables in child process after fork
by ikegami (Patriarch) on Nov 23, 2007 at 02:50 UTC

    when I fork, I have a problem with setting a variable in the main parent loop and making the child process check that variable even though I am passing a reference to the variable in the child proces

    References always refer to addresses in the process's own memory spaces. Each process has its own memory space, and one process cannot access the memory space of another process. When the child dereferences a reference, it's accessing the copy of the referenced variable in the child's memory space and not to a variable in the parent's memory space.