Hi,

You're correct that your problem is in the nested subroutines. The behavior of the handlers you set up inside of verify() is not what you're expecting. There's a lot of stuff going on here that can trap you.

First of all, when you try running this script with warnings enabled (perl -w myscript.pl), you'll get messages like this:

Variable "$result" will not stay shared at verify.pl line 68.
Variable "$outputfield" will not stay shared at verify.pl line 69.
Variable "$result" will not stay shared at verify.pl line 74.
Variable "$outputfield" will not stay shared at verify.pl line 75.

It's always a good idea to use strict and use warnings when you're writing Perl.

Basically, the two variables you're referring to in the handlers ($result and $outputfield) are recreated each time you call verify(), but the handlers will only see the first instance of $result and $outputfield. Successive calls to the handlers are meaningless because $result and $outputfield were local to the first call to verify().

Here's some comments:

sub Pass_Click {

    # Here you are assigning to the $result variable that was privately
    # scoped within the first call to verify()
    $result = 1;

    # Same thing here.  You are modifying the text field in the first text
    # field created on the first call to verify()
    $outputfield->Text("result : $result");

    # Try returning 0 in this handler and in the Fail_Click handler.
    # You'll see that the output field is changing to what you would think at
    # first, but when you close (X out) the first window, the successive
    # windows' text fields won't get updated

    -1;   # say 'return 0' instead;
}


For a better explanation on nested subroutines and private variables, check out these articles:
* http://www.webreference.com/programming/perl/subroutines/2.html
* http://perl.com/pub/a/2002/05/07/mod_perl.html

My take on nested subroutines: If you never plan on using mod_perl, you could probably just completely avoid nested subroutines and be just fine.

So anyway, back to your problem. I'm not really sure what you're trying to accomplish with this script, but an alternative approach you could take could be to only create your window once (at the startup of your script). When you later call verify, (1) show the window, (2) enter the Dialog, (3) handle user input, (4) exit the dialog, (5) then hide the window.

Good Luck,

Charles Alderman

Quoting luksedj the first <[EMAIL PROTECTED]>:

Hello,

I'm just starting to use Win32::GUI and I can't seem to explain the behavior of my application.

I have a main perl program that occasionally needs user input. On those occasions I call a function (sub) that builds the window and starts the dialog, and returns a result gathered from the user input.

The first time this function is called, it works as expected. The user clicks a button, the button callback sets a value, and terminates the Dialog(). The value that was set is the return value of the function.

However, on the second and following times that the function is called, the window is shown, via printouts I can see the return value being set, but when the dialog exits and the function is about to return its return value, the return value is the default one, in stead of the set one. I assume that it has to do something with me, coding the event handlers inside the function that does all the gui stuff. If that is so, can someone explain this behavior? I don;t mind changing my code, but then I want to understand why it behaves like this.

The code is pasted below. The behavior I want is that the user clicks on the pass button which returns 1 to the calling function. If you press 'passed' in every test, then you'll see the return value is 0 starting from the second call to the 'verify' function.


use Win32::GUI();



my $canvas_height = 328;
my $title_height = 32;
my $margin = 15;


for ($i=0; $i<5;$i++){
    my $output = "test $i";
    print "$output ";
    my $result_1 = verify($output);
    print $result_1;
    print " $output\r\n";
}


sub verify {
    my $output = shift;

    my $result = 0;
    my $main =
    Win32::GUI::DialogBox->new(
                   -name    => 'Main',
                   -width   => 280,
                   -height  => $canvas_height+$title_height,
                   -text    => 'Test Step Output Verification',
                   -helpbox => 0,
                   );


    my $outputfield = $main->AddTextfield(
                      -name => 'Outputfield',
                      -multiline => 1,
                      -hscroll => 1,
                      -vscroll => 1,
                      -width => 215,
                      -height => 200,
                      -pos => [ $margin, 60],
                      -disabled => 0,
                      -readonly => 1,
                      );

    my $pass = $main->AddButton(
                -text => 'Passed',
                -width => 100,
                -height => 25,
                -pos => [$outputfield->Left(),
                     $outputfield->Height() + $outputfield->Top() + $margin],
                -name => 'Pass',
                -tabstop => 1
                );

    my $fail = $main->AddButton(
                -text    => 'Failed',
                -width   => 100,
                -height  => 25,
-pos => [$outputfield->Left() + $outputfield->Width() - 100,
                         $pass->Top()],
                -name    => 'Fail',
                -tabstop => 1
                );


    sub Pass_Click {
      $result = 1;
      $outputfield->Text("result : $result");
      -1;
    }

    sub Fail_Click {
      $result = 0;
      $outputfield->Text("result : $result");
      -1;
    }

    sub Main_Terminate {
    -1;
    }


    $outputfield->Text($output);
    $main->Show();
    Win32::GUI::Dialog();
    return $result;
}


Thanks for any hints,

luksedj





____________________________________________________________________________________
Expecting? Get great news right away with email Auto-Check.
Try the Yahoo! Mail Beta.
http://advision.webevents.yahoo.com/mailbeta/newmail_tools.html

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Perl-Win32-GUI-Users mailing list
Perl-Win32-GUI-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users
http://perl-win32-gui.sourceforge.net/




Reply via email to