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

Hi Monks, I want perl to NOT to display its own error messages, when i am handling it myself. Pls suggest me any way to suppress that. Below is the error messege i get from perl, followed by my own error message

Argument "hu" isn't numeric in numeric gt (>) at ./cpu_chk.pl line 120, <STDIN> line 3. ---->(Perl Error)

Bad Choice...Try again now.. ----> (my error msg)

The condition i have in my code is as below :

if(($SRVCH > 22)||($SRVCH < 1)||($SRVCH =~ /(\s+)/)||($SRVCH =~ /[a-z +A-Z]/)) { message("Bad Choice...Try again now.."); system("sleep 1.5"); disp_menu(); }

Replies are listed 'Best First'.
Re: Suppressing Warning/Error messages
by Corion (Patriarch) on Aug 27, 2015 at 10:19 UTC

    The best thing would be to simply check that $SRVCH conforms to what you expect, that is, a sequence of only digits, before doing the numerical checks:

    if( ($SRVCH =~ /\D/) # a non-digit || ($SRVCH > 22) # or greater 22 ||($SRVCH < 1) # or smaller than 1 ||($SRVCH =~ /(\s+)/) # or empty string (but see above) ||($SRVCH =~ /[a-zA-Z]/) # or a to z ){ ...

    Also, there is no need to shell out to sleep, because Perl has sleep as a built-in function. See also Time::HiRes if you need sub-second sleep.

      Hi Corion - Thanks for reply. The input i am expecting is definitely Numeric. But what i am trying to achieve here is, if end user happens to choose a BAD choice(which could be anything else but a number), The message displayed should be simply "BAD Choice". I dont need Perl to Err in its way. How can that be achieved ?

        Restructure your if statement as I showed.

        Also, consider reading warnings as it shows how to suppress warnings. But in my opinion, it's much better to structure your code such that it recognizes bad input without raising warnings by, in your example, checking first whether something looks like a number before going on and checking whether it's larger than 22.