develooper Front page | perl.perl5.porters | Postings from March 2022

Re: Pre-RFC: builtin:: functions for detecting numbers vs strings

Thread Previous | Thread Next
From:
Glenn Golden
Date:
March 12, 2022 14:10
Subject:
Re: Pre-RFC: builtin:: functions for detecting numbers vs strings
Message ID:
Yiypud8NCvWXu3E+@huh.zplane.com
demerphq <demerphq@gmail.com> [2022-03-12 14:23:12 +0800]:
> On Sat, 12 Mar 2022, 05:19 William Lindley, <wlindley@wlindley.com> wrote:
> 
> > In all this discussion, what happens when reading a file
> 
> 
> Any data that was read from a file would be a string unless you did
> something to tell perl otherwise.
> 
> containing the
> > line:
> >
> > 01730
> >
> > which is the postal code for my hometown.  This "looks like" a number
> 
> 
> I don't believe it does actually. The leading 0 means that
> looks_like_number() should reject it.
> 

Could you expand a bit on what you meant above by "should reject"?

I didn't understand if you meant it in the sense of "should be rejected
by looks_like_number() as implemented in the upcoming Perl release", or
if you meant more generally that rejection "should be the current _and_
future behavior".

I'm asking because my experience with looks_like_number() has been (for
many years, up to and including 5.34) based on the attached script and
set of test case strings, several of which have leading zeros.  TTBOMR,
the leading-zeros cases in the first group have always been classified
as "a number" by Scalar::Util::looks_like_number().

I added a few more cases just now for a little better test coverage,
and all seem to still accord with my historical (and present) expectations
as a Certified Perl Moron.  Am I making some mistake in the way that the
test script makes its determination?

Are any of these test cases expected to change in the upcoming release?

Thanks,

Glenn

==========================   TEST SCRIPT  ===================================
#!/usr/bin/perl

use 5.034;
use warnings;
use strict;

use Scalar::Util qw / looks_like_number /;

my $lln;
while (<STDIN>)
{
    chomp;
    if (/^#/) { print "$_\n"; next; }
    if (/^$/) { print "\n"; next; }
    $lln = looks_like_number($_) ? " looks like" : " doesn't look like";
    $lln .= " a number";
    printf("%25s %-30s\n", "'${_}'", $lln);
}
=============================================================================

========================    TEST CASES  =====================================
#
# Morons expect "looks_like_number()" to succeed:
#
123
+123
-123
123.4
123.4E5
123.4E05
123.4E+5
123.4E+05
123.4E+000000005
123.4E-000000005
123.4e-000000005
0000123.4E-000000005
123E+456789
0123
+0123
-0123
0123.4
0123.4E5
0123.4E05
0123.4E+5
0123.4E+05
0123.4E+000000005
0123.4E-000000005
0123.4e-000000005
0000123.4E-000000005
0123E+456789
0
+0
-0
Inf    
Infinity
+Inf  
   -Inf
inf
InFiniTy
   -InFiniTy   

#
# Morons expect "looks_like_number()" to fail:
#
123.4E-+000000005
123.4E+-000000005
123.4g-000000005
123E+4.5
0123.4E+-000000005
0123.4E-+000000005
0123.4g-000000005
0123E+4.5
0x123
0x0123
A Number
=============================================================================

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About