Operating System - HP-UX
1819966 Members
3699 Online
109607 Solutions
New Discussion юеВ

Re: Couple Perl questions

 
SOLVED
Go to solution
Jason Berendsen
Regular Advisor

Couple Perl questions

Hello,

I am trying to find a way to find if a variable is numeric. Does anyone know how the isnum function works? Do you need to define a library to use it? Is there another way to do this.

Also, is there an equivalent to the ksh $? in Perl for the return code of a command?

Thanks,

Jason

3 REPLIES 3
Jordan Bean
Honored Contributor

Re: Couple Perl questions

In PERL, $? is also known as $CHILD_ERROR. It holds the error value of the last `cmd`, pipe close, wait, waitpid, or system function.

Also at your disposal is $@ ($EVAL_ERROR) which is the error value for the last eval or do expression.

I am not aware of any such implicit variable that holds the error value of the last PERL internal function.

Rodney Hills
Honored Contributor
Solution

Re: Couple Perl questions

In perl to test for number, one way is

if ($x=~/^\d+$/) {
print "it is a number\n";
} else {
print "not a number\n";
}

-- Rod Hills
There be dragons...
H.Merijn Brand (procura
Honored Contributor

Re: Couple Perl questions

If something is a number is much more complicated than Rodney shows. Think about fractionals and exponents. The FAQ show a complete example of a complicated regex that can deal with all.

If it is digits only, you could (more safely) do

m/^\s*\d+\s*$/; # strip leading and trailing spaces

Jordan, the long names are *only* available under 'use English'. Use $? and $! after simple system interaction, and $@ after eval
Enjoy, Have FUN! H.Merijn