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?


In reply to validate if output of command is numeric or not by kaka_2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.