Scalar :: Util :: look_like_number behavior in Perl

I am trying to figure out if the input is a number or a string. I stumbled upon looks_like_numberand cannot understand the values ​​that it returns.

use warnings;
use Scalar::Util qw(looks_like_number);

my $name = 11;
print looks_like_number ($name);

This code prints 1if it $namecontains a string and a static number if it $namecontains an integer (i.e. 4352 for each integer).

I am using Perl for Windows.

+4
source share
3 answers

Here is what the documentation says:

look_like_number EXPR

Returns true if perl considers EXPR to be a number. See "Look_like_number" in perlapi.

The link to perlapi in this quote doesn’t really help us, unfortunately:

, SV ( ). Inf Infinity ( ), atof() . Get-magic .

I32   looks_like_number(SV *const sv)

, 0, .

  • 4352, 11.
  • '11', 1.

, .

  • 'test' 'foobar', 0, .
  • 1 , .
  • '1e1', 4, , .

, , - , Perl , , , . , true .

+7

! .

true?

? , . , .

?

, grok_number, .

(0, ), -ORed IS_NUMBER_IN_UV, IS_NUMBER_GREATER_THAN_UV_MAX, IS_NUMBER_NOT_INT, IS_NUMBER_NEG, IS_NUMBER_INFINITY, IS_NUMBER_NAN ( perl.h).

SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK)

, , , .

+15

Do not rely on an exact numerical value. This is an abstraction leak fixed by the latest version Scalar::Util(1.39). The important thing is that this is simply the truth of the result, not its exact numerical value.

See the error https://rt.cpan.org/Ticket/Display.html?id=94806

+3
source

All Articles