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

Many thanks, you saved my day, this approach works perfectly for me.
  • Comment on Re^2: How to check if a scalar value is numeric or string?

Replies are listed 'Best First'.
Re^3: How to check if a scalar value is numeric or string?
by ig (Vicar) on Aug 28, 2009 at 00:55 UTC

    But watch out for side effects.

    my $x = 1; my $y = '1'; print "x is a ", (($x ^ $x)?"string":"number"), "\n"; print "y is a ", (($y ^ $y)?"string":"number"), "\n"; print "\$x = $x\n"; print "\$y = $y\n"; print "x is a ", (($x ^ $x)?"string":"number"), "\n"; print "y is a ", (($y ^ $y)?"string":"number"), "\n"; my $z = $x + $y; print "\$z = $z\n"; print "x is a ", (($x ^ $x)?"string":"number"), "\n"; print "y is a ", (($y ^ $y)?"string":"number"), "\n";

    gives

    x is a number y is a string $x = 1 $y = 1 x is a number y is a string $z = 2 x is a number y is a number

    It might be better to revise your program so that it doesn't depend on such a subtle distinction.