Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://337626]
[virtualsue]: I have a quiet Perl themed slack workspace
[virtualsue]: The person who talks to me on it the most *hates* Perl but she hates everything
[virtualsue]: If anyone is interested, ping me an email address for an invitation.
[virtualsue]: I welcome any talktaive people who do not mind using a propietary walled-garden service for free
[virtualsue]: or even proprietary
[virtualsue]: we talk about programming and music and stuff
herveus waves to virtualsue

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (13)
As of 2017-09-26 14:00 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (295 votes). Check out past polls.