in reply to Re^2: How to capture the "isn't numeric" warning?
in thread How to capture the "isn't numeric" warning?

Hi,

I think isNumber() is doing the same as Scalar::Util::looks_like_number(), except that the former is returning '0' for FALSE, whereas the latter returns '' (the empty string) for FALSE.
One slight advantage I can see in using looks_like_number() is that it doesn't require the warnings pragma.
And, I think, isNumber() is rather convoluted in that int($N) is going to perform essentially the same check as looks_like_number() and then catch the warning if that check fails, before assigning 0 to $R and returning. So it's also a little less efficient.

But that just makes isNumber() the more interesting solution !

Cheers,
Rob

Replies are listed 'Best First'.
Re^4: How to capture the "isn't numeric" warning?
by ikegami (Patriarch) on Jun 15, 2019 at 15:12 UTC

    whereas the latter returns '' (the empty string) for FALSE.

    It does not.

    $ perl -MScalar::Util=looks_like_number -we' CORE::say 0+""; ' Argument "" isn't numeric in addition (+) at -e line 2. 0 $ perl -MScalar::Util=looks_like_number -we' CORE::say 0+looks_like_number("abc"); ' 0

    It returns the usual empty string, 0, 0.0 triple-var Perl uses for false (sv_no).

      It does not.

      Really ??
      Could you show me a Devel::Peek::Dump output for the scalar returned by S::U::looks_like_number('abc') - one that shows that the empty string is not returned ?
      (Also, the version number of Scalar::Util on which this happens, and the 'perl -V' output.)

      Scalar-Util-1.45 (perl-5.16.0):
      C:\>perl -MScalar::Util -MDevel::Peek -le "Dump(Scalar::Util::looks_li +ke_number('abc'));" SV = PVNV(0x726f74) at 0x723250 REFCNT = 2147483647 FLAGS = (IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK) IV = 0 NV = 0 PV = 0x7229b4 ""\0 CUR = 0 LEN = 12
      Scalar-Util-1.5 (perl5.30.0):
      C:\>perl -MScalar::Util -MDevel::Peek -le "Dump(Scalar::Util::looks_li +ke_number('abc')); SV = PVNV(0x2ea0a8) at 0x2e8150 REFCNT = 2147483647 FLAGS = (IOK,NOK,POK,READONLY,PROTECT,pIOK,pNOK,pPOK) IV = 0 NV = 0 PV = 0x6ff24e83 "" CUR = 0 LEN = 0
      My assertion the "the empty string is returned" could well be deemed incomplete because the IV and NV slots are also filled.
      But the PV slot definitely contains the empty string, and it's the contents of the PV slot that are displayed by print().

      AFAICS, any assertion that looks_like_number does not return the empty string for FALSE is complete bullshit.
      (But I upvoted your post anyway - on the strength of the boldness and abrasiveness of its opening assertion.)

      Cheere,
      Rob