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

I have the following event handler dealing with a change in a Wx::TextCtrl:

Wx::Event::EVT_TEXT($self, $IcqText, sub { my ($self, $event) = @_; $self->{AMP1}->Icq(($IcqText->GetValue) / 1000 +);});

Where $IcqText->GetValue returns a wxString and the / 1000 throws the warning Argument "" isn't numeric in division (/) at ./CEAmp.pl line 163. The division is successfully completed.

The following modification masks the warning:

Wx::Event::EVT_TEXT($self, $IcqText, sub { my ($self, $event) = @_; { no warnings; $self->{AMP1}->Icq(($IcqText->GetValue) / 1000 +); } });

Is there an alternate way to "cast" the wxString to numeric?

James

There's never enough time to do it right, but always enough time to do it over...

Replies are listed 'Best First'.
Re: "Cast" a wxString to Numeric?
by Corion (Patriarch) on May 02, 2015 at 13:16 UTC

    Do you really want to treat the empty string as zero? If so, I would simply be more explicit about it:

    my $numerator= $IcqText->GetValue; if( $numerator eq '') { $numerator= 0 }; $self->{AMP1}->Icq( $numerator / 1000 );

      Corion, thanks for your reply.

      Yes, I think I need to go with your suggestion. The event gets triggered every time the TextCtrl is modified and if you backspace until you erase the entire entry the value will end up "" which needs to be interpreted as 0 for my purposes.

      James

      There's never enough time to do it right, but always enough time to do it over...

Re: "Cast" a wxString to Numeric?
by Anonymous Monk on May 03, 2015 at 01:08 UTC

    Sounds like you want to use a Wx::SpinCtrl/Wx::SpinCtrlDouble and not a Wx::TextCtrl

    wxperl_demo --show wxSpinCtrl

    Perl doesn't have wxString :)

      Thanks, I'll give it a look. The response from Corion showed me that the warning message was telling me something different than I had assumed. Yes, wxString is from the wxWidgets docs.

      James

      There's never enough time to do it right, but always enough time to do it over...

        To close the loop, wxSpinCtrl was the better solution as it eliminates the possibility of a divide by zero caused by an empty/completely erased field.

        James

        There's never enough time to do it right, but always enough time to do it over...