Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re3: Pure Perl Internals (with Pure Perl Segfaults)

by blakem (Monsignor)
on Mar 18, 2004 at 06:50 UTC ( #337626=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: Pure Perl Internals (with Pure Perl Segfaults)
in thread Pure Perl Internals (with Pure Perl Segfaults)

Ingy needed a pure Perl method of determining if a scalar contained an integer or a string
While his results are cool, he could have saved a lot of work if he'd asked around to see if anyone had already found a solution to this problem (note the unary "~", it's important)
sub is_integer { ~$_[0] !~ /\D/ }
Here are some tests to demonstrate that it works:
#!/usr/bin/perl -wT use strict; use Test; BEGIN { plan tests => 345 } for my $num (-56789, -300, -1, 0..100, 5345, 6574572, 23457356) { ok( is_integer($num) ); my $string = "$num"; ok( not is_integer($string) ); } for my $string ((map { chr($_) } 0..127), 'dog', 'cat', 'mouse') { ok( not is_integer($string) ); } sub is_integer { ~$_[0] !~ /\D/ }
I found the is_integer() implementation in my cool-snippets-from-perlmonks directory, but can't seem to find the original thread.....

-Blake


Comment on Re3: Pure Perl Internals (with Pure Perl Segfaults)
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2014-07-10 05:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (199 votes), past polls