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

Dear Monks

in my perl script, I am running few commands which result 0 or < 0 which will be later used for an arithmetic calculation.

but in some cases command may throw error and arithmetic calculation may give an result which is not expected.

so I would like to validate if return output from command is numeric or not.

I see many people recommended if section in below code.

use warnings; my $cmd = "cmd"; my $value = qx($cmd); my $cmd1 = "cmd1"; my $value1 = qx($cmd1); if (($value+0) eq $value) { print $value . " is numeric"; } else { print $value . " is not numeric"; } if (($value1+0) eq $value1) { print $value1 . " is numeric"; } else { print $value1 . " is not numeric"; } my $total = $value + $value1; print $total; // this gives me total of two values.

I tried to use the same but always result is not numeric which it could be but then i would expect warning while performing the arithmetic operation but i get correct result there so i believe $value is numeric but validation code has some problem.

any pointers?

Replies are listed 'Best First'.
Re: validate if output of command is numeric or not
by Discipulus (Canon) on Jan 08, 2016 at 08:44 UTC
    Hello,

    why do you not just print the result? like in print "DEBUG \$value is: [$value]\n";
    Do the same for the other value so you'll be sure of the content of them. Remember that is possible the returned value is a multiline string, which probaly is not exactly a 'numeric value' (see qx at Quote like Operators).

    Having done the above you can grab the numeric part with a regex (multiline switch?) and do te equality test:
    if ($num1 eq $num2){ ... }

    HtH
    L*
    UPDATE: If the command return an error it is probably sent to STDERR that is not affected by qx operator. See the yet mentioned doc to see how workaround that issue. You must first check if the command executed correctly: perlvars contains infos about error variables, anyway try something like
    perl -e "$ret = qx/error_returning_command/;print qq([$ret] [$^E] [$ +{^CHILD_ERROR_NATIVE}])"
    to see what your commands return.

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: validate if output of command is numeric or not (numify)
by Anonymous Monk on Jan 08, 2016 at 08:47 UTC
Re: validate if output of command is numeric or not
by BillKSmith (Monsignor) on Jan 08, 2016 at 14:03 UTC
    You can have either of two types of problems. Test for each separately because you probably want to respond to each differently. Choose one of the suggested methods to verify that the command ran without error. If successful, use a regex to verify that the result is "reasonable" for your calculations.
    Bill
Re: validate if output of command is numeric or not
by Anonymous Monk on Jan 09, 2016 at 09:37 UTC
    The try of mathematics is nice, but flames your brain. You may much faster through using regex "\d+". Please note by this time, that "eq" is not the same than "==" inside of perl!
    sub check { my $value=""; if ($_[0]=~ m/\d+/) { $value=$_[0]; print qq~$_[0] is numeric\n~; } el +se { $value=""; print qq~$_[0] is not numeric\n~; } return($value); } my $in=0815; my $out=&check($in); # have phun!

      Again.

      sub _isNumeric($){ my $t = shift; defined($t) or confess; no warnings; my $a = $t; $a+=0; return $a eq $t; }

      This should be built in! Doing $a+=0 issues a warning. Unless you a a very careless programmer your code must issue no warnings for valid input. So if you are testing if input is numeric (where it may or may not be, both cases valid) then you have to jump through hoops like this.

      That code above may not work for all cases but it is enough for my purposes.

      Yet another reason to hate Perl. Fix it Larry!

        Your _isNumeric functions nearly the same as Scalar::Util's looks_like_number, mentioned by the other anon elsewhere in this thread. Scalar::Util is a core module, and exposes Perl's internal looks_like_number function, so it pretty much is built in, and your _isNumeric is just using it in a kind of roundabout way. Sorry, but this is a hoop you've built for yourself!