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

I am new to Win32::GUI and I am in the process of designing a game known as KENO. (I didn't pick it, it was a engineering expo that I can no longer enter in...) But anyways, I am making 80 buttons that when clicked will push the value they represent into the array so that I can later check to see if they guessed right. The problem is that the subroutine goes before being clicked. It is probably a basic syntax error, but I am either blind or just stupid...

Here is the important parts of my code:

#!/usr/bin/perl use Win32::GUI(); # Perl module for access to Windows <API functions @chosen; $main = Win32::GUI::Window->new( -name => 'Main', #name of the window, use for events -width => 675, -height => 600, -text => 'Keno', #title of window -minsize => [675, 600], -dialogui => 1 #contorls special keyboard handling ); ########Controls#################################### # 80 Buttons... Probably better and much more efficient #way... # The subroutine under "-onClick => &Button_Click(x)" is #executing be +fore the app. even shows up #basically I have 80 similar buttons $Button1= Win32::GUI::Button->new(-parent => $main, -pushlike => 1, -n +ame => Button1, -text => '1', -onClick => &Button_Click(1),-height => + 50, -width => 50, -pos => [15, 55],); $main->Show(); #By default windows are hidden, so make + #it visible... Win32::GUI::Dialog(); #start a Windows message loop, to + #get user interaction #######-Event Handlers################################ sub Main_Terminate{ -1; #Terminates loop }; sub Button_Click{ my $arg = $_[0]; #my $size = @chosen; unless( @chosen[9] ) { print "$arg was choosen\n"; push (@chosen, $ar +g); #my $message = Win32::MsgBox("$arg was choosen", 0, "$arg was choosen +"); } else { print "To many numbers chosen\n";} };

P.S. As you can tell from my profile, I am new to perlmonks. And sorry about the formatting, I had some trouble... (mostly laziness)

Thanks,
stringplayer92

Replies are listed 'Best First'.
Re: Subroutines not working right
by Joost (Canon) on Feb 21, 2008 at 23:42 UTC
    Point of order: please put code in <code> ... </code> tags, as per writeup formatting tips.

    Anyway,

    onClick => &Button_Click(1)

    is probably not what you want. That will pass the return value of the call to Button_OnClick(1). If you want to pass a reference to a function, use -onClick => \&Button_OnClick.

    I'm not sure that -onclick is even a valid argument, but then again, I don't know Win32::GUI. You may want to re-read the documentation.

      Thanks for the tip,
      I actually realized that right before seeing your reply...
      I will look into using a reference to the function
      Windows programming is so annoying....

      Stringplayer92

        I notice you had an argument. If you need a "reference with arguments", create a small anon routine
        $func = \&foo; -vs- $func = sub { foo(1) };