in reply to Detecting if a scalar has a number or string

So, what you're looking for is the mythical can_usefully_act_as_a_number() function. Riiiight.

The problem isn't a Perl issue. There is some very simple XS code that determines if the internal SV contains a value in the number slot. (I think this is what DBI does in its "Is it a number?" function.)

The problem is actually a semantic one. What do you consider to be a useful number? For example, why do you think Scalar::Util's function is called "looks_like_number" instead of "is_number"? It's because what one person considers a number, another person won't.

Why don't you describe what it is that determines if something can usefully act as a number? I think you'll find it a lot more difficult than you realize. And, without the requirements, writing the code is a little more difficult.

Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Replies are listed 'Best First'.
Re^2: Detecting if a scalar has a number or string
by rrwo (Friar) on Nov 18, 2004 at 18:36 UTC

    You've completely missed the point of my post! I don't care about semantics, I care about whether the numeric operators (specifically the comparison operators for my needs) are defined. Either it's a native Perl number or it's an overloaded object (such as a BigInt). If it acts just like a number as far as Perl is concerned, then it's a number as far as I'm concerned.

      (Go back and read my reply more carefully, specifically the last paragraph.)

      So, you want only those things that are either numeric scalars or which inherit from overload and can provide the numeric comparisons. That's a good definition of what you feel a number is.

      To achieve that, you will probably want to do something along these lines:

      use Scalar::Utils qw( blessed ); use DBI qw( looks_like_number ); use overload; sub my_is_number { my $x = shift; unless ( blessed( $x ) ) { return looks_like_number( $x ); } if ( overload::Method( $x, '<=>' ) ) { return !!1; } return; }

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      If it acts just like a number as far as Perl is concerned, then it's a number as far as I'm concerned.

      You can let Perl tell you what it thinks then...

      #!perl use strict; use warnings; sub seems_like_number { my $thing = shift; use warnings qw/FATAL all/; # Promote warnings to fatal, so # they can be trapped. The effect is # lexically scoped. eval { $thing += 0; }; return $@ ? 0 : 1; } while ( <DATA> ){ chomp; if( seems_like_number( $_ ) ) { printf "%10s seems like a number.\n", $_; } else { printf "%10s doesn't seem like a number.\n", $_; } } __DATA__ 1234 1.234 -2.345 1-1+2 0.032 .321 ASDF ASDF1234 1234ASDF

      The first line after __DATA__ is intentionally blank, to test whether or not an empty string will qualify as a number... and of course it won't.

      Ok, now if Perl thinks it's not a number, you'll know about it, and if Perl doesn't object to it being considered a number, you'll know that too.


      Dave

        Note that looks_like_number() tells you exactly the same thing, just without all of the overhead. (:

        - tye