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

Gentle Monks,

Today I have decided to make a windows front end using Win32::GUI. Subroutines play into this pretty heavily and I am running into problems. Specifically I need to figure out how to properly pass variables into my subroutine, and if I am correctly calling my sub. Here is my code:

use Win32::GUI; $main=Win32::GUI::Window->new(-title=>"application",-name=>"Main",-wid +th => 400, -height => 200); $main->AddButton(-name=>"BOK",-text=>"GO!",-pos=>[148,125], -onClick=> + \&SUBMIT,); my $urlfield = $main->AddTextfield( -name => "urlfield", -text => "", -left => 10, -top => 10, -width => 200, -height => 25, -prompt => ["URL:", 80], ); $main->Show(); Win32::GUI::Dialog(); sub Window_Terminate { return -1; # Stop message loop and finish program } sub SUBMIT { my ($submitted_url) = @_; print "$submitted_url \n"; }

I thought that I had it correct. I call SUBMIT within the AddButton, and it will print newlines, but no data. What am I missing? I appreciate everyone's help.

Thanks,
ghettofinger

Replies are listed 'Best First'.
Re: Passing variables to subroutines and Win32::GUI
by pg (Canon) on Aug 21, 2005 at 07:31 UTC

    Like this:

    -onClick => sub {SUBMIT($blah1, $blah2)}

      That gets me closer, but now that makes my $blah1 variable: Win::32::GUI::Textfield=HASH(01Xc2c35b). I am not sure why this is.

        What am I missing?
        You are assuming that Button BOK, when calling its own onClick event handler, will pass the text of the Textfield urlfield. This will not work. What *does* get passed is just a copy of the BOK Button object, which you see printing as Win32::GUI::Textfield=HASH(0x1c2c35b). You need to obtain the URL another way.

        In the code below, I get the URL by calling the $urlfield->Text() method within the onClick event handler of the BOK Button.
        I also made these changes:

        • Renamed sub Window_Terminate to sub Main_Terminate, to match the -name you gave your main window.
          A sub named Window_Terminate will never be called in your program, because no object is named Window.
        • Added return 1; to the -onClick sub; Always return 1 from a Win32::GUI event handler, unless you *want* to cause alternate behavior, like ignoring the event.
          For a better explanation, see the Win32::GUI::UserGuide::Concepts documentation, updated last month for version 1.02 of Win32::GUI.
        • Changed the print to Data::Dumper, because it is a better debugging tool.
        • Added -dialogui to Win32::GUI::Window->new(), to enable keyboard navigation.
        • Added -tabstop to AddTextfield() and AddButton(), to allow you to tab between them.
        • Added -ok to AddButton(), to allow you to press the button by pressing ENTER in the text field.
        • Added $urlfield->SetFocus(), so that you don't have to click the Textfield to start typing.
        Working, tested code:
        use strict; use warnings; use Win32::GUI; use Data::Dumper; $Data::Dumper::Useqq = 1; $| = 1; my $main = Win32::GUI::Window->new( -title => 'application', -name => 'Main', -width => 400, -height => 200, -dialogui => 1, ); my $urlfield = $main->AddTextfield( -name => 'urlfield', -text => '', -left => 10, -top => 10, -width => 200, -height => 25, -prompt => ['URL:', 80], -tabstop => 1, ); $main->AddButton( -name => 'BOK', -text => 'GO!', -pos => [148,125], -tabstop => 1, -ok => 1, -onClick => sub { # Either of these work: my $text = $urlfield->Text(); # my $text = $main->urlfield->Text(); SUBMIT( $text ); return 1; }, ); $urlfield->SetFocus(); $main->Show(); Win32::GUI::Dialog(); sub SUBMIT { my ($submitted_url) = @_; print Dumper $submitted_url; } sub Main_Terminate { # -1 = Stop message loop and finish program return -1; }