Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: Detecting if a scalar has a number or string

by rrwo (Friar)
on Nov 18, 2004 at 18:36 UTC ( [id://408845]=note: print w/replies, xml ) Need Help??


in reply to Re: Detecting if a scalar has a number or string
in thread Detecting if a scalar has a number or string

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.

  • Comment on Re^2: Detecting if a scalar has a number or string

Replies are listed 'Best First'.
Re^3: Detecting if a scalar has a number or string
by dragonchild (Archbishop) on Nov 18, 2004 at 18:54 UTC
    (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.

Re^3: Detecting if a scalar has a number or string
by davido (Cardinal) on Nov 18, 2004 at 23:52 UTC

    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        

        For the record, you're absolutely right. Scalar::Util's looks_like_number() is 655% faster than my eval trap fatal warning approach. But for some reason I do get a sort of a kick out of the idea of letting perl (the interpreter) tell me if a scalar is a number, rather than Perl (the script). Consider my solution purely an academic enveavor, and use the module for production code.

        The benchmark script:

        The results:

        Rate Seems Looks Seems 38.8/s -- -87% Looks 297/s 665% --

        Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://408845]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (1)
As of 2024-04-25 04:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found