in reply to detecting of scalar is string or numeric

Its better not to do detection, but simply force numification if you're expecting a number, and let warnings issue a warning

#!/usr/bin/perl -- use strict; use warnings; use B(); sub SvTYPE (\[$@%&*]) { use B(); ref(B::svref_2object($_[0])) } { my $x = 42; TYPE(\$x); } { my $x = 42.24; TYPE(\$x); } { my $x = "42.24"; TYPE(\$x); $x=$x+0; print SvTYPE($x), "\n"; } { my $x = "00042"; TYPE(\$x); $x=$x+0; print SvTYPE($x), "\n"; } { my $x = "42\x{260E}"; TYPE(\$x); } sub TYPE { no warnings; print $_[0],' = ', ${$_[0]},' = ', ref(B::svref_2object($_[0])), " +\n"; } __END__
SCALAR(0xae664c) = 42 = B::IV
SCALAR(0xae637c) = 42.24 = B::NV
SCALAR(0xae633c) = 42.24 = B::PV
B::PVNV
SCALAR(0xae627c) = 00042 = B::PV
B::PVIV
SCALAR(0xae609c) = 42☎ = B::PV

Replies are listed 'Best First'.
Re^2: detecting of scalar is string or numeric (SvTYPE)
by ikegami (Patriarch) on Aug 10, 2018 at 07:19 UTC

    That doesn't help.

    Those class names just tell you the type of scalar, but nothing about the value within.

    For example, if you get B::IV, all you know is that the scalar contains one of the following:

    • Nothing (undef)
    • A signed integer
    • An unsigned integer
    • A reference

    For example, if you get B::PVIV, all you know is that the scalar contains one of the following:

    • Nothing (undef)
    • A signed integer
    • An unsigned integer
    • A string
    • A reference
    • A signed integer and a string
    • An unsigned integer and a string