|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Can anyone explain this to me, it returns 1 when a number is passed and 0 if a non number is passed to it, but I don't understand how.
sub isNumber { my $n = shift; local $noError = 1; local $^W = 1; local $SIG{__WARN__} = sub { $noError = undef }; eval { my $x = $n + 0 }; return ($noError); } |
|
#2
|
||||
|
||||
|
Hi!
Everything happens in this line: eval { my $x = $n + 0 }; The sub is testing whether a number (i.e. 0) may be added to the input without causing a warn. If this succeeds, the input is a number. Otherwise it's text, etc. The remaining parts of the sub is just overhead to assign the input, define some vars and to redirect the warn. Regards, Atrus.
__________________
Webmaster - Stefan Meier KG TABAKWAREN - Pfeifen, Premium-Zigarren, ... (_Ger) |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Isnumber function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|