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

Hello Monks, regarding the following code
my $PreMute = "Off"; Win32::GUI::Dialog(); #line 174 sub pre_mute_button_Click { $PreMute = "xxx"; if($PreMute eq "On") { $PreMute = "Off"; } else { $PreMute = "On"; } }

When ever I press the pre_mute_button button I get this message "Argument "On" isn't numeric in subroutine entry at C:\guardian_service\guardian_service.pl line 174." What puzzles me is that this assignment (added for debug) $PreMute = "xxx"; causes no messages but the assignments within the if statement generate the message What am I doing wrong Regards Justin

PS Sorry if this is repeated, I thought I was previously logged on

Replies are listed 'Best First'.
Re: Argument "On" isn't numeric in subroutine entry
by GrandFather (Saint) on Nov 20, 2013 at 07:24 UTC

    I've not used the Win32::GUI stuff, but I suspect that pre_mute_button_Click() is called as a button click handler. Very likely the calling code expects a numeric value returned from your sub. However that's not wat is returned. The following may be instructive:

    print pre_mute_button_Click(); sub pre_mute_button_Click { $PreMute = "xxx"; if($PreMute eq "On") { $PreMute = "Off"; } else { $PreMute = "On"; } }

    Prints:

    On

    The reason that happens is that Perl subs return the last value evaluated if there is no explicit return statement. The assignments are the last thing evaluated so the result of whichever assignment happens is what is returned.

    You need to read the documentation to find out what value is expected to be returned and provide an explicit return statement to ensure the right value is used.

    True laziness is hard work
      Thank you. That fixed my problem. Cheers Justin