From: luksedj t. f. <lu...@ya...> - 2007-03-20 09:34:32
|
Hello,=0A=0AI'm just starting to use Win32::GUI and I can't seem to explain= the behavior of my application.=0A=0AI have a main perl program that occas= ionally needs user input. On those occasions I call a function (sub) that b= uilds the window and starts the dialog, and returns a result gathered from = the user input.=0A=0AThe first time this function is called, it works as ex= pected. The user clicks a button, the button callback sets a value, and ter= minates the Dialog(). The value that was set is the return value of the fun= ction.=0A=0AHowever, on the second and following times that the function is= called, the window is shown, via printouts I can see the return value bein= g set, but when the dialog exits and the function is about to return its re= turn 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 insi= de the function that does all the gui stuff. If that is so, can someone exp= lain this behavior? I don;t mind changing my code, but then I want to under= stand why it behaves like this.=0A=0AThe code is pasted below. The behavior= I want is that the user clicks on the pass button which returns 1 to the c= alling function. If you press 'passed' in every test, then you'll see the r= eturn value is 0 starting from the second call to the 'verify' function.=0A= =0A=0Ause Win32::GUI();=0A=0A=0A=0Amy $canvas_height =3D 328;=0Amy $title_h= eight =3D 32;=0Amy $margin =3D 15;=0A=0A=0Afor ($i=3D0; $i<5;$i++){=0A m= y $output =3D "test $i";=0A print "$output ";=0A my $result_1 =3D ver= ify($output);=0A print $result_1;=0A print " $output\r\n";=0A}=0A = =0A=0Asub verify {=0A my $output =3D shift;=0A =0A my $result =3D = 0;=0A my $main =3D=0A Win32::GUI::DialogBox->new(=0A = -name =3D> 'Main',=0A -width =3D> 280,=0A = -height =3D> $canvas_height+$title_height,=0A = -text =3D> 'Test Step Output Verification',=0A -helpbo= x =3D> 0,=0A );=0A =0A =0A my $outputfield =3D $= main->AddTextfield(=0A -name =3D> 'Outputfield',=0A = -multiline =3D> 1,=0A -hscroll =3D>= 1,=0A -vscroll =3D> 1,=0A -width= =3D> 215,=0A -height =3D> 200,=0A = -pos =3D> [ $margin, 60],=0A -disabled =3D> 0,=0A = -readonly =3D> 1,=0A );=0A =0A = my $pass =3D $main->AddButton(=0A -text =3D> 'Passed',=0A = -width =3D> 100,=0A -height =3D> 25,=0A = -pos =3D> [$outputfield->Left(),=0A $outputfi= eld->Height() + $outputfield->Top() + $margin],=0A -name =3D= > 'Pass',=0A -tabstop =3D> 1=0A );=0A =0A = my $fail =3D $main->AddButton(=0A -text =3D> 'Failed',= =0A -width =3D> 100,=0A -height =3D> 25,= =0A -pos =3D> [$outputfield->Left() + $outputfield->Widt= h() - 100,=0A $pass->Top()],=0A -nam= e =3D> 'Fail',=0A -tabstop =3D> 1=0A );=0A= =0A =0A sub Pass_Click {=0A $result =3D 1;=0A $output= field->Text("result : $result");=0A -1;=0A }=0A =0A sub Fail_= Click {=0A $result =3D 0;=0A $outputfield->Text("result : $result= ");=0A -1;=0A }=0A =0A sub Main_Terminate {=0A -1;=0A }= =0A =0A =0A $outputfield->Text($output);=0A $main->Show();=0A = Win32::GUI::Dialog();=0A return $result;=0A}=0A=0A=0AThanks for any hi= nts,=0A=0Aluksedj=0A=0A=0A=0A=0A =0A_______________________________________= _____________________________________________=0AExpecting? Get great news r= ight away with email Auto-Check. =0ATry the Yahoo! Mail Beta.=0Ahttp://advi= sion.webevents.yahoo.com/mailbeta/newmail_tools.html |
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/ > |