Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I am quite new to Perl world and try to comply RTFM rule. However I could not figure out how to use Posix isdigit function. Any help more than welcome. Regards, Nevzat

Replies are listed 'Best First'.
Re: how do I use Posix isdigit function?
by japhy (Canon) on May 09, 2002 at 06:06 UTC
    Well, it seems its as simple as:
    use POSIX 'isdigit'; if (isdigit($c)) { print "$c is a digit" }
    However, I don't think you really need to use this function. A regex might be faster: $c =~ /\A\d\z/ for example. Or, if you know you're dealing with ASCII: length($c) == 1 and ord($c) >= ord(0) and ord($c) <= ord(9) -- it's really up to you, though.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: how do I use Posix isdigit function?
by Zaxo (Archbishop) on May 09, 2002 at 06:22 UTC

    For the mechanics of calling POSIX functions, you can keep the overhead down by only importing the names you want to use:

    #!/usr/bin/perl use warnings; use strict; use POSIX qw( isdigit ); for (<DATA>) { print $_, isdigit($_) ? ' is' : ' is not'; ' all digits.', $/; } __DATA__ 7 5 A 75A 433 12340
    Note that perl's POSIX isdigit takes strings of any length, so this is not just like /\d/. The equivalent is /^\d+$/.

    After Compline,
    Zaxo

Re: how do I use Posix isdigit function?
by grep (Monsignor) on May 09, 2002 at 06:05 UTC
    I would recommend using a regex instead. 'm/\d/' or a varient of that regex will get you what you need.

    A regex is much more idiomatic and you can save the overhead of the POSIX module

    UPDATE: From the docs
    This is identical to the C function, except that it can apply to a single character or to a whole string. Consider using regular expressions and the /[[:isdigit:]]/ construct instead, or the /\d/ construct



    grep
    Unix - where you can throw the manual on the keyboard and get a command