1

I posted "How to undersand the POE-Tk use of destroy?" in an attempt to reduce the bug in my production code to a test case. But it seems that the solution to the test case is not working in the full program.

The program is 800+ lines long so I am hesitant to post it in full. I realize that the snippets I provide here may be too short to be of any use, but I hope to get some direction in either where to look for a solution or what additional information I can provide.

Here is the Session::Create section of my POE-Tk app.


POE::Session->create(
    inline_states => {
        _start      => \&ui_start,
        get_zone    => \&get_zone,
        ping        => \&ping,
        mk_disable  => \&mk_disable,
        mk_active   => \&mk_active,
        pop_up_add => \&pop_up_add,
        add_button_press => sub {
            my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
            print "\nadd button pressed\n\n";
            &validate;
        },
        ih_button_1_press => sub {
            my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
            print "\nih_button_1 pressed\n\n";
            if( Tk::Exists($heap->{ih_mw}) ) {
                print "\n\nih_mw exists in ih_button_1_press\n\n";
            } else {
                print "\n\nih_mw does not exist in ih_button_1_press\n\n";
            }
            1;
            $heap->{ih_mw}->destroy if Tk::Exists($heap->{ih_mw});
            &auth;
        },
        pop_up_del => \&pop_up_del,
        auth        => \&auth,
#       validate    => \&validate,
        auth_routine => \&auth_routine,
        raise_widget    => \&raise_widget,
        del_action  => \&del_action,
        over        => sub { exit; }
    }
);

add_button_press is called here;


sub pop_up_add {
    ...
    my $add_but_2 = $add_frm_2->Button( 
        -text => "Add Record",
        -command => $session->postback("add_button_press"),
        -font => "{Arial} 12 {bold}") -> pack(
            -anchor => 'c',
            -pady => 6,
        );
    ...
}

validate creates the Toplevel widget $heap->{ih_mw};


sub validate {
    ...
    if( ! $valid ) {
        print "\n! valid entered\n\n";
        $heap->{label_text} .= "Add record anyway?";
        my $lt_ref = \$heap->{label_text};
    ...
        my $heap->{ih_mw} = $heap->{add_mw}->Toplevel( -title => "ih_mw");
    ... 
        if( Tk::Exists($heap->{ih_mw}) ) {
            print "\n\nih_mw exists in validate\n\n";
        } else {
            print "\n\nih_mw does not exist in validate\n\n";
        }
    ...
        my $ih_but1 = $heap->{ih_mw}->Button( -text => "Add",
            -font => 'vfont',
            -command => $session->postback("ih_button_1_press"),
            )->pack( -pady => 5 );
    ...
}

Pressing $ih_but1 results in this;

C:\scripts\alias\resource>alias_poe_V-3_0_par.pl

add button pressed

sub validate called

! valid entered

ih_mw exists in validate

ih_button_1 pressed

ih_mw does not exist in ih_button_1_press

So the $heap->{ih_mw} widget seems to be unkown to the ih_button_1_press anonymous subroutine even with the inclusion of "($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];"

Community
  • 1
  • 1
jpolache
  • 305
  • 3
  • 12
  • Please try to see whether both $heap and $heap->{ih_mw) are actually defined(). If $heap isn't defined then you could possibly still have the same problem as in your previous post. – Inshallah Jul 20 '09 at 20:22
  • I added a if(defined($heap)) in front of the if( Tk::Exists($heap->{ih_mw}) ). It came back that $heap is defined. ih_button_1 pressed... heap is defined... ih_mw does not exist in ih_button_1_press... – jpolache Jul 20 '09 at 20:29
  • What about defined($heap->{ih_mw})? – Inshallah Jul 20 '09 at 20:46

1 Answers1

2

Where does $heap in &validate come from? You don't pass it as a parameter. Could $heap in &validate and $heap in &in_button_1_press not be the same thing? Have you tried printing the stringy form of $heap to see if the addresses are the same in the two functions?

Andrew Barnett
  • 5,066
  • 1
  • 22
  • 25
  • Some missing code; use Tk; use POE qw( Loop::TkActiveState ); From perldoc POE::Session; POE::Session's Calling Convention sub handle_event { my ($kernel, $heap, $parameter) = @_[KERNEL, HEAP, ARG0]; ...; } Or the use of C"$_[KERNEL]", C"$_[HEAP]" and C"$_[ARG0]" inline, as is done in most examples. What's going on here is rather basic. Perl passes parameters into subroutines or methods using the @_ array. "KERNEL", "HEAP", "ARG0" and others are constants exported by POE::Session (which is included for free when a program uses POE). "stringy form of $heap"? – jpolache Jul 20 '09 at 19:54
  • You didn't answer my question: where does $heap come from in &validate? You call &validate from add_button_press without any arguments, so if you're getting $heap in &validate via $heap = @_[HEAP], then you're going to be in for a surprise. Also, the &validate form of function calling, vs just plain validate, does *not* inherit @_ from the calling context. – Andrew Barnett Jul 20 '09 at 20:27
  • 2
    Whoops. This doesn't look right (in validate): my $heap->{ih_mw} ... Try that without the 'my', and see if it works better. – Andrew Barnett Jul 20 '09 at 20:38
  • Andrew, if you will post that as an answer, I will be glad to check and upvote it ;). Many thanks. - Jon – jpolache Jul 20 '09 at 20:42
  • From perlsub (1): &NAME; # Makes current @_ visible to called subroutine. – Inshallah Jul 20 '09 at 20:48
  • Doh! I knew it was something like that. Thanks Inshalla. – Andrew Barnett Jul 21 '09 at 00:10