From: Charles A. <cha...@al...> - 2007-03-20 21:07:23
|
Hi, You're correct that your problem is in the nested subroutines. The =20 behavior of the handlers you set up inside of verify() is not what =20 you're expecting. There's a lot of stuff going on here that can trap =20 you. First of all, when you try running this script with warnings enabled =20 (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 =20 writing Perl. Basically, the two variables you're referring to in the handlers =20 ($result and $outputfield) are recreated each time you call verify(), =20 but the handlers will only see the first instance of $result and =20 $outputfield. Successive calls to the handlers are meaningless =20 because $result and $outputfield were local to the first call to =20 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 =3D 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, =20 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, =20 you could probably just completely avoid nested subroutines and be =20 just fine. So anyway, back to your problem. I'm not really sure what you're =20 trying to accomplish with this script, but an alternative approach you =20 could take could be to only create your window once (at the startup of =20 your script). When you later call verify, (1) show the window, (2) =20 enter the Dialog, (3) handle user input, (4) exit the dialog, (5) then =20 hide the window. Good Luck, Charles Alderman Quoting luksedj the first <lu...@ya...>: > Hello, > > I'm just starting to use Win32::GUI and I can't seem to explain the =20 > behavior of my application. > > I have a main perl program that occasionally needs user input. On =20 > those occasions I call a function (sub) that builds the window and =20 > starts the dialog, and returns a result gathered from the user input. > > The first time this function is called, it works as expected. The =20 > user clicks a button, the button callback sets a value, and =20 > terminates the Dialog(). The value that was set is the return value =20 > of the function. > > However, on the second and following times that the function is =20 > called, the window is shown, via printouts I can see the return =20 > value being set, but when the dialog exits and the function is about =20 > to return its return value, the return value is the default one, in =20 > stead of the set one. I assume that it has to do something with me, =20 > coding the event handlers inside the function that does all the gui =20 > stuff. If that is so, can someone explain this behavior? I don;t =20 > mind changing my code, but then I want to understand why it behaves =20 > like this. > > The code is pasted below. The behavior I want is that the user =20 > clicks on the pass button which returns 1 to the calling function. =20 > If you press 'passed' in every test, then you'll see the return =20 > value is 0 starting from the second call to the 'verify' function. > > > use Win32::GUI(); > > > > my $canvas_height =3D 328; > my $title_height =3D 32; > my $margin =3D 15; > > > for ($i=3D0; $i<5;$i++){ > my $output =3D "test $i"; > print "$output "; > my $result_1 =3D verify($output); > print $result_1; > print " $output\r\n"; > } > > > sub verify { > my $output =3D shift; > > my $result =3D 0; > my $main =3D > Win32::GUI::DialogBox->new( > -name =3D> 'Main', > -width =3D> 280, > -height =3D> $canvas_height+$title_height, > -text =3D> 'Test Step Output Verification', > -helpbox =3D> 0, > ); > > > my $outputfield =3D $main->AddTextfield( > -name =3D> 'Outputfield', > -multiline =3D> 1, > -hscroll =3D> 1, > -vscroll =3D> 1, > -width =3D> 215, > -height =3D> 200, > -pos =3D> [ $margin, 60], > -disabled =3D> 0, > -readonly =3D> 1, > ); > > my $pass =3D $main->AddButton( > -text =3D> 'Passed', > -width =3D> 100, > -height =3D> 25, > -pos =3D> [$outputfield->Left(), > $outputfield->Height() + $outputfield->Top() + $margi= n], > -name =3D> 'Pass', > -tabstop =3D> 1 > ); > > my $fail =3D $main->AddButton( > -text =3D> 'Failed', > -width =3D> 100, > -height =3D> 25, > -pos =3D> [$outputfield->Left() + =20 > $outputfield->Width() - 100, > $pass->Top()], > -name =3D> 'Fail', > -tabstop =3D> 1 > ); > > > sub Pass_Click { > $result =3D 1; > $outputfield->Text("result : $result"); > -1; > } > > sub Fail_Click { > $result =3D 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 yo= ur > opinions on IT & business topics through brief surveys-and earn cash > http://www.techsay.com/default.php?page=3Djoin.php&p=3Dsourceforge&CID=3DD= EVDEV > _______________________________________________ > Perl-Win32-GUI-Users mailing list > Per...@li... > https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users > http://perl-win32-gui.sourceforge.net/ > |