I am trying to find out if an input is number or string. I came across looks_like_number
and cannot understand the values it returns.
use warnings;
use Scalar::Util qw(looks_like_number);
my $name = 11;
print looks_like_number ($name);
This code prints 1
if $name
contains a string and a static number if $name
contains an integer (i.e. 4352 for each integer).
I am using Perl on Windows.
You forgot to ask a question! Here are two possibilities.
Why not? It returns a true value as documented. It makes no difference which true value it is.
If the scalar contains a string, it uses grok_number
which has specific document return values.
The type of the number is returned (
0
if unrecognised), otherwise it is a bit-ORed combination ofIS_NUMBER_IN_UV
,IS_NUMBER_GREATER_THAN_UV_MAX
,IS_NUMBER_NOT_INT
,IS_NUMBER_NEG
,IS_NUMBER_INFINITY
,IS_NUMBER_NAN
(defined inperl.h
).
Otherwise, it uses
SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK)
You can't tell which of the two was used, so you can't ascribe meaning to the value, which is why it's undocumented.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With