in reply to How to check if a scalar value is numeric or string?

Thanks to the sugestions above i have the function isnum:
sub isnum ($) { $_[0] ^ $_[0] ? 0 : 1 }
XORing a number gives zero, so the function returns 1. XORing a string gives a string of nulls, still a non-empty string, so the function returns 0

Replies are listed 'Best First'.
Re^2: How to check if a scalar value is numeric or string?
by ibravos (Initiate) on Mar 09, 2012 at 11:02 UTC
    Correction: isnum('') returns 1, so here is a better version
    sub isnum ($) { return 0 if $_[0] eq ''; $_[0] ^ $_[0] ? 0 : 1 }