Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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.....